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

java通过SMTP发送QQ邮件(参考自龙果学院)

时间:2016-09-24 22:06:11      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:java通过smtp发送qq邮件(参考自龙果学院)

java通过SMTP发送QQ邮件编辑

标签:JAVA,其他发布于 2016-09-24 16:08:24

 

个人邮箱开通SMTP服务(进入个人邮箱,点击设置–>账户): 
技术分享

点击了账户后,往下拉可以看到SMTP服务选项,默认情况下这个选项是不开启的。点击开启腾讯会进行身份验证,身份验证通过以后,会收到一个用于使用SMTP的16位口令,验证身份的过程中把收到的口令保存下来,因为后面要使用SMTP功能必须要用到这个口令。 
技术分享

具体实现代码:

    public static void main(String args[]) {        // 邮件内容         String content = "content";        // 邮件标题         String subject = "龙果学院";        // Smtp服务器地址         String smtpHost = "smtp.qq.com";        //发件人邮箱         String emailUserName = "发件人邮箱";        //开通SMTP服务过程中收到的认证口令         String emailPassword = "开通SMTP服务过程中收到的认证口令";             Properties props = new Properties();            try {                // 开启debug调试                 props.setProperty("mail.debug", "true");                // 发送服务器需要身份验证                 props.setProperty("mail.smtp.auth", "true");                // 设置邮件服务器主机名                 props.setProperty("mail.host", smtpHost);                // 发送邮件协议名称                 props.setProperty("mail.transport.protocol", "smtp");                 MailSSLSocketFactory sf = new MailSSLSocketFactory();                 sf.setTrustAllHosts(true);                 props.put("mail.smtp.ssl.enable", "true");                 props.put("mail.smtp.ssl.socketFactory", sf);                // 设置环境信息                 Session session = Session.getInstance(props, new Authenticator() {                    // 在session中设置账户信息,Transport发送邮件时会使用                     protected PasswordAuthentication getPasswordAuthentication() {                        return new PasswordAuthentication(emailUserName, emailPassword);                     }                 });                // 创建邮件对象                 Message msg = new MimeMessage(session);                // 发件人                 msg.setFrom(new InternetAddress(emailUserName));                   // 多个收件人                 msg.setRecipients(RecipientType.TO, InternetAddress.parse("xxxx@qq.com,xxx@qq.com"));                // 抄送人                 msg.setRecipient(RecipientType.CC, new InternetAddress("xxx@qq.com"));                // 暗送人                 // msg.setRecipient(RecipientType.BCC, new InternetAddress("xxx@qq.com"));                  // 主题                 msg.setSubject(subject );                // HTML内容                 msg.setContent("<div align=\"center\">你好啊</div>", "text/html;charset=utf-8");                // 连接邮件服务器、发送邮件、关闭连接,全干了                 Transport.send(msg);             }catch( Exception e)     {         LOG.info(e.getMessage(), e);     } }123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657

在发邮件过程中有的人会发送不成功,出现如下错误: 
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

这个是jdk导致的,jdk里面有一个jce的包,安全性机制导致的访问https会报错,官网上有替代的jar包,换掉就好了

对应包的下载地址: 
http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html

下载好后,直接替换掉本地JDK中的对应的两的包就好了。


本文出自 “SMTP发送QQ邮件” 博客,请务必保留此出处http://roncoo.blog.51cto.com/7828736/1856090

java通过SMTP发送QQ邮件(参考自龙果学院)

标签:java通过smtp发送qq邮件(参考自龙果学院)

原文地址:http://roncoo.blog.51cto.com/7828736/1856090

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