一、导入pom依赖
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
二、代码实现
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Properties;
public class MailWithAttachment {
public static void main(String[] args) throws UnsupportedEncodingException, MessagingException, com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException {
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol","smtp");
properties.setProperty("mail.smtp.host","smtp.aliyun.com");
properties.setProperty("mail.smtp.port","465");
properties.setProperty("mail.smtp.auth","true");
properties.setProperty("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.fallback","false");
properties.setProperty("mail.smtp.socketFactory.port","465");
Session session = Session.getInstance(properties);
session.setDebug(true);
MimeMessage mimeMessage = createMimeMessage(session,"properties@aliyun.com","495356619@qq.com");
Transport transport = session.getTransport();
transport.connect("properties@aliyun.com","properties123");
transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
transport.close();
}
public static MimeMessage createMimeMessage(Session session,String send,String recipient) throws MessagingException, UnsupportedEncodingException, com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException {
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setSubject("setSubject是标题", StandardCharsets.UTF_8.name());
InternetAddress internetAddress = new InternetAddress(send,"personal是发件人的名字",StandardCharsets.UTF_8.name());
mimeMessage.setFrom(internetAddress);
// 创建图片节点
MimeBodyPart imagePart = new MimeBodyPart();
DataHandler imageDataHandler = new DataHandler(new FileDataSource("S:/01.png")); //图片地址
imagePart.setDataHandler(imageDataHandler);
imagePart.setContentID("myImage");
// 创建文本节点
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent("这是正文内容image:<img src='cid:myImage' />","text/html;charset=utf-8");
// 将文本节点,图片节点 --> 组装
MimeMultipart mm_text_image = new MimeMultipart();
mm_text_image.addBodyPart(imagePart);
mm_text_image.addBodyPart(textPart);
mm_text_image.setSubType("related");
// 注意:正文中 只能出现MimeBodyPart普通节点 不能出现复合节点只能出现MimeMultipart普通节点
// MimeMultipart -> MimeBodyPart
MimeBodyPart text_image_bodyPart = new MimeBodyPart();
text_image_bodyPart.setContent(mm_text_image);
// 附件
MimeBodyPart attachment = new MimeBodyPart();
DataHandler fileDataHandler = new DataHandler(new FileDataSource("S:/02.png"));
attachment.setDataHandler(fileDataHandler);
//给附件设置文件名
attachment.setFileName(MimeUtility.encodeText(fileDataHandler.getName()));
// 将刚才处理好的”文本+图片“节点 与 附件 设置成一个新的 复合节点
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text_image_bodyPart);
mm.addBodyPart(attachment);
mm.setSubType("mixed"); //混合关系
mimeMessage.setContent(mm,"text/html;charset=utf-8");
InternetAddress recipientAddress = new InternetAddress(recipient, "personal是收件人的名称", StandardCharsets.UTF_8.name());
mimeMessage.setRecipient(MimeMessage.RecipientType.BCC,recipientAddress);
mimeMessage.setSentDate(new Date());
mimeMessage.saveChanges();
return mimeMessage;
}
}
网友评论