码迷,mamicode.com
首页 > 编程语言 > 详细

JavaEmailUil

时间:2017-06-20 10:52:47      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:bool   instance   dem   set   from   message   发件人   except   dma   

 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","<b>content</b>...",new Date());
        
    }

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

JavaEmailUil

标签:bool   instance   dem   set   from   message   发件人   except   dma   

原文地址:http://www.cnblogs.com/chengyungzheng/p/7052972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!