1,拷贝mail.jar 和activation.jar到项目中

2,开启邮箱的 POP3/SMTP服务,以QQ邮箱为例

进去QQ邮箱-->设置-->账号-->进行设置如下图

注意:开启完之后,QQ 邮箱会生成一个授权码,在代码里连接邮箱使用这个授权码而不是原始的邮箱密码,这样可以避免使用明文密码。






完整代码示例

public class MailTool {
public static void main(String[] args) throws MessagingException, GeneralSecurityException {
Properties props = new Properties(); // 开启debug调试
props.setProperty("mail.debug", "true");
// 发送服务器需要身份验证
props.setProperty("mail.smtp.auth", "true");
// 设置邮件服务器主机名
props.setProperty("mail.host", "smtp.qq.com");
// 发送邮件协议名称
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); Message msg = new MimeMessage(session);
msg.setSubject("seenews 错误");
StringBuilder builder = new StringBuilder();
builder.append("url = " + "http://blog.csdn.net/never_cxb/article/details/50524571");
builder.append("\n页面爬虫错误");
builder.append("\n时间 " + TimeTool.getCurrentTime());
msg.setText(builder.toString());
msg.setFrom(new InternetAddress("**发送人的邮箱地址**")); Transport transport = session.getTransport();
transport.connect("smtp.qq.com", "**发送人的邮箱地址**", "**你的邮箱密码或者授权码**"); transport.sendMessage(msg, new Address[] { new InternetAddress("**接收人的邮箱地址**") });
transport.close();
}
}

开启 SSL 加密

网上搜了一下,其他比如163、新浪邮箱不需要 SSL 加密,可以放弃 QQ 邮箱。

网上还有种说法,把 smtp.qq.com 换成 smtp.exmail.qq.com也不需要 SSL加密,但是笔者没有run成功。所以还是老老实实加上 SSL 加密吧。

下面的代码开启了 SSL 加密

MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);






注意:如果发送内容有敏感词汇,QQ会自动把邮件放入垃圾箱。

下面是使用163邮箱发送邮件的代码示例:

public static void sendEmail(User user)
throws MessagingException, GeneralSecurityException {
Properties prop = new Properties();
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.host", "smtp.163.com");
prop.setProperty("mail.smtp.auth", "true");
prop.setProperty("mail.debug", "true");
Authenticator authenticator = new PopAuthenticator("邮箱登录用户名", "授权码"); //创建会话
Session session = Session.getInstance(prop,authenticator);
//填写信封写信
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("********@163.com")); //发件人
msg.setRecipient(RecipientType.TO, new InternetAddress(user.getEmail())); //收件人,可以是任意的邮箱账号,可以不是163邮箱
msg.setSubject("时尚轻纺");
msg.setText("尊敬的"+user.getUsername()+"用户,您好,********");
//验证用户名密码发送邮件
Transport transport = session.getTransport();
transport.send(msg);
}

注意:当项目发布到云服务器的时候,我是发布到腾讯云,不能使用域名  "smtp.163.com"  连接到163邮箱服务器,这时候要使用IP地址连接

修改为  prop.setProperty("mail.smtp.host", "220.181.12.13");

这样云服务器就可以连上邮箱服务器了。

参考文章:http://blog.csdn.net/lcy_dandan/article/details/46380933

关注公众号,分享干货,讨论技术

