美文网首页
Javamail发送邮件

Javamail发送邮件

作者: jsjack_wang | 来源:发表于2018-01-09 19:43 被阅读0次
    1.添加Maven依赖
     <dependency>
      <groupId>javax.mail</groupId>
      <artifactId>mail</artifactId>
      <version>1.4</version>
    </dependency>
    <dependency>
      <groupId>javax.activation</groupId>
      <artifactId>activation</artifactId>
      <version>1.1</version>
    </dependency>
    
    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<String> 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<String> recipients = new ArrayList<String>();
            recipients.add(recipient);
            send(recipients, email);
        }
    
        /**
         * 群发邮件
         */
        public static void massSend(List<String> 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<String> recipients = new ArrayList<String>();
        recipients.add("2668659188@qq.com");
    
        massSend(recipients, simpleEmail);
    }
    

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

    相关文章

      网友评论

          本文标题:Javamail发送邮件

          本文链接:https://www.haomeiwen.com/subject/ztvgnxtx.html