import javax.activation.DataHandler;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* Create by wzy on 18/6/22
*/
public class EmailSender {
private String hostName;
private String charset;
private Authenticator authenticator;
private int port;
EmailSender(String hostName, String charset, Authenticator authenticator, int port){
this.hostName = hostName;
this.charset = charset;
this.authenticator = authenticator;
this.port = port;
}
private static final int MAX_SEND_SIZE = 500;
public boolean sendSimpleEmail(String fromAddress,String fromName, String sub, String content, List toAddressList,
List attachFileList, List embedImageList){
if (toAddressList == null || toAddressList.size()<=0) {
throw new RuntimeException("toEmailAddressList is empty!");
}
if (toAddressList.size() > MAX_SEND_SIZE) {
throw new RuntimeException("toEmailAddressList is more than [" + MAX_SEND_SIZE + "]!");
}
try {
Session session = getSession();
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromAddress, fromName, charset));
message.setRecipients(Message.RecipientType.TO, getAddressArr(toAddressList));
message.setSubject(sub, charset);
//message.setContent(content, "text/html;charset="+charset);
// 消息体
MimeMultipart mainPart = new MimeMultipart();
BodyPart body = new MimeBodyPart();
body.setContent(content, "text/html;charset=UTF-8");
mainPart.addBodyPart(body);
// 附件
if(attachFileList != null && attachFileList.size() > 0){
for(AttachFileBo fileBo:attachFileList){
BodyPart attachFilePart = new MimeBodyPart();
attachFilePart.setDataHandler(new DataHandler(new ByteArrayDataSource(fileBo.getIn(), "application/octet-stream")));
attachFilePart.setFileName(fileBo.getFileName());
mainPart.addBodyPart(attachFilePart);
}
}
// 内嵌图片
if(embedImageList != null && embedImageList.size() > 0){
for(EmbedImageBo embedImageBo : embedImageList){
BodyPart embedImagePart = new MimeBodyPart();
embedImagePart.setDataHandler(new DataHandler(new ByteArrayDataSource(embedImageBo.getIn(), "application/octet-stream")));
((MimeBodyPart) embedImagePart).setContentID(embedImageBo.getCid());
mainPart.addBodyPart(embedImagePart);
}
}
message.setContent(mainPart);
Transport.send(message);
return true;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
private Session getSession(){
// 获取系统属性
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", hostName);
properties.setProperty("mail.smtp.port", String.valueOf(port));
properties.setProperty("mail.debug", "false");
properties.setProperty("mail.smtp.starttls.enable", "false");
properties.setProperty("mail.smtp.starttls.required", "false");
properties.setProperty("mail.smtp.sendpartial", "false");
properties.setProperty("mail.smtps.sendpartial", "false");
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.connectiontimeout", "60000");
properties.setProperty("mail.smtp.timeout", "60000");
return Session.getDefaultInstance(properties, authenticator);
}
private InternetAddress[] getAddressArr(List toAddressList) throws Exception{
InternetAddress[] addressArr = new InternetAddress[toAddressList.size()];
for(int i=0; i很高兴认识你1222";
List toAddressList = new ArrayList();
toAddressList.add("[email protected]");
toAddressList.add("[email protected]");
// 附件
List attachFileList= new ArrayList();
AttachFileBo attachFileBo = new AttachFileBo();
attachFileBo.setIn(new FileInputStream(new File("/Users/jd/test.log")));
attachFileBo.setFileName("test.log");
attachFileList.add(attachFileBo);
attachFileBo = new AttachFileBo();
attachFileBo.setIn(new FileInputStream(new File("/Users/jd/Downloads/image002.png")));
attachFileBo.setFileName("image002.png");
attachFileList.add(attachFileBo);
attachFileBo = new AttachFileBo();
attachFileBo.setIn(new FileInputStream(new File("/Users/jd/Downloads/rarosx-5.6.b3.tar.gz")));
attachFileBo.setFileName("rarosx-5.6.b3.tar.gz");
attachFileList.add(attachFileBo);
//内嵌图片
List embedImageBoList = new ArrayList();
EmbedImageBo embedImageBo = new EmbedImageBo();
embedImageBo.setIn(new FileInputStream(new File("/Users/jd/Documents/5b29d9b0N1f334e90.jpg")));
embedImageBo.setCid("123123123");
embedImageBoList.add(embedImageBo);
Boolean result = sender.sendSimpleEmail("[email protected]", "王自英", "你好", content, toAddressList, attachFileList, embedImageBoList);
System.out.println(result);
}
}
这么一个发送邮件的方法可以支持四种邮件发送:1.纯文本邮件,2.HTML邮件, 3.附件邮件, 4内嵌图片邮件, 5.混合邮件。main方法中有测试代码,可以根据不同的参数发送不同的邮件。如果有附件,发送时带附件发送。如果有内嵌图片,发送时带内嵌图片发送,如果都有,那全都可以发送。注意:内嵌图片和网络图片不一样,网络图片其实还是HTML邮件,内嵌图片是本地图片或图片的IO流。
发送附件时,不管附件的类型是什么,png,txt,log,zip 等,统统使用"application/octet-stream" ,并设置文件的filename。
发送内嵌图片时cid要对应的上。
import java.io.FileInputStream;
/**
* Create by wzy on 18/6/22
*/
public class AttachFileBo {
private String FileName;
private FileInputStream in;
public String getFileName() {
return FileName;
}
public void setFileName(String fileName) {
FileName = fileName;
}
public FileInputStream getIn() {
return in;
}
public void setIn(FileInputStream in) {
this.in = in;
}
}
import java.io.FileInputStream;
/**
* Create by wzy on 18/6/22
*/
public class EmbedImageBo {
private String cid;
private FileInputStream in;
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
public FileInputStream getIn() {
return in;
}
public void setIn(FileInputStream in) {
this.in = in;
}
}