工程目录如下:

1、准备javaMail需要的两个Jar包:mail.jar、activation.jar,然后add to build path

2、QQ邮箱开启SMTP服务,开启后,它会给你一串授权码

完整代码如下所示:

  1. import java.security.GeneralSecurityException;
  2. import java.util.Properties;
  3.  
  4. import javax.mail.Authenticator;
  5. import javax.mail.Message;
  6. import javax.mail.MessagingException;
  7. import javax.mail.PasswordAuthentication;
  8. import javax.mail.Session;
  9. import javax.mail.Transport;
  10. import javax.mail.internet.InternetAddress;
  11. import javax.mail.internet.MimeMessage;
  12.  
  13. import com.sun.mail.util.MailSSLSocketFactory;
  14.  
  15. public class HelloMail {
  16.  
  17. public static void main(String[] args) throws GeneralSecurityException {
  18.  
  19. // 收件人电子邮箱
  20. String to = "xxxxx@qq.com";
  21.  
  22. // 发件人电子邮箱
  23. String from = "xxxxx@qq.com";
  24.  
  25. // 获取系统属性
  26. Properties properties = new Properties();
  27.  
  28. //发送邮件协议
  29. properties.setProperty("mail.transport.protocol", "SMTP");
  30.  
  31. // 设置邮件服务器
  32. properties.setProperty("mail.host", "smtp.qq.com");
  33.  
  34. // 设置邮件服务器端口
  35. properties.setProperty("mail.smtp.port", "");
  36.  
  37. //开启SSL加密:QQ邮箱需要
  38. MailSSLSocketFactory sf = new MailSSLSocketFactory();
  39. sf.setTrustAllHosts(true);
  40. properties.put("mail.smtp.ssl.enable", "true");
  41. properties.put("mail.smtp.ssl.socketFactory", sf);
  42.  
  43. // 设置邮件服务器是否需要登录认证
  44. properties.setProperty("mail.smtp.auth", "true");
  45.  
  46. // 验证账号及密码,密码需要是第三方授权码
  47. Authenticator auth = new Authenticator() {
  48. public PasswordAuthentication getPasswordAuthentication(){
  49. return new PasswordAuthentication("xxxxx@qq.com", "这里添授权码");
  50. }
  51. };
  52.  
  53. // 获取默认session对象
  54. Session session = Session.getInstance(properties,auth);
  55.  
  56. try{
  57. // 创建默认的 MimeMessage 对象
  58. MimeMessage message = new MimeMessage(session);
  59.  
  60. // Set From: 头部头字段
  61. message.setFrom(new InternetAddress(from));
  62.  
  63. // Set To: 邮件接收人
  64. message.addRecipient(Message.RecipientType.TO,
  65. new InternetAddress(to));
  66.  
  67. // Set Subject: 头部头字段
  68. message.setSubject("This is the Subject Line!");
  69.  
  70. // 设置消息体
  71. message.setText("This is actual message");
  72.  
  73. // 发送消息
  74. Transport.send(message);
  75. System.out.println("Sent message successfully....");
  76. }catch (MessagingException mex) {
  77. mex.printStackTrace();
  78. }
  79.  
  80. }
  81.  
  82. }

运行成功,然后qq邮箱就收到了邮件

