欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java发送邮件(网易邮箱)

程序员文章站 2024-03-20 10:46:22
...

1.准备工作

开启设置客户端授权码,记住设置的授权码
Java发送邮件(网易邮箱)
开启服务
Java发送邮件(网易邮箱)

2.代码

封装邮件信息

public class Mail {

    /**
     * 发件人
     */
    private String from;
    /**
     * 收件人
     */
    private StringBuilder toAddress = new StringBuilder();
    /**
     * 抄送
     */
    private StringBuilder ccAddress = new StringBuilder();
    /**
     * 暗送
     */
    private StringBuilder bccAddress = new StringBuilder();
    /**
     * 主题
     */
    private String subject;
    /**
     * 正文
     */
    private String content;


    public Mail() {}

    public Mail(String from, String to) {
        this(from, to, null, null);
    }

    public Mail(String from, String to, String subject, String content) {
        this.from = from;
        this.toAddress.append(to);
        this.subject = subject;
        this.content = content;
    }

    /**
     * 返回发件人
     * @return
     */
    public void setFrom(String from) {
        this.from = from;
    }

    /**
     * 返回发件人
     * @return
     */
    public String getFrom() {
        return from;
    }

    /**
     * 返回主题
     */
    public String getSubject() {
        return subject;
    }

    /**
     * 设置主题
     */
    public void setSubject(String subject) {
        this.subject = subject;
    }

    /**
     * 获取主题内容
     */
    public String getContent() {
        return content;
    }

    /**
     * 设置主题内容
     * @param content
     */
    public void setContent(String content) {
        this.content = content;
    }

    /**
     * 获取收件人
     * @return
     */
    public String getToAddress() {
        return toAddress.toString();
    }

    /**
     * 获取抄送
     * @return
     */
    public String getCcAddress() {
        return ccAddress.toString();
    }

    /**
     * 获取暗送
     * @return
     */
    public String getBccAddress() {
        return bccAddress.toString();
    }

    /**
     * 添加收件人,可以是多个收件人
     * @param to
     */
    public void addToAddress(String to) {
        if(this.toAddress.length() > 0) {
            this.toAddress.append(",");
        }
        this.toAddress.append(to);
    }

    /**
     * 添加抄送人,可以是多个抄送人
     * @param cc
     */
    public void addCcAddress(String cc) {
        if(this.ccAddress.length() > 0) {
            this.ccAddress.append(",");
        }
        this.ccAddress.append(cc);
    }

    /**
     * 添加暗送人,可以是多个暗送人
     * @param bcc
     */
    public void addBccAddress(String bcc) {
        if(this.bccAddress.length() > 0) {
            this.bccAddress.append(",");
        }
        this.bccAddress.append(bcc);
    }
}

验证发送

public class WyMail {
    public static Session createSession(String host, final String username, final String password) {
        Properties prop = new Properties();
        prop.setProperty("mail.host", host);// 指定主机
        prop.setProperty("mail.smtp.auth", "true");// 指定验证为true
        prop.setProperty("mail.smtp.starttls.enable", "true");
        prop.setProperty("mail.smtp.ssl.trust", "smtp.163.com");


        // 创建验证器
        Authenticator auth = new Authenticator() {
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        };

        // 获取session对象
        return Session.getInstance(prop, auth);
    }

    /**
     * 发送指定的邮件
     *
     * @param mail
     */
    public static void send(Session session, final Mail mail) throws MessagingException,
            IOException, AddressException {

        MimeMessage msg = new MimeMessage(session);// 创建邮件对象
        msg.setFrom(new InternetAddress(mail.getFrom()));// 设置发件人
        msg.addRecipients(Message.RecipientType.TO, mail.getToAddress());// 设置收件人

        //设置抄送
        String cc = mail.getCcAddress();
        if (!cc.isEmpty()) {
            msg.addRecipients(Message.RecipientType.CC, cc);
        }

        //设置暗送
        String bcc = mail.getBccAddress();
        if (!bcc.isEmpty()) {
            msg.addRecipients(Message.RecipientType.BCC, bcc);
        }
        //设置主题
        msg.setSubject(mail.getSubject());

        //创建部件集对象
        MimeMultipart parts = new MimeMultipart();
        //创建一个部件
        MimeBodyPart part = new MimeBodyPart();
        //设置邮件文本内容
        part.setContent(mail.getContent(), "text/html;charset=utf-8");
        //把部件添加到部件集中
        parts.addBodyPart(part);
        //给邮件设置内容
        msg.setContent(parts);
        //发邮件
        Transport.send(msg);
    }
}

配置文件

subject=邮件标题
content=邮件内容
from=发送者
host=smtp.163.com
username=邮箱地址(不含后缀名)
password=授权码

总结:
异常 javax.mail.MessagingException: Could not convert socket to TLS
解决方案:添加 prop.setProperty(“mail.smtp.ssl.trust”, “smtp.163.com”)
异常 javax.mail.MessagingException:[EOF]
结局方案:添加 prop.setProperty(“mail.smtp.starttls.enable”, “true”)