SpringBoot 腾讯企业邮箱

借鉴:http://www.cnblogs.com/LUA123/p/5575134.html
腾讯企业邮箱需要采用基于SSL的Mail登陆方式进行发送和接收电子邮件,并且需要设置企业邮箱的授权码。

在邮箱设置=》安全设置中,绑定微信获取微信的动态码,然后就可以添加授权码,授权码起到密码作用来登录邮箱。
SpringBoot 腾讯企业邮箱_第1张图片
application.properties配置文件:

#####企业邮箱#####
spring.mail.account=***@*****.com
spring.mail.pass=ewaOLNIhYXPklfqf
spring.mail.from=***@*****.com
spring.mail.host=smtp.exmail.qq.com
spring.mail.port=465
spring.mail.protocol=smtp

测试类:

package com.qianbao.baihua.oms;

import com.sun.mail.util.MailSSLSocketFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class CompanyEmailUtils {
    public CompanyEmailUtils() {
    }

    @Value("${spring.mail.account}")
    private String account;//登录用户名
    @Value("${spring.mail.pass}")
    private String pass;        //登录密码
    @Value("${spring.mail.from}")
    private String from;        //发件地址
    @Value("${spring.mail.host}")
    private String host;        //服务器地址
    @Value("${spring.mail.port}")
    private String port;        //端口
    @Value("${spring.mail.protocol}")
    private String protocol; //协议

    //用户名密码验证,需要实现抽象类Authenticator的抽象方法PasswordAuthentication
    static class MyAuthenricator extends Authenticator {
        String u = null;
        String p = null;

        public MyAuthenricator(String u, String p) {
            this.u = u;
            this.p = p;
        }

        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(u, p);
        }
    }

    @Test
    public void send() {
        Properties prop = new Properties();
        //协议
        prop.setProperty("mail.transport.protocol", protocol);
        //服务器
        prop.setProperty("mail.smtp.host", host);
        //端口
        prop.setProperty("mail.smtp.port", port);
        //使用smtp身份验证
        prop.setProperty("mail.smtp.auth", "true");
        //使用SSL,企业邮箱必需!
        //开启安全协议
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);
        Session session = Session.getDefaultInstance(prop, new MyAuthenricator(account, pass));
        session.setDebug(true);
        MimeMessage mimeMessage = new MimeMessage(session);
        try {
            mimeMessage.setFrom(new InternetAddress(from, "XXX"));
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("*******@qq.com"));
            mimeMessage.setSubject("XXX账户密码重置");
            mimeMessage.setSentDate(new Date());
            mimeMessage.setText("您在XXX使用了密码重置功能,请点击下面链接重置密码:\n"
                    + "http://localhost:/XXX/ResetPassword?id=123456789");
            mimeMessage.saveChanges();
            Transport.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(SpringBoot)