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, "1215230335@qq.com", "1215230335@qq.com", "1215230335@qq.com", "1215230335@qq.com"); Transport transport=(Transport) se.getTransport(); transport.connect("1215230335@qq.com", "slxptfnnjocgicdi");//动态授权码
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} }

  

上一篇:caffe Python API 之Inference


下一篇:QQ空间直播秒开优化实践[读]