java发带图片正文和附件的邮件mail

Posted 黑魔法os

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java发带图片正文和附件的邮件mail相关的知识,希望对你有一定的参考价值。

package com.mail;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.MessagingException;
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 mailtest {
	public static MimeMessage createmail(Session se,String send,String recv1,String recv2,String recv3) throws MessagingException, UnsupportedEncodingException {
		MimeMessage message=new MimeMessage(se);
		message.setSubject("标题","utf-8");
		//message.setContent("正文。。。。。","text/html;charset=utf-8");本例用图片代替正文
		Address address=new InternetAddress(send,"sengname","utf-8");
		message.setFrom(address);
		
		
		//创建图片文本节点
		MimeBodyPart imagePart=new MimeBodyPart();
		DataHandler dataHandler=new DataHandler(new FileDataSource("t1.jpg"));
		imagePart.setDataHandler(dataHandler);
		imagePart.setContentID("myimage");
		
		MimeBodyPart textPart=new MimeBodyPart();
		textPart.setContent("<image src=‘cid:myimage ‘/>","text/html;charset=utf-8");
		
		//组装文本、图片节点
		MimeMultipart mimeMultipart=new MimeMultipart();
		mimeMultipart.addBodyPart(imagePart);
		mimeMultipart.addBodyPart(textPart);
		mimeMultipart.setSubType("related");//关联关系
		//图片-文本--复合--转普通节点
		MimeBodyPart tex_image_tPart=new MimeBodyPart();
		tex_image_tPart.setContent(mimeMultipart);
		
		
		//附件
		MimeBodyPart aPart=new MimeBodyPart();
		DataHandler dataHandler1=new DataHandler(new FileDataSource("t1.jpg"));
		aPart.setDataHandler(dataHandler1);
		//附件文件名
		aPart.setFileName(MimeUtility.encodeText(dataHandler1.getName()));
		//组装
		MimeMultipart mimeMultipart1=new MimeMultipart();
		mimeMultipart1.addBodyPart(tex_image_tPart);
		mimeMultipart1.addBodyPart(aPart);
		mimeMultipart1.setSubType("mixd");//混合关系
		
		message.setContent(mimeMultipart1,"text/html;charset=utf-8");
		
		
		message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(recv1,"shoujianren1","utf-8"));//发送shojianren
		message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress(recv2,"shoujianren2","utf-8"));//抄送
		message.setRecipient(MimeMessage.RecipientType.BCC, new InternetAddress(recv3,"shoujianren3","utf-8"));//密送
		message.setSentDate(new Date());
		
		message.saveChanges();
		return message;
		
	}
	public static void main(String[] args) throws MessagingException, UnsupportedEncodingException {
		// TODO Auto-generated method stub
		Properties props=new Properties();

		props.setProperty("mail.transport.protocol", "smtp");// 使用的协议(JavaMail规范要求)
		props.setProperty("mail.smtp.host", "smtp.qq.com");
		props.setProperty("mail.smtp.port","465");//设置端口  // 发件人的邮箱的 SMTP 服务器地址
		props.setProperty("mail.smtp.auth", "true");// 需要请求认证
		
		// 需要请求认证;只有qq有这一项
		props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.setProperty("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.socketFactory.port", "465");
		
		Session se=Session.getInstance(props);
		se.setDebug(true);//dayin运行信息
		MimeMessage message=createmail(se, "[email protected]", "[email protected]", "[email protected]", "[email protected]");
		
		Transport transport=(Transport) se.getTransport();
		
		transport.connect("[email protected]", "slxptfnnjocgicdi");//动态授权码
		transport.sendMessage(message, message.getAllRecipients());
		transport.close();
	}

}

  

以上是关于java发带图片正文和附件的邮件mail的主要内容,如果未能解决你的问题,请参考以下文章

解决java mail发送TXT附件被直接显示在正文中的问题

Java mail发送邮件附件出现.eml文件夹

springboot:java实现邮件及附件发送HTML正文的三种方式附带源码

如何使用 System.NET.mail 发送带有附件但没有任何纯文本正文的电子邮件?

springboot:java实现邮件及附件发送HTML正文的三种方式附带源码

[Java] JavaMail 发送带图片的 html 格式的邮件