Java + 腾讯企业邮箱 + javamail + SSL 发送邮件(转载:http://www.cnblogs.com/LUA123/p/5575134.html)

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

腾讯企业邮箱官网: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.com
5 e.port=465
6 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();
    }

 

 下面是我收到的邮件

到此就完成了
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当你遇到 "Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: javax/mail/Authenticator" 这个错误时,它通常发生在Java中使用JavaMail API尝试发送电子邮件的时候。这个错误表明Java环境无法找到javax.mail.Authenticator类,这通常是由于缺少JavaMail API相关的库(如JavaMail API jar文件)或者路径设置不正确。 以下是解决这个问题的一些步骤: 1. **检查依赖**:确认你的项目构建路径是否包含了JavaMail API的jar包。如果没有,你需要将其添加到项目的构建路径或Maven/Gradle等构建工具的依赖中。对于Maven,可以在pom.xml文件里加入 `<dependency>` 标签,比如: ```xml <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.6.0</version> <!-- 版本号可能根据实际需求调整 --> </dependency> ``` 2. **设置系统路径**:如果你的项目是在IDEA或者Eclipse这样的环境中,确保JRE Libraries或Classpath配置已包含该库。 3. **检查环境变量**:如果使用的是JDK,确保JAVA_HOME环境变量指向了正确安装的JDK版本,并且JAVAMAIL_URL、JAVAMAIL_DEBUG这类相关的系统属性已经设置。 4. **清理并重建项目**:有时,仅仅重新导入或清除项目缓存,然后重新编译和运行也可能解决问题。 5. **更新或替换库**:如果使用的库版本过旧,尝试升级到最新版本看是否能解决这个问题。 相关问题-- 1. JavaMail API是什么? 2. 如何在Java中手动添加第三方库到项目依赖? 3. 如何在Java IDE中设置classpath或JRE Libraries?

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值