Newer
Older
huludao / src / main / java / com / newfiber / led / business / controller / JavaMailUtils.java
package com.newfiber.led.business.controller;


import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import java.util.Properties;

public class JavaMailUtils {

    public  static Session createSession(){
        String smtp = "smtp.126.com";

        //创建登录名和密码
        String userName = "whxfhgd@126.com";
        String passWord = "GUTNCUHWHRWVBQIA";

        //授权码  GUTNCUHWHRWVBQIA

        //STMP服务器的连接信息
        // 连接到SMTP服务器25端口:
        Properties props = new Properties();

        props.put("mail.smtp.host", smtp); // SMTP主机名
        props.put("mail.smtp.port", "25"); // 主机端口号
        props.put("mail.smtp.auth", "true"); // 是否需要用户认证
        props.put("mail.smtp.starttls.enable", "true"); // 启用TLS加密


        //创建Session
        //参数1:STMP服务器偶的连接信息
        //参数2:用户认证对象(Authenticator接口的匿名实现类)

        // 获取Session实例:
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {

                return new PasswordAuthentication(userName, passWord);
            }
        });
        // 设置debug模式便于调试:
        session.setDebug(true);
        return session;
    }
}