专属我的味道

Java SMTP工具类

maven导入

com.sun.mail:javax.mail:1.6.1

QQ截图20200709222700.png
项目目录下创建config.prop,来配置SMTP

#STMP配置
mailname=888888@qq.com
mailpwd=password
smtphost=smtp.qq.com
smtpport=465

新建SendMailUtils类

package cn.myflavor.www.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMailUtils {
    private static String from = "";
    private static String user = "";
    private static String password = "";
    private static String mailadmin = "";
    private static String smtphost = "";
    private static String smtpport = "";

    /*
     * 读取属性文件的内容,并为上面上个属性赋初始值
     */
    static {
        Properties prop = new Properties();
        InputStream is = SendMailUtils.class.getClassLoader().getResourceAsStream("config.prop");
        try {
            prop.load(is);
            user=prop.getProperty("mailname");
            password=prop.getProperty("mailpwd");
            from=user;
            mailadmin=prop.getProperty("mailadmin");
            smtphost=prop.getProperty("smtphost");
            smtpport=prop.getProperty("smtpport");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void sendMail(String toEmailAddres,String subject, String content) throws Exception {

        String auth = "true";                     //是否需要进行身份验证
        String protocol = "smtp";                  //传输协议
        String personalName = "发件人昵称";         //发件人邮箱别名
        String mailDebug = "true";                //是否开启debug
        String contentType = null;                 //邮件正文类型
        Properties props = new Properties();
        props.put("mail.smtp.host", smtphost);
        props.put("mail.smtp.auth", auth == null ? "true" : auth);
        props.put("mail.transport.protocol", protocol == null ? "smtp" : protocol);
        props.put("mail.smtp.port", smtpport == null ? "25" : smtpport);
        props.put("mail.debug", mailDebug == null ? "false" : mailDebug);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        Session mailSession = Session.getInstance(props);
        // 设置session,和邮件服务器进行通讯。
        MimeMessage message = new MimeMessage(mailSession);
        // 设置邮件主题
        message.setSubject(subject);
        // 设置邮件正文
        message.setContent(content, contentType == null ? "text/html;charset=UTF-8" : contentType);
        // 设置邮件发送日期
        message.setSentDate(new Date());
        InternetAddress address = new InternetAddress(from, personalName);
        // 设置邮件发送者的地址
        message.setFrom(address);
        // 设置邮件接收方的地址
        message.setRecipients(Message.RecipientType.TO, toEmailAddres);
        Transport transport = null;
        transport = mailSession.getTransport();
        message.saveChanges();
        transport.connect(smtphost, user, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

}
含原创标签的文章未经允许不得转载 | 当前页面:专属我的味道 » Java SMTP工具类

评论

文章评论已关闭!