Javamail发送邮件

1.添加Maven依赖
 
  javax.mail
  mail
  1.4


  javax.activation
  activation
  1.1

2.创建email.properties,设置发邮件邮箱的用户名密码
email.username=
email.password=
3.添加EmailAuthenticator继承Authenticator
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class EmailAuthenticator extends Authenticator {

    private String username;

    private String password;

    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
    }
}
4.创建EmailUtil工具类
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public abstract class EmailUtil {

    private static Session session = null;

    private static EmailAuthenticator authenticator = null;

    static {
        try {
            InputStream inputStream = EmailUtil.class.getResourceAsStream("/email.properties");
            Properties properties = new Properties();
            properties.load(inputStream);

            authenticator = new EmailAuthenticator();
            String username = properties.getProperty("email.username");
            authenticator.setUsername(username);

            String password = properties.getProperty("email.password");
            authenticator.setPassword(password);

            String smtpHostName = "smtp." + username.split("@")[1];
            properties.put("mail.smtp.auth", "true");
            properties.put("mail.smtp.host", smtpHostName);

            session = Session.getInstance(properties, authenticator);
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private EmailUtil() { }

    /**
     * 通用发邮件方法
     */
    private static void send(List recipients, SimpleEmail email) throws MessagingException {
        final MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(authenticator.getUsername()));
        InternetAddress[] addresses = new InternetAddress[recipients.size()];
        for (int index = 0; index < recipients.size(); index ++) {
            addresses[index] = new InternetAddress(recipients.get(index));
        }
        message.setRecipients(RecipientType.TO, addresses);
        message.setSubject(email.getSubject());
        message.setContent(email.getContent(), "text/html;charset=utf-8");

        Transport.send(message);
    }

    /**
     * 发送邮件
     */
    public static void send(String recipient, SimpleEmail email) throws MessagingException {
        List recipients = new ArrayList();
        recipients.add(recipient);
        send(recipients, email);
    }

    /**
     * 群发邮件
     */
    public static void massSend(List recipients, SimpleEmail email) throws MessagingException {
        send(recipients, email);
    }
}
5.测试
public static void main(String[] args) throws Exception {
    SimpleEmail simpleEmail = new SimpleEmail();
    simpleEmail.setSubject("今天你学习了么?");
    simpleEmail.setContent("今天你写博客了么");

    List recipients = new ArrayList();
    recipients.add("[email protected]");

    massSend(recipients, simpleEmail);
}

代码地址:https://gitee.com/jsjack_wang/JavaDemo

你可能感兴趣的:(Javamail发送邮件)