JavaEmailUil

Posted 翔运

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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","[email protected]","title","<b>content</b>...",new Date());
        
    }

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

以上是关于JavaEmailUil的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数