JavaMail实现邮件的发送的更多相关文章

  1. 使用JavaMail发送邮件-从FTP读取图片并添加到邮件正文发送

    业务分析: 最近工作需要,需要从FTP读取图片内容,添加到邮件正文发送.发送邮件正文,添加附件采用Spring的MimeMessageHelper对象来完成,添加图片也将采用MimeMessageHe ...

  2. Java 基于JavaMail的邮件发送

    http://blog.csdn.net/xietansheng/article/details/51673073 http://blog.csdn.net/xietansheng/article/d ...

  3. Spring MVC+javamail实现邮件发送

    Spring MVC+javamail实现邮件发送 开启邮箱的POP3/SMTP服务(这里以QQ邮箱举例) 设置 --> 账户 -- > 开启POP3/STMP服务,然后得到一个授权码. ...

  4. Springboot+Javamail实现邮件发送

    Springboot+Javamail实现邮件发送 使用的是spring-context-support-5.2.6.RELEASE.jar里的javamail javamail 官方文档:javam ...

  5. 使用JavaMail创建邮件发送邮件

    一.RFC882文档简单说明 RFC882文档规定了如何编写一封简单的邮件(纯文本邮件),一封简单的邮件包含邮件头和邮件体两个部分,邮件头和邮件体之间使用空行分隔. 邮件头包含的内容有: from字段 ...

  6. JavaWeb学习总结(五十二)——使用JavaMail创建邮件和发送邮件

    一.RFC882文档简单说明 RFC882文档规定了如何编写一封简单的邮件(纯文本邮件),一封简单的邮件包含邮件头和邮件体两个部分,邮件头和邮件体之间使用空行分隔. 邮件头包含的内容有: from字段 ...

  7. (转载)JavaWeb学习总结(五十二)——使用JavaMail创建邮件和发送邮件

    博客源地址:http://www.cnblogs.com/xdp-gacl/p/4216311.html 一.RFC882文档简单说明 RFC882文档规定了如何编写一封简单的邮件(纯文本邮件),一封 ...

  8. (转载)JavaWeb学习总结(五十一)——邮件的发送与接收原理

    博客源地址:http://www.cnblogs.com/xdp-gacl/p/4209586.html 一. 邮件开发涉及到的一些基本概念 1.1.邮件服务器和电子邮箱 要在Internet上提供电 ...

  9. JavaWeb学习总结(五十一)——邮件的发送与接收原理

    一. 邮件开发涉及到的一些基本概念 1.1.邮件服务器和电子邮箱 要在Internet上提供电子邮件功能,必须有专门的电子邮件服务器.例如现在Internet很多提供邮件服务的厂商:sina.sohu ...

随机推荐

  1. 由一个hash字符串生成多个子hash字符串

    通过存储一个head hash,然后把子hash放到网络中 当然,也可以像默克尔树那样的,生成多级的子hash ,可以通过规则配置不同的hash 生成方式.倒置的默克尔树 我有一个文件,然后我把她分隔 ...

  2. OSI七层协议模型及OSI参考模型中的数据封装过程

    转载自:http://blog.csdn.net/qq_14935437/article/details/71081546 OSI模型,即开放式通信系统互联参考模型(Open System Inter ...

  3. nodejs笔记--express篇(五)

    创建一个express + ejs的项目 express -e testEjsWebApp cd testEjsWebApp npm install http://localhost:3000 Usa ...

  4. UBUNTU如何安装tar.gz版的flash

    adobe flash player的官方下载页面为:https://get.adobe.com/cn/flashplayer/ 不过近期通过APT方式以及ubuntu的软件中心都安装不了flashp ...

  5. <Effective C++>读书摘要--Designs and Declarations<二>

    <Item 20> Prefer pass-by-reference-to-const to pass-by-value 1.By default, C++ passes objects ...

  6. 3dContactPointAnnotationTool开发日志(十二)

      因为ReferenceImage的锚点是固定的左下角,缩放时controller面板也会跟着动.为了使Scale的时候controller上的slider不会远离指针,于是把controller固 ...

  7. MySQL中使用trim()删除两侧字符

    在某些情况下由于程序没处理好,导致数据表中有些字段的值会有空白字符出现,如 这样如果在严格比对name时会匹配不到.trim()函数可以解决这样的问题 作为trim()函数的子集,ltrim()函数是 ...

  8. thinkPHP框架单一入口文件解析

    一.index.php  (可参考ThinkPHP学习手册http://document.thinkphp.cn/manual_3_2.html#entrance_file) index.php单入口 ...

  9. get computer system mac info in javascript

    get computer system mac info in javascript Q: how to using js get computer system mac information? A ...

  10. 第33天:封装自己的class类

    封装自己的class类,实现浏览器兼容. <!DOCTYPE html> <html lang="en"> <head> <meta ch ...