邮件发送——java mail
2022/1/8 17:03:46
本文主要是介绍邮件发送——java mail,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
java mail说白了就是套模板,大框架。框架搭起来后邮件发送的内容就稍微变通一下,就和搭积木一样。
依赖的jar包
使用项目管理工具创建项目的话对应去搜就行了。
纯文本邮件
import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.security.GeneralSecurityException; import java.util.Properties; public class Mail { public static void main(String[] args) throws GeneralSecurityException, MessagingException { Properties properties = new Properties(); properties.setProperty("mail.host","smtp.qq.com"); //设置qq邮件服务器 properties.setProperty("mail.transport.protocol","smtp"); //邮件发送协议 properties.setProperty("mail.smtp.auth","true"); //需要验证用户名和密码 //发送邮件的五个步骤 //1. 获取Session实例(定义整个程序所需环境信息,比如主机名、端口号、采用的邮件发送和接收协议) Session session = Session.getDefaultInstance(properties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("2979746577@qq.com","jxitmoyolhrjdcdh"); } }); session.setDebug(true); //开启debug模式 //2. 通过session获取transport(用来发送邮件) Transport transport = session.getTransport(); //3. 使用邮箱的用户名和授权码连上邮件服务器 transport.connect("smtp.qq.com","2979746577@qq.com","jxitmoyolhrjdcdh"); //4. 创建邮件 MimeMessage msg = new MimeMessage(session); //设置邮件的发送人 msg.setFrom(new InternetAddress("2979746577@qq.com")); //设置邮件的接收人 msg.setRecipient(Message.RecipientType.TO,new InternetAddress("2979746577@qq.com")); //设置邮件的主题 msg.setSubject("你好啊!java mail"); //设置邮件的内容 msg.setContent("<h1 style=’color=red‘>你好啊!希望我们能成为很好的朋友</h1>","text/html;charset=utf-8"); //5. 发送邮件 transport.sendMessage(msg, msg.getAllRecipients()); //6. 关闭连接 transport.close(); } }
带附件的邮件
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 MailPlus { public static void main(String[] args) throws Exception{ Properties properties = new Properties(); properties.setProperty("mail.host","smtp.qq.com"); //设置qq邮件服务器 properties.setProperty("mail.transport.protocol","smtp"); //邮件发送协议 properties.setProperty("mail.smtp.auth","true"); //需要验证用户名和密码 //发送邮件的五个步骤 //1. 获取Session实例(定义整个程序所需环境信息,比如主机名、端口号、采用的邮件发送和接收协议) Session session = Session.getDefaultInstance(properties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("2979746577@qq.com","jxitmoyolhrjdcdh"); } }); session.setDebug(true); //开启debug模式 //2. 通过session获取transport(用来发送邮件) Transport transport = session.getTransport(); //3. 使用邮箱的用户名和授权码连上邮件服务器 transport.connect("smtp.qq.com","2979746577@qq.com","jxitmoyolhrjdcdh"); //4. 创建邮件 MimeMessage msg = new MimeMessage(session); //设置邮件的发送人 msg.setFrom(new InternetAddress("2979746577@qq.com")); //设置邮件的接收人 msg.setRecipient(Message.RecipientType.TO,new InternetAddress("2979746577@qq.com")); //设置邮件的主题 msg.setSubject("你好啊!java mail"); //================================================================= //准备图片数据 MimeBodyPart image = new MimeBodyPart(); //图片需要经过数据处理 DataHandler handler = new DataHandler(new FileDataSource("3.png")); image.setDataHandler(handler); image.setContentID("bz.jpg"); //给图片设置一个id,附件是设置FileName //准备正文数据 MimeBodyPart text = new MimeBodyPart(); text.setContent("这是邮件的正文,附带了一张图片<img src='cid:bz.jpg'","text/html;charset=utf-8"); //描述数据关系 MimeMultipart mm = new MimeMultipart(); mm.addBodyPart(image); mm.addBodyPart(text); mm.setSubType("related"); //把编辑好的内容放到消息中,保存修改 msg.setContent(mm); msg.saveChanges(); //================================================================= //5. 发送邮件 transport.sendMessage(msg, msg.getAllRecipients()); //6. 关闭连接 transport.close(); } }
这篇关于邮件发送——java mail的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)
- 2024-05-30【Java】百万数据excel导出功能如何实现
- 2024-05-30我们小公司,哪像华为一样,用得上IPD(集成产品开发)?
- 2024-05-30java excel上传--poi
- 2024-05-30安装笔记本应用商店的pycharm,再安排pandas等模块,说是没有打包工具?
- 2024-05-29java11新特性
- 2024-05-29哪些无用敏捷指标正在破坏敏捷转型?
- 2024-05-29鸿蒙原生应用再新丁!新华社 入局鸿蒙
- 2024-05-29设计模式 之 迭代器模式(Iterator)