Java + 腾讯企业邮箱 + javamail + SSL 发送邮件(转载 http //www cnblogs co

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

说实话腾讯的企业邮箱真心不错!

腾讯企业邮箱官网:http://exmail.qq.com/login/

新用户注册:http://exmail.qq.com/onlinesell/intro

点击开通

你跟着步骤走就行了,没啥难的,如果你没有域名,你就去买一个呗,也花不了多少钱的。

注册成功后,是这个页面,并且会有一个弹窗告诉你一些信息

现在你点击添加成员,因为你不添加成员的话你是无法发送邮件的。

完成后是这样

然后你打开腾讯企业邮箱登录界面,输入你刚才增加的成员邮箱的:登录名 + 密码,进去后是一个类似于普通QQ邮箱的界面

到这里邮箱部分就解决了,哦还有,腾讯会自动给你发一个邮件,

点开后是这个,

以前写过普通QQ邮箱的发送代码,我从没见过SSL,所以一开始全然不懂。。,但是上网查阅得知

随着各个Mail服务器对于安全的重视,纷纷采用基于SSL的Mail登陆方式进行发送和接收电子邮件

好了现在开始写代码。

 有一个属性文件用来存储邮箱信息的

email.properties,放在src路径下面
1 e.account=你的邮箱用户名2 e.pass=你的邮箱登录密码3 e.from=你的邮箱用户名4 e.host=smtp.exmail.qq.com5 e.port=4656 e.protocol=smtp
复制代码
  1 import java.io.IOException;  2 import java.io.InputStream;  3 import java.io.UnsupportedEncodingException;  4 import java.security.GeneralSecurityException;  5 import java.util.Date;  6 import java.util.Properties;  7   8 import javax.mail.Authenticator;  9 import javax.mail.Message; 10 import javax.mail.MessagingException; 11 import javax.mail.PasswordAuthentication; 12 import javax.mail.Session; 13 import javax.mail.Transport; 14 import javax.mail.internet.InternetAddress; 15 import javax.mail.internet.MimeMessage; 16  17 import com.sun.mail.util.MailSSLSocketFactory; 18  19 public class SendEmailUtils { 20  21     private static String account;//登录用户名 22     private static String pass;        //登录密码 23     private static String from;        //发件地址 24     private static String host;        //服务器地址 25     private static String port;        //端口 26     private static String protocol; //协议 27      28     static{ 29         Properties prop = new Properties(); 30         InputStream instream = ClassLoader.getSystemResourceAsStream("email.properties"); 31         try { 32             prop.load(instream); 33         } catch (IOException e) { 34             System.out.println("加载属性文件失败"); 35         } 36         account = prop.getProperty("e.account"); 37         pass = prop.getProperty("e.pass"); 38         from = prop.getProperty("e.from"); 39         host = prop.getProperty("e.host"); 40         port = prop.getProperty("e.port"); 41         protocol = prop.getProperty("e.protocol"); 42     } 43     //用户名密码验证,需要实现抽象类Authenticator的抽象方法PasswordAuthentication 44     static class MyAuthenricator extends Authenticator{   45         String u = null 46         String p = null 47         public MyAuthenricator(String u,String p){   48             this.u=u;   49             this.p=p;   50         }   51         @Override   52         protected PasswordAuthentication getPasswordAuthentication() {   53             return new PasswordAuthentication(u,p);   54         }   55     } 56      57     private String to;    //收件人 58     private String id;    //重置密码地址标识(这句话是我的业务需要,你们可以不要) 59      60     public SendEmailUtils(String to, String id) { 61         this.to = to; 62         this.id = id; 63     } 64  65     public void send(){ 66         Properties prop = new Properties(); 67         //协议 68         prop.setProperty("mail.transport.protocol", protocol); 69         //服务器 70         prop.setProperty("mail.smtp.host", host); 71         //端口 72         prop.setProperty("mail.smtp.port", port); 73         //使用smtp身份验证 74         prop.setProperty("mail.smtp.auth", "true"); 75         //使用SSL,企业邮箱必需! 76         //开启安全协议 77         MailSSLSocketFactory sf = null; 78         try { 79             sf = new MailSSLSocketFactory(); 80             sf.setTrustAllHosts(true); 81         } catch (GeneralSecurityException e1) { 82             e1.printStackTrace(); 83         } 84         prop.put("mail.smtp.ssl.enable", "true"); 85         prop.put("mail.smtp.ssl.socketFactory", sf); 86         // 87         Session session = Session.getDefaultInstance(prop, new MyAuthenricator(account, pass)); 88         session.setDebug(true); 89         MimeMessage mimeMessage = new MimeMessage(session); 90         try { 91             mimeMessage.setFrom(new InternetAddress(from,"XXX")); 92             mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 93             mimeMessage.setSubject("XXX账户密码重置"); 94             mimeMessage.setSentDate(new Date()); 95             mimeMessage.setText("您在XXX使用了密码重置功能,请点击下面链接重置密码:\n" 96                     + "http://localhost:8080/XXX/ResetPassword?id=" 97                     + id); 98             mimeMessage.saveChanges(); 99             Transport.send(mimeMessage);100         } catch (MessagingException e) {101             e.printStackTrace();102         } catch (UnsupportedEncodingException e) {103             e.printStackTrace();104         }105     }106 107 }
复制代码

 下面是测试类

@Test    public void test4(){        SendEmailUtils s = new SendEmailUtils("收件邮箱",                “标识码”);        s.send();    }

 下面是我收到的邮件

到此就完成了
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/jdtugfcg/article/details/83895304
今日推荐