带附件发送:

  1. import java.io.File;
  2. import java.security.GeneralSecurityException;
  3. import java.util.Properties;
  4.  
  5. import javax.activation.DataHandler;
  6. import javax.activation.DataSource;
  7. import javax.activation.FileDataSource;
  8. import javax.mail.Authenticator;
  9. import javax.mail.BodyPart;
  10. import javax.mail.Message;
  11. import javax.mail.MessagingException;
  12. import javax.mail.Multipart;
  13. import javax.mail.PasswordAuthentication;
  14. import javax.mail.Session;
  15. import javax.mail.Transport;
  16. import javax.mail.internet.InternetAddress;
  17. import javax.mail.internet.MimeBodyPart;
  18. import javax.mail.internet.MimeMessage;
  19. import javax.mail.internet.MimeMultipart;
  20.  
  21. import com.sun.mail.util.MailSSLSocketFactory;
  22.  
  23. /*
  24. * 带附件的邮件
  25. */
  26. public class MailAttach {
  27.  
  28. public static void main(String[] args) throws GeneralSecurityException {
  29.  
  30. // 收件人电子邮箱
  31. String to = "XXXXXXX"; //也可以的
  32.  
  33. // 发件人电子邮箱
  34. String from = "XXXXXXX@qq.com";
  35.  
  36. // 获取系统属性
  37. Properties properties = new Properties();
  38.  
  39. //发送邮件协议
  40. properties.setProperty("mail.transport.protocol", "SMTP");
  41.  
  42. // 设置邮件服务器
  43. properties.setProperty("mail.host", "smtp.qq.com");
  44.  
  45. // 设置邮件服务器端口
  46. properties.setProperty("mail.smtp.port", "");
  47.  
  48. //开启SSL加密:QQ邮箱需要
  49. MailSSLSocketFactory sf = new MailSSLSocketFactory();
  50. sf.setTrustAllHosts(true);
  51. properties.put("mail.smtp.ssl.enable", "true");
  52. properties.put("mail.smtp.ssl.socketFactory", sf);
  53.  
  54. // 设置邮件服务器是否需要登录认证
  55. properties.setProperty("mail.smtp.auth", "true");
  56.  
  57. // 验证账号及密码,密码需要是第三方授权码
  58. Authenticator auth = new Authenticator() {
  59. public PasswordAuthentication getPasswordAuthentication(){
  60. return new PasswordAuthentication("XXXXXXX@qq.com", "授权码");
  61. }
  62. };
  63.  
  64. // 获取默认session对象
  65. Session session = Session.getInstance(properties,auth);
  66.  
  67. try{
  68. // 创建默认的 MimeMessage 对象
  69. MimeMessage message = new MimeMessage(session);
  70.  
  71. // Set From: 头部头字段
  72. message.setFrom(new InternetAddress(from));
  73.  
  74. // Set To: 邮件接收人
  75. message.addRecipient(Message.RecipientType.TO,
  76. new InternetAddress(to));
  77.  
  78. // Set Subject: 主题名称
  79. message.setSubject("This is the Subject Line!");
  80.  
  81. // 创建消息部分
  82. BodyPart messageBodyPart = new MimeBodyPart();
  83.  
  84. // 消息内容
  85. messageBodyPart.setText("TEST/TEST");
  86.  
  87. // 创建多重消息
  88. Multipart multipart = new MimeMultipart();
  89.  
  90. // 设置文本消息部分
  91. multipart.addBodyPart(messageBodyPart);
  92.  
  93. // 附件部分
  94. messageBodyPart = new MimeBodyPart();
  95.  
  96. //把文件,添加到附件1中
  97. //数据源
  98. DataSource source = new FileDataSource(new File("附件路径,比如:D:/test.zip"));
  99. //设置第一个附件的数据
  100. messageBodyPart.setDataHandler(new DataHandler(source));
  101. //设置附件的文件名
  102. messageBodyPart.setFileName("file1.zip");
  103. multipart.addBodyPart(messageBodyPart);
  104. message.setContent(multipart);
  105.  
  106. // 发送消息
  107. Transport.send(message);
  108. System.out.println("Sent message successfully....");
  109. }catch (MessagingException mex) {
  110. mex.printStackTrace();
  111. }
  112.  
  113. }
  114.  
  115. }

