Home  >  Article  >  Java  >  How to send emails in java

How to send emails in java

小老鼠
小老鼠Original
2023-12-26 16:22:571271browse

You can use JavaMail API to send emails in Java. Valid email server information (such as SMTP server address, port, username and password, etc.) is required. Please note that some email service providers may require specific permissions or application passwords to be enabled in order to send emails from Java applications. Therefore, make sure you have configured the relevant permissions.

How to send emails in java

Operating system for this tutorial: Windows 10 system, Dell G3 computer.

In Java, you can use the JavaMail API to send emails. Below is a simple example showing how to send mail using JavaMail API. Please note that you need to provide valid email server information (such as SMTP server address, port, username and password, etc.).

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EmailSender {
    public static void main(String[] args) {
        // 邮件服务器配置信息
        String host = "your_smtp_host";
        String username = "your_email_username";
        String password = "your_email_password";
        int port = 587; // SMTP端口号,一般为587
        // 发件人和收件人信息
        String from = "your_email@example.com";
        String to = "recipient@example.com";
        // 创建邮件会话
        Properties properties = new Properties();
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", String.valueOf(port));
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        Session session = Session.getInstance(properties, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        try {
            // 创建邮件对象
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject("Test Email");
            message.setText("This is a test email sent from Java.");
            // 发送邮件
            Transport.send(message);
            System.out.println("Email sent successfully!");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

In this example, you need to replace the following information:

your_smtp_host: Your SMTP server address.

your_email_username: Your email username.

your_email_password: Your email password.

your_email@example.com: The sender’s email address.

recipient@example.com: Recipient email address.

Please note that some email service providers may require specific permissions or application passwords to be turned on in order to send emails from Java applications. Therefore, make sure you have configured the relevant permissions.

The above is the detailed content of How to send emails in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn