JavaMail发送邮件、带附件邮件(完整版)
工程目录如下:
1、准备javaMail需要的两个Jar包:mail.jar、activation.jar,然后add to build path
2、QQ邮箱开启SMTP服务,开启后,它会给你一串授权码
完整代码如下所示:
- import java.security.GeneralSecurityException;
- import java.util.Properties;
- import javax.mail.Authenticator;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import com.sun.mail.util.MailSSLSocketFactory;
- public class HelloMail {
- public static void main(String[] args) throws GeneralSecurityException {
- // 收件人电子邮箱
- String to = "xxxxx@qq.com";
- // 发件人电子邮箱
- String from = "xxxxx@qq.com";
- // 获取系统属性
- Properties properties = new Properties();
- //发送邮件协议
- properties.setProperty("mail.transport.protocol", "SMTP");
- // 设置邮件服务器
- properties.setProperty("mail.host", "smtp.qq.com");
- // 设置邮件服务器端口
- properties.setProperty("mail.smtp.port", "");
- //开启SSL加密:QQ邮箱需要
- MailSSLSocketFactory sf = new MailSSLSocketFactory();
- sf.setTrustAllHosts(true);
- properties.put("mail.smtp.ssl.enable", "true");
- properties.put("mail.smtp.ssl.socketFactory", sf);
- // 设置邮件服务器是否需要登录认证
- properties.setProperty("mail.smtp.auth", "true");
- // 验证账号及密码,密码需要是第三方授权码
- Authenticator auth = new Authenticator() {
- public PasswordAuthentication getPasswordAuthentication(){
- return new PasswordAuthentication("xxxxx@qq.com", "这里添授权码");
- }
- };
- // 获取默认session对象
- Session session = Session.getInstance(properties,auth);
- try{
- // 创建默认的 MimeMessage 对象
- MimeMessage message = new MimeMessage(session);
- // Set From: 头部头字段
- message.setFrom(new InternetAddress(from));
- // Set To: 邮件接收人
- message.addRecipient(Message.RecipientType.TO,
- new InternetAddress(to));
- // Set Subject: 头部头字段
- message.setSubject("This is the Subject Line!");
- // 设置消息体
- message.setText("This is actual message");
- // 发送消息
- Transport.send(message);
- System.out.println("Sent message successfully....");
- }catch (MessagingException mex) {
- mex.printStackTrace();
- }
- }
- }
运行成功,然后qq邮箱就收到了邮件
带附件发送:
- import java.io.File;
- import java.security.GeneralSecurityException;
- import java.util.Properties;
- import javax.activation.DataHandler;
- import javax.activation.DataSource;
- import javax.activation.FileDataSource;
- import javax.mail.Authenticator;
- import javax.mail.BodyPart;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Multipart;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
- import com.sun.mail.util.MailSSLSocketFactory;
- /*
- * 带附件的邮件
- */
- public class MailAttach {
- public static void main(String[] args) throws GeneralSecurityException {
- // 收件人电子邮箱
- String to = "XXXXXXX"; //也可以的
- // 发件人电子邮箱
- String from = "XXXXXXX@qq.com";
- // 获取系统属性
- Properties properties = new Properties();
- //发送邮件协议
- properties.setProperty("mail.transport.protocol", "SMTP");
- // 设置邮件服务器
- properties.setProperty("mail.host", "smtp.qq.com");
- // 设置邮件服务器端口
- properties.setProperty("mail.smtp.port", "");
- //开启SSL加密:QQ邮箱需要
- MailSSLSocketFactory sf = new MailSSLSocketFactory();
- sf.setTrustAllHosts(true);
- properties.put("mail.smtp.ssl.enable", "true");
- properties.put("mail.smtp.ssl.socketFactory", sf);
- // 设置邮件服务器是否需要登录认证
- properties.setProperty("mail.smtp.auth", "true");
- // 验证账号及密码,密码需要是第三方授权码
- Authenticator auth = new Authenticator() {
- public PasswordAuthentication getPasswordAuthentication(){
- return new PasswordAuthentication("XXXXXXX@qq.com", "授权码");
- }
- };
- // 获取默认session对象
- Session session = Session.getInstance(properties,auth);
- try{
- // 创建默认的 MimeMessage 对象
- MimeMessage message = new MimeMessage(session);
- // Set From: 头部头字段
- message.setFrom(new InternetAddress(from));
- // Set To: 邮件接收人
- message.addRecipient(Message.RecipientType.TO,
- new InternetAddress(to));
- // Set Subject: 主题名称
- message.setSubject("This is the Subject Line!");
- // 创建消息部分
- BodyPart messageBodyPart = new MimeBodyPart();
- // 消息内容
- messageBodyPart.setText("TEST/TEST");
- // 创建多重消息
- Multipart multipart = new MimeMultipart();
- // 设置文本消息部分
- multipart.addBodyPart(messageBodyPart);
- // 附件部分
- messageBodyPart = new MimeBodyPart();
- //把文件,添加到附件1中
- //数据源
- DataSource source = new FileDataSource(new File("附件路径,比如:D:/test.zip"));
- //设置第一个附件的数据
- messageBodyPart.setDataHandler(new DataHandler(source));
- //设置附件的文件名
- messageBodyPart.setFileName("file1.zip");
- multipart.addBodyPart(messageBodyPart);
- message.setContent(multipart);
- // 发送消息
- Transport.send(message);
- System.out.println("Sent message successfully....");
- }catch (MessagingException mex) {
- mex.printStackTrace();
- }
- }
- }
JavaMail发送邮件、带附件邮件(完整版)的更多相关文章
- JavaMail实现带附件的收发邮件
一.前言 参考博客: http://blog.csdn.net/xietansheng/article/details/51722660 http://www.cnblogs.com/HigginCu ...
- 利用springframework+javax.mail发邮件(普通邮件、带附件邮件、HTML格式邮件)
Spring提供了发送电子邮件的支持,可以发送普通邮件.带附件邮件.HTML格式邮件,甚至还可以使用Velocity模板定制化邮件内容. 一.引入相关的库 1 2 3 4 5 6 7 8 9 10 1 ...
- ORACLE发送带附件邮件的二三事之一
在oracle使用过程中,我们可以通过pl/sql生成数据文件,也可以通过spool on spool off生成,但某些环境下,我们需要通过存储过程处理数据,数据处理完,需要自动生成数据文件,手工导 ...
- 使用JavaMail发送带附件的邮件
所需jar包 链接:http://pan.baidu.com/s/1dFo4cDz 密码:akap 工具类: package com.javamail.utils; import java.util. ...
- Java发送邮件(带附件)
实现java发送邮件的过程大体有以下几步: 准备一个properties文件,该文件中存放SMTP服务器地址等参数. 利用properties创建一个Session对象 利用Session创建Mess ...
- centos 使用mutt发送邮件带附件
1.安装mutt工具 yum install -y mutt 2.使用mutt发邮件并带附件echo "统计日志" | /usr/bin/mutt -s "统计日志&qu ...
- 【Mail】JavaMail发送带附件的邮件(二)
上一篇讲了使用JavaMail发送普通邮件([Mail]JavaMail介绍及发送邮件(一)),本例讲发送复杂的邮件(带有附件的邮件) 生成一封复杂的邮件 新建一个JavaWeb的Maven工程,引入 ...
- (转)用javamail发送带附件的邮件
本文转载自:http://redleaf.iteye.com/blog/78217 mail.java 代码 package mail; import java.util.* ; import jav ...
- 使用JavaMail发送邮件和接受邮件
转载:http://blog.csdn.net/zdp072/article/details/30977213 一. 为什么要学习JavaMail 为什么要学习JavaMail开发? 现在很多WEB应 ...
随机推荐
- Python 数字(Number)
Python 数字(Number) Python 数字数据类型用于存储数值. 数据类型是不允许改变的,这就意味着如果改变数字数据类型的值,将重新分配内存空间. 以下实例在变量赋值时 Number 对象 ...
- 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 ...
- You Don't Know JS: Scope & Closures (附加:Lexical/dynamic作用域)(附加:Lexical-this)
JavaScript只有Lexical Scope 模式 Lexical Scope就是在写代码的时候,定义函数的时候创建的作用域! 而动态作用域是在runtime时,函数被调用的地方的作用域! 实际 ...
- Confluence 6 创建你的个人空间
作为一个项目中的新手,你可能希望将一些工作保存为你自己可见,直到你准备将你的工作分享出去.同时你可能会收到任务指挥中心发送的只针对你的任务,你也希望这些任务能存储在一个安全的地方. 针对类似这样任务需 ...
- day1-6 字符串、列表、元组、字典、类型转换
day1 1.python历史. 宏观上:python2 与 python3 区别: python2 源码不标准,混乱,重复代码太多, python3 统一 标准,去除重复代码. 2.python的环 ...
- 完整的Django入门指南学习笔记1
转自[https://blog.csdn.net/qq_35554125/article/details/79462885] part 1: 前沿 教程材料一共会被分为七个部分. 此教程将从安装.开发 ...
- Music in Car CodeForces - 746F (贪心,模拟)
大意: n首歌, 第$i$首歌时间$t_i$, 播放完获得贡献$a_i$, 最多播放k分钟, 可以任选一首歌开始按顺序播放, 最多选w首歌半曲播放(花费时间上取整), 求贡献最大值. 挺简单的一个题, ...
- layui 表格图片放大
1. 表格塞图片 ,{title: '图片', width:120, templet: function(d) { return '<div onclick="show_img(thi ...
- D - Power Tower欧拉降幂公式
题意:给你一个数组a,q次查询,每次l,r,要求 \(a_{l}^{a_{l+1}}^{a_{l+2}}...{a_r}\) 题解:由欧拉降幂可知,最多log次eu(m)肯定变1,那么直接暴力即可,还 ...
- flexbox与grid layout的区别
flexbox是一种针对一维的局部布局,以轴为核心的弹性布局. grid layout是二维的更加全面的网格布局,