1. Jar packages are imported into pom files

<dependency>
	<groupId>javax.mail</groupId>
	<artifactId>mail</artifactId>
	<version>1.5.0-b01</version>
</dependency>
Copy the code

2. Create a mail sending class

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 发邮件
 */
public class SendEmail implements Runnable {

    /**
     * 邮件服务器地址
     */
    private String host;

    /**
     * 端口号
     */
    private String port;

    /**
     * 发件人邮箱
     */
    private String from;

    /**
     * 发件人邮箱登录密码
     */
    private String password;

    private SendEmail() {
    }

    public SendEmail(String host, String port, String from, String password) {
        this.host = host;
        this.port = port;
        this.from = from;
        this.password = password;
    }

    private void send() throws Exception {
        Properties properties = new Properties();
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", port);
        properties.put("mail.smtp.auth", true);
        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.socketFactory.port", port);
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from, password);
            }
        });
        MimeMessage message = new MimeMessage(session);
        // 设置发送方地址、发送方昵称、编码方式
        message.setFrom(new InternetAddress(from, "掘金", "UTF-8"));
        // 设置接收方地址(多个)
        String[] recipientArr = {"[email protected]", "[email protected]", "[email protected]"};
        InternetAddress[] recipient = new InternetAddress[recipientArr.length];
        for (int i = 0; i < recipientArr.length; i++) {
            recipient[i] = new InternetAddress(recipientArr[i]);
        }
        message.setRecipients(Message.RecipientType.TO, recipient);

        // 一次只发送一个接收方
        // message.setRecipient(Message.RecipientType.TO,new InternetAddress("[email protected]"));

        // 设置抄送方地址(多个)
        String[] copyRecipientArr = {"[email protected]", "[email protected]"};
        InternetAddress[] copyRecipient = new InternetAddress[recipientArr.length];
        for (int i = 0; i < copyRecipientArr.length; i++) {
            copyRecipient[i] = new InternetAddress(copyRecipientArr[i]);
        }
        message.setRecipients(Message.RecipientType.CC, copyRecipient);

        // 一次只发送一个抄送方
        // message.setRecipient(Message.RecipientType.CC, new InternetAddress("[email protected]"));

        // 设置邮件主题
        message.setSubject("Java mail 测试", "UTF-8");
        // 设置邮件正文(发送html格式)
        message.setText("<h1>Hello</h1>", "UTF-8", "html");
        
        // 不发送html格式
        // message.setText("这是一封测试邮件!", "UTF-8");

        // 发送邮件
        Transport.send(message);
    }

    @Override
    public void run() {
        try {
            send();
        } catch (Exception e) {
            System.out.println("邮件发送异常");
            e.printStackTrace();
        }
    }
}
Copy the code

Use 3.

Public static void main(String[] args) {new SendEmail("","","","").run(); }Copy the code