发送邮件

package com.jeemis.framework.util;


import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 邮件工具类
 */
public class MailUtil {
    /**
     * 发送邮件
     */
    public static void sendEmil(Map pmMap){
        // 参数配置
        try{
            Properties props = new Properties();
            props.setProperty("mail.transport.protocol", "smtp");
            props.setProperty("mail.smtp.host", pmMap.get("sendEmailSMTPHost"));
            props.setProperty("mail.smtp.auth", "true");
            props.setProperty("mail.smtp.port", "25");
            props.setProperty("mail.smtp.socketFactory.port", "25");
            // 根据配置创建会话对象, 用于和邮件服务器交互
            Session session = Session.getDefaultInstance(props);
            session.setDebug(true);   // 设置为debug模式, 可以查看详细的发送 log
            // 创建一封邮件
            Message message = createMimeMessage(session,pmMap.get("sendEmailAccount"), pmMap.get("receiveMailAccount"),pmMap.get("emilTitle"),pmMap.get("emilContent"));
            // 根据 Session 获取邮件传输对象
            Transport transport = session.getTransport();
            // 使用 邮箱账号 和 密码 连接邮件服务器, 这里认证的邮箱必须与 message 中的发件人邮箱一致, 否则会报错
            transport.connect(pmMap.get("sendEmailAccount"), pmMap.get("sendEmailPassword"));
            // 发送邮件
            transport.sendMessage(message, message.getAllRecipients());
            // 关闭连接
            transport.close();
        }catch (Exception e){
//            logger.debug("邮件发送失败");
        }
    }

    /**
     * 创建一封简单邮件
     */
    private static Message createMimeMessage(Session session, String sendMail, String receiveMail,String psEmilTile,String psEmilContent) throws Exception {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(sendMail));
        @SuppressWarnings("static-access")
        InternetAddress[] internetAddressTo = new InternetAddress().parse(receiveMail);
        message.setRecipients(Message.RecipientType.TO, internetAddressTo);
        // 设置邮件标题
        message.setSubject(psEmilTile);
        // 设置邮件正文
        message.setText(psEmilContent);
        message.setSentDate(new Date());
        //保存设置
        message.saveChanges();
        return message;
    }


    public static void main(String[] args) {

        String sendEmailAccount = "[email protected]";
        //如果有授权码,此处填写授权码
        String sendEmailPassword = "";
        // 发件人邮箱的 SMTP 服务器地址, 可以登录web邮箱查询
        String sendEmailSMTPHost = "smtp.163.com";
        // 收件人邮箱地址
        String receiveMailAccount = "[email protected]";
        //邮件标题
        String emilTitle = "1";
        //邮件内容
        String emilContent = "11";
        Map mMap = new HashMap<>(5);
        mMap.put("sendEmailSMTPHost",sendEmailSMTPHost);
        mMap.put("sendEmailAccount",sendEmailAccount);
        mMap.put("sendEmailPassword",sendEmailPassword);
        mMap.put("receiveMailAccount",receiveMailAccount);
        mMap.put("emilTitle",emilTitle);
        mMap.put("emilContent",emilContent);
        sendEmil(mMap);
    }
}

你可能感兴趣的:(一些有用的工具类)