发送复杂邮件
发送复杂邮件
发送复杂邮件和简单邮件的配置一样,就是发送体有一点点复杂
了解需要用到的类
1. MimeBodyPart类
它表示的是一个Mime消息,它和MimeMessage类(这个类在普通类邮件中用过)一样都是从Part接口继承过来的。
2.MimeMultipart类
它用来组合多个MIME消息,一个MimeMultipart对象可以包含多个代表MIME消息的MimeBodyPart对象
3.用一符图来表示他们的关系
编写java
package com.Google; import com.sun.mail.util.MailSSLSocketFactory; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.imageio.spi.ImageInputStreamSpi; 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 mailTest { public static void main(String[] args) throws Exception { Properties prop = new Properties(); prop.setProperty("mail.host","smtp.qq.com");//设置QQ邮箱服务器 prop.setProperty("mail.transport.protocol","smtp");//发送协议 prop.setProperty("mail.smtp.auth","true");//验证用户名和密码 //关于QQ邮箱,还要设置SSl安全加密 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true);//信任所有主机 prop.put("mail.smtp.ssl.enable","true"); prop.put("mail.smtp.ssl.socketFactory",sf); //使用javaMail发送邮箱的五个步骤 //1,创建整个应用程序所需的环境信息的Session对象 Session session = Session.getDefaultInstance(prop, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { //发件人邮箱用户名,授权码(Authentication,验证) return new PasswordAuthentication("203428174@qq.com", "xkwdlcsqvclaccah"); } }); session.setDebug(true); //2,通过Session获取transport对象 Transport transport = session.getTransport(); //3,使用邮箱的用户名和授权码,连接邮箱服务器 transport.connect("smtp.qq.com","203428174@qq.com", "xkwdlcsqvclaccah"); //4,编写内容 MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("203428174@qq.com"));//设置发件人 message.setRecipient(Message.RecipientType.TO, new InternetAddress("203428174@qq.com"));//设置收件人 message.setSubject("复杂邮箱"); //准备图片数据 MimeBodyPart image = new MimeBodyPart(); DataHandler dataHandler = new DataHandler(new FileDataSource("E:\\java\\Fileupp\\mai\\imag\\img.png"));//处理数据 image.setDataHandler(dataHandler);//图片的数据 image.setContentID("bb.jpg");//设置图片的别名 //准备普通文本的数据 MimeBodyPart text = new MimeBodyPart(); text.setContent("这是一封带图片<img src='cid:bb.jpg'","text/html;chartset=UTF-8"); //描述数据关系 MimeMultipart mtp = new MimeMultipart(); mtp.addBodyPart(image); mtp.addBodyPart(text); mtp.setSubType("mixed");//默认用最大范围 message.setContent(mtp);//邮件正文内容 message.saveChanges();//保存修改 //5,发送邮箱 transport.sendMessage(message, message.getAllRecipients()); transport.close(); } }
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术