• JavaEmailUil


     1 import java.io.UnsupportedEncodingException;
     2 import java.util.Date;
     3 import java.util.Properties;
     4 import javax.mail.MessagingException;
     5 import javax.mail.Session;
     6 import javax.mail.Transport;
     7 import javax.mail.internet.InternetAddress;
     8 import javax.mail.internet.MimeMessage;
     9 
    10 public class EmailUtil {
    11     /**
    12      * 发送邮件需要开通IMAP,如果不是qq邮箱要更改smtp地址和ssl端口
    13      * log 查看详细的发送log
    14      * sendMail 发件人
    15      * receiveMail 收件人
    16      * sendMailPassWord 发件人密码
    17      * title 邮件标题
    18      * content 邮件内容
    19      * content中可以插入html标签
    20      * Date 发送时间
    21      * @author chenyunzheng
    22      * @return null
    23      * @throws MessagingException
    24      * @throws UnsupportedEncodingException
    25      */
    26     public void sendEmail(boolean log,String sendMail,String sendMailPassWord,String receiveMail,String title,String content,Date date){
    27         Properties props = new Properties();
    28         props.setProperty("mail.transport.protocol", "smtp");//使用协议
    29         props.setProperty("mail.smtp.host", "smtp.qq.com");//发件邮箱smtp服务器地址
    30         props.setProperty("mail.smtp.auth", "true");//请求认证
    31         String smtpPort = "465";//ssl端口
    32         props.setProperty("mail.smtp.port", smtpPort);
    33         props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    34         props.setProperty("mail.smtp.socketFactory.fallback", "false");
    35         props.setProperty("mail.smtp.socketFactory.port", smtpPort);
    36         //创建会话对象
    37         Session session = Session.getInstance(props);
    38         session.setDebug(log);
    39         try {
    40         MimeMessage message = new MimeMessage(session);
    41         message.setFrom(new InternetAddress(sendMail,"xxx公司","utf-8"));
    42         //可以增加多个收件人  抄送 CC,密送 BCC
    43         message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress(receiveMail,"用户  123","utf-8"));
    44         message.setSubject(title,"utf-8");
    45         message.setContent(content, "text/html;charset=UTF-8");
    46         message.setSentDate(date);
    47         message.saveChanges();
    48         
    49         Transport transport = session.getTransport();
    50         transport.connect(sendMail,sendMailPassWord);//认证邮箱与发件人一致
    51         transport.sendMessage(message, message.getAllRecipients());
    52         transport.close();
    53         } catch (UnsupportedEncodingException e) {
    54             e.printStackTrace();
    55         } catch (MessagingException e) {
    56             e.printStackTrace();
    57         }
    58    
    59     }
    60 }

    使用工具类

        public static void main(String[] args) {
            EmailUtil mail = new EmailUtil();
            mail.sendEmail(true,"****@chenyunzheng.me","password","service@chenyunzheng.me","title","<style type='text/css'>b{color: red;}</style> <b>123</b>456",new Date());
            
        }

    还需导入mail.jar,如果是maven需要添加maven依赖

    点击参考其它博客

  • 相关阅读:
    [CF707D]Persistent Bookcase_主席树_bitset
    [CF798D]Mike and distribution_贪心
    [LuoguP2164][SHOI2007]交通网络_拓扑排序_概率期望
    [LuoguP3064][USACO12DEC]伊斯坦布尔的帮派Gangs of Istanbull(加强版)_线段树_贪心
    [CF306C]White, Black and White Again_排列组合
    [LuoguP2167][SDOI2009]Bill的挑战_容斥原理/状压dp
    [LuoguP2163][SHOI2007]园丁的烦恼_CDQ分治
    正则字符串插入字符
    [react]
    react 预览pdf 转换
  • 原文地址:https://www.cnblogs.com/chengyungzheng/p/7052972.html
Copyright © 2020-2023  润新知