邮箱发送邮件(包含附件,网易、QQ)
效果图
代码
package com.mabo.email;import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/*** @Author mabo* @Description smtp服务* 发送邮件* 支持发送者的邮箱为:网易邮箱、QQ邮箱、139邮箱* 接收邮箱不限* 推荐使用QQ的smtp服务器,速度更快* 不推荐使用139邮箱* 139邮箱,无法发送不带附件的邮件*/public class EmailTest{//QQsmtp服务器private static final String QQHost="smtp.qq.com";private static final String QQPort="587";//网易smtp服务器private static final String WangYiHost="smtp.163.com";private static final String WangYiPort="25";//139邮箱,无法发送不带附件的private static final String E139Host="smtp.139.com";private static final String E139Port="25";public static void main(String[] args) throws Exception {//发送人邮箱是qq邮箱String username="xxx35619@qq.com";//配置QQsmtp服务器成功后的令牌String password="xxxsfqvondjee";//接受邮件的地址1String receive1="xxx22@163.com";//接受邮件的地址2String receive2="xxx2129@qq.com";// //发送人是网易邮箱
// String username = "xxx0222@163.com";
// //配置网易smtp服务器成功后的令牌
// String password = "xxxHWSRPNFHI";
// // 接受邮件的地址1
// String receive1="xxx5619@qq.com";// //发送人是139邮箱
// String username = "xxx704@139.com";
// //配置网易smtp服务器成功后的令牌
// String password = "xxx63ce95564ce00";
// //接受邮件的地址1
// String receive1="xxx9@qq.com";//发送的附件List<String> list=new ArrayList<>();list.add("F:/test.doc");list.add("F:/testw副本.doc");//邮件接收方集合List<String> listReceiver=new ArrayList<>();listReceiver.add(receive1);listReceiver.add(receive2);//发送不带附件的邮件sendEmailNoFile(username,password,receive1,"标题,无附件","正文,无附件");//发送带附件,多个接收者多附件的邮件sendEmail(username,password,listReceiver,"标题,带附件","正文,带附件",list);System.out.println("发送成功");}/*** @Author mabo* @Description 邮箱发送邮件,包含附件* String sender,发送者邮箱* String password ,邮箱令牌* List<String> receives,接受者邮箱集合* String subject,标题* String content,正文* List<String> files文件名称,为null不发送附件*/public static void sendEmail(String sender,String password ,List<String> receivers,String subject, String content,List<String> files) throws Exception {String host;String port;if (sender.contains("@163.com")){host =WangYiHost;port =WangYiPort;}else if (sender.contains("@qq.com")){host =QQHost;port =QQPort;}else if (sender.contains("@139.com")){host =E139Host;port =E139Port;}else {throw new Exception("当前发送者邮箱不支持smtp服务器");}// 创建Properties 类用于记录邮箱的一些属性Properties props = new Properties();// 表示SMTP发送邮件,必须进行身份验证props.put("mail.smtp.auth", "true");//此处填写SMTP服务器props.put("mail.smtp.host", host);//端口号,QQ邮箱端口587props.put("mail.smtp.port", port);// 此处填写,写信人的账号props.put("mail.user", sender);// 此处填写16位STMP口令props.put("mail.password", password);// 构建授权信息,用于进行SMTP进行身份验证Authenticator authenticator = new Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {// 用户名、密码String userName = props.getProperty("mail.user");String password = props.getProperty("mail.password");return new PasswordAuthentication(userName, password);}};// 使用环境属性和授权信息,创建邮件会话Session session = Session.getInstance(props, authenticator);session.setDebug(true);try {List<File> attaches = new ArrayList<File>();if (files!=null){for (String file: files) {File attach = new File(file);attaches.add(attach);}}//根据session创建MimeMessageMimeMessage message = new MimeMessage(session);//发件人message.setFrom(new InternetAddress(sender));//收件人for (String receive:receivers) {message.addRecipient(Message.RecipientType.TO, new InternetAddress(receive));}//主题message.setSubject(subject);Multipart multipart = new MimeMultipart();//邮件正文BodyPart contentPart = new MimeBodyPart();contentPart.setContent(content, "text/html;charset=utf-8");multipart.addBodyPart(contentPart);//邮件附件if(attaches.size()>0) {for(File attachment : attaches) {BodyPart attachmentPart = new MimeBodyPart();DataSource source = new FileDataSource(attachment);attachmentPart.setDataHandler(new DataHandler(source));//避免中文乱码的处理attachmentPart.setFileName(MimeUtility.encodeWord(attachment.getName()));multipart.addBodyPart(attachmentPart);}}message.setContent(multipart);//保存邮件message.saveChanges();Transport.send(message);} catch (AddressException e) {throw new AddressException("未找到接收邮箱");} catch (MessagingException e) {throw new MessagingException("发送失败");} catch (UnsupportedEncodingException e) {throw new UnsupportedEncodingException("发送失败");}}/*** @Author mabo* @Description 单对单发送邮件,不包含附件* String sender,发送者邮箱* String password ,邮箱令牌* String receives,接受者邮箱* String subject,标题* String content,正文*/public static void sendEmailNoFile(String sender,String password ,String receiver,String subject, String content) throws Exception {ArrayList<String> objects = new ArrayList<>();objects.add(receiver);sendEmail(sender,password,objects,subject,content,null);}}
如何开启QQ邮箱SMTP服务
网易和QQ邮箱的开启SMTP服务设置都差不多
- 进入邮箱,点击设置
- 点击进入账户或者账户安全
- 下滑找到SMTP服务,点击开启,安装提示步骤发送短信开启
4.短信验证成功,获取口令,即为代码中的password,然后就可以开始在代码中使用了
如何开启网易邮箱SMTP服务
进入首页,点击设置,点击POP3/SMTP/IMAP
点击开启smtp服务
根据提示,发送短信验证码,最终获取令牌,即可使用smtp服务
邮箱发送邮件(包含附件,网易、QQ)相关推荐
- python3发送qq邮件_python3通过qq邮箱发送邮件以及附件
本文实例为大家分享了python3通过qq邮箱发送邮件以及附件的具体代码,供大家参考,具体内容如下ZLP免费资源网 开启qq邮箱的smtp服务ZLP免费资源网 ZLP免费资源网 代码:ZLP免费资源网 ...
- python3通过qq邮箱发送邮件以及附件
本文实例为大家分享了python3通过qq邮箱发送邮件以及附件的具体代码,供大家参考,具体内容如下 开启qq邮箱的smtp服务 代码: 在学习过程中有什么不懂得可以加我的 python学习qun,85 ...
- Python登录qq邮箱发送邮件(附件)
# -*- coding: utf-8 -*- #导入smtplib和MIMETextimport smtplib,time from email.mime.text import MIMEText ...
- CentOS 7 使用外部邮箱 发送邮件和附件—mail,mailx
1.查看软件包是否安装 [root@localhost ~]# rpm -qa|grep mail mailx-12.5-12.el7_0.x86_64 javamail-1.4.6-8.el7.no ...
- Python连接qq邮箱服务器,调用qq邮箱发送邮件实战演示,qq邮箱授权码开通方法
直接用 python 自带的 smtplib 库和 email 库就能实现邮件的发送. qq 邮箱发送的话,直接用密码是登陆不了的,需要用授权码. import smtplib from email. ...
- python 用QQ邮箱发送邮件
SMTP 是发送邮件的协议.Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮箱. Python对SMTP支持有smtplib 和email 两个模块,email负责构造 ...
- java通过网易邮箱发送邮件
java通过网易邮箱发送邮件 首先申请网易邮箱后开启POP3/SMTP服务,会获得一个授权码 通过hutool工具发送邮件 需要添加的依赖 <dependency><groupId& ...
- python 发送邮件 163_python练习-使用163邮箱发送邮件
具体代码如下> #密码等敏感信息已经用****替换 import smtplib,sys from email.mime.text import MIMEText from email.head ...
- 通过javax.mail发送邮件(示例gmail、qq)
最近学习了一下javax.mail,想通过gmail邮箱发送邮件到qq邮箱.但是由于gmail的两步验证,一直授权无法通过. 错误如下: DEBUG: JavaMail version 1.4.7 D ...
最新文章
- java设计模式----装饰器模式
- 【贪心】【P5078】Tweetuzki 爱军训
- VTK与Qt整合的示例
- 2021物理大事汇:μ子磁性异常,时间晶体现身,天体物理大丰收
- 手机怎么解决同ip多账号_抖音被限流,作品被判搬运,账号被判搬运限流,Dou?被封怎么办?...
- Python编程语言学习:python中与数字相关的函数(取整等)、案例应用之详细攻略
- Python列表排序 reverse、sort、sorted 操作方法详解
- Kotlin 知识梳理(13) 运行时的泛型
- 【转】PB实现在通知区域添加图标
- WEB API 系列(二) Filter的使用以及执行顺序
- 前端学习(3263):js中undefine
- golang new和make却别
- 自动阅读行业又出新招?离线阅读脚本套路满满
- 电子元器件商城与数据手册下载网站汇总
- WebIM 网页聊天工具
- vue-element-ui-文件上传ts版
- python 需要在什么环境下运行_运行Python需要什么环境?也需要虚拟机吗?
- 题解 CF1395A 【Boboniu Likes to Color Balls】
- IDEA高效使用技巧--->IDEA批量修改变量快捷键和全局搜索键
- Mysql中查询系统时间的方法