效果图

代码

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服务设置都差不多

  1. 进入邮箱,点击设置
  2. 点击进入账户或者账户安全
  3. 下滑找到SMTP服务,点击开启,安装提示步骤发送短信开启

4.短信验证成功,获取口令,即为代码中的password,然后就可以开始在代码中使用了

如何开启网易邮箱SMTP服务

  1. 进入首页,点击设置,点击POP3/SMTP/IMAP

  2. 点击开启smtp服务

  3. 根据提示,发送短信验证码,最终获取令牌,即可使用smtp服务

邮箱发送邮件(包含附件,网易、QQ)相关推荐

  1. python3发送qq邮件_python3通过qq邮箱发送邮件以及附件

    本文实例为大家分享了python3通过qq邮箱发送邮件以及附件的具体代码,供大家参考,具体内容如下ZLP免费资源网 开启qq邮箱的smtp服务ZLP免费资源网 ZLP免费资源网 代码:ZLP免费资源网 ...

  2. python3通过qq邮箱发送邮件以及附件

    本文实例为大家分享了python3通过qq邮箱发送邮件以及附件的具体代码,供大家参考,具体内容如下 开启qq邮箱的smtp服务 代码: 在学习过程中有什么不懂得可以加我的 python学习qun,85 ...

  3. Python登录qq邮箱发送邮件(附件)

    # -*- coding: utf-8 -*- #导入smtplib和MIMETextimport smtplib,time from email.mime.text import MIMEText ...

  4. 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 ...

  5. Python连接qq邮箱服务器,调用qq邮箱发送邮件实战演示,qq邮箱授权码开通方法

    直接用 python 自带的 smtplib 库和 email 库就能实现邮件的发送. qq 邮箱发送的话,直接用密码是登陆不了的,需要用授权码. import smtplib from email. ...

  6. python 用QQ邮箱发送邮件

    SMTP 是发送邮件的协议.Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮箱. Python对SMTP支持有smtplib 和email 两个模块,email负责构造 ...

  7. java通过网易邮箱发送邮件

    java通过网易邮箱发送邮件 首先申请网易邮箱后开启POP3/SMTP服务,会获得一个授权码 通过hutool工具发送邮件 需要添加的依赖 <dependency><groupId& ...

  8. python 发送邮件 163_python练习-使用163邮箱发送邮件

    具体代码如下> #密码等敏感信息已经用****替换 import smtplib,sys from email.mime.text import MIMEText from email.head ...

  9. 通过javax.mail发送邮件(示例gmail、qq)

    最近学习了一下javax.mail,想通过gmail邮箱发送邮件到qq邮箱.但是由于gmail的两步验证,一直授权无法通过. 错误如下: DEBUG: JavaMail version 1.4.7 D ...

最新文章

  1. java设计模式----装饰器模式
  2. 【贪心】【P5078】Tweetuzki 爱军训
  3. VTK与Qt整合的示例
  4. 2021物理大事汇:μ子磁性异常,时间晶体现身,天体物理大丰收
  5. 手机怎么解决同ip多账号_抖音被限流,作品被判搬运,账号被判搬运限流,Dou?被封怎么办?...
  6. Python编程语言学习:python中与数字相关的函数(取整等)、案例应用之详细攻略
  7. Python列表排序 reverse、sort、sorted 操作方法详解
  8. Kotlin 知识梳理(13) 运行时的泛型
  9. 【转】PB实现在通知区域添加图标
  10. WEB API 系列(二) Filter的使用以及执行顺序
  11. 前端学习(3263):js中undefine
  12. golang new和make却别
  13. 自动阅读行业又出新招?离线阅读脚本套路满满
  14. 电子元器件商城与数据手册下载网站汇总
  15. WebIM 网页聊天工具
  16. vue-element-ui-文件上传ts版
  17. python 需要在什么环境下运行_运行Python需要什么环境?也需要虚拟机吗?
  18. 题解 CF1395A 【Boboniu Likes to Color Balls】
  19. IDEA高效使用技巧--->IDEA批量修改变量快捷键和全局搜索键
  20. Mysql中查询系统时间的方法

热门文章

  1. 编译android版本sdl2
  2. 全网推广公司告诉你互联网营销优势在哪里
  3. 高新区:打造大数据产业聚集示范区
  4. Android dependencies
  5. java 加密解密 工具包_java加密解密工具类
  6. SpringCloud升级之路2020.0.x版-26.OpenFeign的组件
  7. JS 经典闭包面试题
  8. 制作轮播图思路 ~ 使用JS做一个轮播图,先在脑海里捋一捋逻辑,我的思路是这样的,看图啦:
  9. 关于RadioButtonList的SelectChange事件没有及时触发问题
  10. 巨量星图sign算法还原(头条系)