JavaMail发送邮件、带附件邮件(完整版)的更多相关文章

  1. JavaMail实现带附件的收发邮件

    一.前言 参考博客: http://blog.csdn.net/xietansheng/article/details/51722660 http://www.cnblogs.com/HigginCu ...

  2. 利用springframework+javax.mail发邮件(普通邮件、带附件邮件、HTML格式邮件)

    Spring提供了发送电子邮件的支持,可以发送普通邮件.带附件邮件.HTML格式邮件,甚至还可以使用Velocity模板定制化邮件内容. 一.引入相关的库 1 2 3 4 5 6 7 8 9 10 1 ...

  3. ORACLE发送带附件邮件的二三事之一

    在oracle使用过程中,我们可以通过pl/sql生成数据文件,也可以通过spool on spool off生成,但某些环境下,我们需要通过存储过程处理数据,数据处理完,需要自动生成数据文件,手工导 ...

  4. 使用JavaMail发送带附件的邮件

    所需jar包 链接:http://pan.baidu.com/s/1dFo4cDz 密码:akap 工具类: package com.javamail.utils; import java.util. ...

  5. Java发送邮件(带附件)

    实现java发送邮件的过程大体有以下几步: 准备一个properties文件,该文件中存放SMTP服务器地址等参数. 利用properties创建一个Session对象 利用Session创建Mess ...

  6. centos 使用mutt发送邮件带附件

    1.安装mutt工具 yum install -y mutt 2.使用mutt发邮件并带附件echo "统计日志" | /usr/bin/mutt -s "统计日志&qu ...

  7. 【Mail】JavaMail发送带附件的邮件(二)

    上一篇讲了使用JavaMail发送普通邮件([Mail]JavaMail介绍及发送邮件(一)),本例讲发送复杂的邮件(带有附件的邮件) 生成一封复杂的邮件 新建一个JavaWeb的Maven工程,引入 ...

  8. (转)用javamail发送带附件的邮件

    本文转载自:http://redleaf.iteye.com/blog/78217 mail.java 代码 package mail; import java.util.* ; import jav ...

  9. 使用JavaMail发送邮件和接受邮件

    转载:http://blog.csdn.net/zdp072/article/details/30977213 一. 为什么要学习JavaMail 为什么要学习JavaMail开发? 现在很多WEB应 ...

随机推荐

  1. Python 数字(Number)

    Python 数字(Number) Python 数字数据类型用于存储数值. 数据类型是不允许改变的,这就意味着如果改变数字数据类型的值,将重新分配内存空间. 以下实例在变量赋值时 Number 对象 ...

  2. R quantile函数 | cut函数 | sample函数 | all函数 | scale函数 | do.call函数

    取出一个数字序列中的百分位数 1. 求某一个百分比 x<-rnorm(200) quantile(x,0.9) 2. 求一系列的百分比 quantile(x,c(0.1,0.9)) quanti ...

  3. You Don't Know JS: Scope & Closures (附加:Lexical/dynamic作用域)(附加:Lexical-this)

    JavaScript只有Lexical Scope 模式 Lexical Scope就是在写代码的时候,定义函数的时候创建的作用域! 而动态作用域是在runtime时,函数被调用的地方的作用域! 实际 ...

  4. Confluence 6 创建你的个人空间

    作为一个项目中的新手,你可能希望将一些工作保存为你自己可见,直到你准备将你的工作分享出去.同时你可能会收到任务指挥中心发送的只针对你的任务,你也希望这些任务能存储在一个安全的地方. 针对类似这样任务需 ...

  5. day1-6 字符串、列表、元组、字典、类型转换

    day1 1.python历史. 宏观上:python2 与 python3 区别: python2 源码不标准,混乱,重复代码太多, python3 统一 标准,去除重复代码. 2.python的环 ...

  6. 完整的Django入门指南学习笔记1

    转自[https://blog.csdn.net/qq_35554125/article/details/79462885] part 1: 前沿 教程材料一共会被分为七个部分. 此教程将从安装.开发 ...

  7. Music in Car CodeForces - 746F (贪心,模拟)

    大意: n首歌, 第$i$首歌时间$t_i$, 播放完获得贡献$a_i$, 最多播放k分钟, 可以任选一首歌开始按顺序播放, 最多选w首歌半曲播放(花费时间上取整), 求贡献最大值. 挺简单的一个题, ...

  8. layui 表格图片放大

    1. 表格塞图片 ,{title: '图片', width:120, templet: function(d) { return '<div onclick="show_img(thi ...

  9. D - Power Tower欧拉降幂公式

    题意:给你一个数组a,q次查询,每次l,r,要求 \(a_{l}^{a_{l+1}}^{a_{l+2}}...{a_r}\) 题解:由欧拉降幂可知,最多log次eu(m)肯定变1,那么直接暴力即可,还 ...

  10. flexbox与grid layout的区别

    flexbox是一种针对一维的局部布局,以轴为核心的弹性布局. grid layout是二维的更加全面的网格布局,