package Samples;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public class Demo2WithAttachement {
public static void sendEmail() throws Exception {
//1、 Properties对象封装会话所需的信息
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");//使用协议 smtp
props.setProperty("mail.smtp.host", "smtp.qq.com");// qq的smtp服务器地址
props.setProperty("mail.smtp.port", "465"); //协议端口号
props.setProperty("mail.smtp.auth", "true");// 需要授权
//是否需要SSL安全认证 qq是要求需要的
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false"); // 没有经过SSL认证的邮件不接收
props.setProperty("mail.smtp.socketFactory.port", "465");
//2、根据Properties对象封装的信息创建Session对象
Session session = Session.getInstance(props);
session.setDebug(true); //是否开启调试模式
//3、 根据session创建邮件MimeMessage
MimeMessage message = creatMimeMessage(session,"发件人邮箱","收件人邮箱","UTF-8");
//4、通过session建立与stmp服务器的连接
Transport transport = session.getTransport();
//connect(String host,String user,String password) host:发件人邮箱地址; user:用户名; password:邮箱授权码
transport.connect("发件人邮箱","发件人邮箱授权码");
//5、发送邮件
transport.sendMessage(message, message.getAllRecipients());
//6、关闭连接
transport.close();
}
public static MimeMessage creatMimeMessage(Session session,String send, String recipient, String charset) throws Exception {
//简单邮件:{发件人(from)、收件人(to)、标题(subject)、正文(content)、日期()date} {图片、附件}
MimeMessage message = new MimeMessage(session);
//InternetAddress(String address,String personal, String charset) 其中address:邮件地址 ;personal:标识 ; charset:编码
Address from = new InternetAddress(send,"我是发件人",charset);
Address to = new InternetAddress(recipient,"我是收件人",charset);
message.setFrom(from);
//创建图片节点
MimeBodyPart imagePart = new MimeBodyPart();
DataHandler imageHandler = new DataHandler(new FileDataSource("src/study.jpg"));
imagePart.setDataHandler(imageHandler);
//通过ID唯一标识一张图片
imagePart.setContentID("mystudyImage");
//创建文本节点:目的是为了加载图片节点
MimeBodyPart textPart = new MimeBodyPart();
//正文部分显示文本和图片
//setContent(Object o, String type) Object可为html类型;其中type 为网页编码类型
textPart.setContent("这是正文……;image: <img src='cid:mystudyImage'></img>","text/html;charset=utf-8");
//将文本节点和图片节点组装为复合节点mm_text_image
MimeMultipart mm_text_image = new MimeMultipart();
mm_text_image.addBodyPart(textPart);
mm_text_image.addBodyPart(imagePart);
//单节点的联系类型 文本节点和图片节点同在正文为related联系
mm_text_image.setSubType("related");//关联关系
//Mime协议规定:正文中只能出现MimeBodyPart普通节点,不能是复合节点MimeMutilpart
// MimeMutilpart-- >MimeBodyPart
MimeBodyPart text_image_bodyPart = new MimeBodyPart();
text_image_bodyPart.setContent(mm_text_image);
//添加附件节点
MimeBodyPart attachmentPart = new MimeBodyPart();
DataHandler fileHandler = new DataHandler(new FileDataSource("src/mulan.jpg"));
attachmentPart.setDataHandler(fileHandler);
//给附件设置名字 fileHandler.getName()返回文件名字
attachmentPart.setFileName(MimeUtility.encodeText(fileHandler.getName()));
//将“正文+图片”节点与“附件”节点组装成复合节点
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(attachmentPart);
mm.addBodyPart(text_image_bodyPart);
//设置二者的关系 混合关系
mm.setSubType("mixed");
//收件人类型:普通收件人(TO)、抄送(CC)、密送(BCC)
message.setRecipient(MimeMessage.RecipientType.TO,to);
message.setSubject("这是主题",charset); // 邮件主题
message.setContent(mm); //正文内容
message.setSentDate(new Date()); //发件日期
message.saveChanges();
return message;
}
public static void main(String[] args) throws Exception {
sendEmail();
}
}
Java常见应用(四):发送带附件的邮件
最新推荐文章于 2022-05-20 15:25:54 发布