当前位置: 首页 > news >正文

java 发送邮件

前期准备

pom文件中引入 JavaMail API 和 JavaBean Activation FrameWork,得到两个jar包:mail.jar 和 activation.jar

发送简单邮件(只有邮件正文,普通文本)

package com.zbttest.email;import com.sun.mail.util.MailSSLSocketFactory;import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;public class Test {public static void main(String[] args) throws Exception {new Test().sendMessage();}public void sendMessage() throws Exception {Properties pro = new Properties();pro.setProperty("mail.host","smtp.qq.com");//设置邮件服务器 为qq邮箱pro.setProperty("mail.transport.protocol","smtp");//邮件发送协议pro.setProperty("mail.smtp.auth","true");//需要验证用户名和密码//关于qq邮箱  还要设置SSL加密  加上以下几行代码即可MailSSLSocketFactory sf = new MailSSLSocketFactory();sf.setTrustAllHosts(true);pro.put("mail.smtp.ssl.enable",true);pro.put("mail.smtp.ssl.socketFactory",sf);//使用java发送邮件的  5个步骤//1.创建定义整个应用程序所需要的环境信息的Session对象//qq才有  其他邮箱就不用Session session = Session.getDefaultInstance(pro, new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {//发件人邮箱用户名、授权码(qq邮箱电脑版获取)return new PasswordAuthentication("1234567@qq.com","授权码");}});//开启Session的Debug模式,这样就可以看到程序发送email的运行状态session.setDebug(true);//2 通过session 的到transport对象Transport transport = session.getTransport();//3 使用邮箱的用户名和授权码链连接邮件服务器   发件邮箱的信息transport.connect("smtp.qq.com","1234567@qq.com","授权码");//4 创建邮件//创建邮件对象MimeMessage mimeMessage = new MimeMessage(session);//设置发件人mimeMessage.setFrom(new InternetAddress("12345676@qq.com"));//设置收件人邮箱mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("1234567@163.com"));//邮件主题 即邮件的标题mimeMessage.setSubject("测试java发送邮件");//邮件的文本内容mimeMessage.setContent("你好啊,这是一封测试邮件","text/html;charset=UTF-8");//5 发送邮件transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());//关闭连接transport.close();}
}

发送含有图片数据的邮件

package com.zbttest.email;import com.sun.mail.util.MailSSLSocketFactory;import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;public class Test1 {public static void main(String[] args) throws Exception {new Test().sendMessage();}public void sendMessage() throws Exception {Properties pro = new Properties();pro.setProperty("mail.host","smtp.qq.com");//设置邮件服务器 为qq邮箱pro.setProperty("mail.transport.protocol","smtp");//邮件发送协议pro.setProperty("mail.smtp.auth","true");//需要验证用户名和密码//关于qq邮箱  还要设置SSL加密  加上以下几行代码即可MailSSLSocketFactory sf = new MailSSLSocketFactory();sf.setTrustAllHosts(true);pro.put("mail.smtp.ssl.enable",true);pro.put("mail.smtp.ssl.socketFactory",sf);//使用java发送邮件的  5个步骤//1.创建定义整个应用程序所需要的环境信息的Session对象//qq才有  其他邮箱就不用Session session = Session.getDefaultInstance(pro, new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {//发件人邮箱用户名、授权码(qq邮箱电脑版获取)return new PasswordAuthentication("1234567@qq.com","授权码");}});//开启Session的Debug模式,这样就可以看到程序发送email的运行状态session.setDebug(true);//2 通过session 的到transport对象Transport transport = session.getTransport();//3 使用邮箱的用户名和授权码链连接邮件服务器   发件邮箱的信息transport.connect("smtp.qq.com","1234567@qq.com","授权码");//4 创建邮件//创建邮件对象MimeMessage mimeMessage = new MimeMessage(session);//设置发件人mimeMessage.setFrom(new InternetAddress("12345676@qq.com"));//设置收件人邮箱mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("1234567@163.com"));//邮件主题 即邮件的标题mimeMessage.setSubject("测试java发送邮件");/*邮件的文本内容mimeMessage.setContent("你好啊,这是一封测试邮件","text/html;charset=UTF-8");*///准备邮件数据//准备图片数据MimeBodyPart img = new MimeBodyPart();//图片需要经过特殊处理DataHandler dh = new DataHandler(new FileDataSource("src/resources/bz.jpg"));//将图片添加进body中img.setDataHandler(dh);//给图片设置一个 id  后面可以通过id进行引用img.setContentID("bz.jpg");//准备正文数据MimeBodyPart text = new MimeBodyPart();//正文内容  里面可以通过 cid 引用上面的图片text.setContent("这是一封邮件正文带图片<img src='cid:bz.jpg'>的邮件","text/html;charset=UTF-8");//添加数据并描述数据关系MimeMultipart mm = new MimeMultipart();mm.addBodyPart(text);mm.addBodyPart(img);mm.setSubType("related");//只有文本 用 alternztive  有内嵌资源 用 related  有附件 用 mixed//设置到消息中并保存修改//把最后写好的邮件放到消息当中mimeMessage.setContent(mm);//保存修改mimeMessage.saveChanges();//5 发送邮件transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());//关闭连接transport.close();}
}

发送含有附件的邮件

package com.zbttest.email;import com.sun.mail.util.MailSSLSocketFactory;import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;public class Test2 {public static void main(String[] args) throws Exception {new Test().sendMessage();}public void sendMessage() throws Exception {Properties pro = new Properties();pro.setProperty("mail.host","smtp.qq.com");//设置邮件服务器 为qq邮箱pro.setProperty("mail.transport.protocol","smtp");//邮件发送协议pro.setProperty("mail.smtp.auth","true");//需要验证用户名和密码//关于qq邮箱  还要设置SSL加密  加上以下几行代码即可MailSSLSocketFactory sf = new MailSSLSocketFactory();sf.setTrustAllHosts(true);pro.put("mail.smtp.ssl.enable",true);pro.put("mail.smtp.ssl.socketFactory",sf);//使用java发送邮件的  5个步骤//1.创建定义整个应用程序所需要的环境信息的Session对象//qq才有  其他邮箱就不用Session session = Session.getDefaultInstance(pro, new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {//发件人邮箱用户名、授权码(qq邮箱电脑版获取)return new PasswordAuthentication("1234567@qq.com","授权码");}});//开启Session的Debug模式,这样就可以看到程序发送email的运行状态session.setDebug(true);//2 通过session 的到transport对象Transport transport = session.getTransport();//3 使用邮箱的用户名和授权码链连接邮件服务器  发件邮箱的信息transport.connect("smtp.qq.com","1234567@qq.com","授权码");//4 创建邮件//创建邮件对象MimeMessage mimeMessage = createMailMessage(session);//5 发送邮件transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());//关闭连接transport.close();}private MimeMessage createMailMessage(Session session) throws Exception {//创建用来返回的消息对象MimeMessage mimeMessage = new MimeMessage(session);//设置发件人mimeMessage.setFrom(new InternetAddress("12345676@qq.com"));//设置收件人邮箱mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("1234567@163.com"));//邮件主题 即邮件的标题mimeMessage.setSubject("测试java发送邮件");/*编写邮件内容1.图片2.附件3.文本*///图片MimeBodyPart body1 = new MimeBodyPart();body1.setDataHandler(new DataHandler(new FileDataSource("src/resources/bz.jpg")));body1.setContentID("bz.jpg");//文本MimeBodyPart body2 = new MimeBodyPart();body2.setContent("不知道发点啥了,反正里面有一张图片<img src='cid:bz.jpg'>","text/html;charset=UTF-8");//附件MimeBodyPart body3 = new MimeBodyPart();body3.setDataHandler(new DataHandler(new FileDataSource("src/resources/log4j.properties")));body3.setFileName("设置附件文件的名字");//附件设置名字MimeBodyPart body4 = new MimeBodyPart();body4.setDataHandler(new DataHandler(new FileDataSource("src/resources/1.txt")));body4.setFileName("设置附件文件的名字1");//附件设置名字//拼接邮件正文内容MimeMultipart multipart1 = new MimeMultipart();multipart1.addBodyPart(body1);multipart1.addBodyPart(body2);multipart1.setSubType("related");//将文本和图片内嵌进去//将拼装好的文本和图片设置为正文MimeBodyPart conteneText = new MimeBodyPart();conteneText.setContent(multipart1);//拼接附件MimeMultipart allFile = new MimeMultipart();allFile.addBodyPart(body3);allFile.addBodyPart(body4);allFile.addBodyPart(conteneText);allFile.setSubType("mixed");//正文和附件都有  将类型设置为  mixed//将内容添加到消息中mimeMessage.setContent(allFile);//保存修改mimeMessage.saveChanges();return mimeMessage;}
}

http://www.lryc.cn/news/516072.html

相关文章:

  • 聚类系列 (二)——HDBSCAN算法详解
  • AngularJS HTML DOM
  • C语言延时实现
  • OSI模型的网络层中产生拥塞的主要原因?
  • 机器学习周报-ModernTCN文献阅读
  • 什么是网关路由
  • 信号的产生、处理
  • 在Linux中,zabbix如何监控脑裂?
  • C++基础概念复习
  • Earth靶场
  • JavaScript 日期格式
  • django vue3实现大文件分段续传(断点续传)
  • xiaoya小雅超集使用夸克网盘缓存教程
  • 计算机基础知识复习1.4
  • SpringMVC(三)请求
  • Node.js应用程序遇到了内存溢出的问题
  • 如何构建云原生时空大数据平台?
  • 二极管钳位电路分享
  • 腾讯云智能结构化 OCR:驱动多行业数字化转型的核心引擎
  • 19.3、Unix Linux安全分析与防护
  • JVM对象内存结构
  • 联邦学习和大模型相结合: 数据隐私,提升训练效率,架构优化
  • 命令别名和命令历史
  • 打造三甲医院人工智能矩阵新引擎(二):医学影像大模型篇--“火眼金睛”TransUNet
  • Scade pragma: separate_io
  • IWOA-GRU和GRU时间序列预测(改进的鲸鱼算法优化门控循环单元)
  • “知识图谱AI教学辅助系统:点亮智慧学习的新灯塔
  • 产品 防尘防水IP等级 划分与实验方法
  • 【微服务】1、引入;注册中心;OpenFeign
  • 01、Docker学习,第一天:简单入门与安装