JMail邮件发送
import org.apache.commons.lang3.StringUtils;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.FileOutputStream;
import java.util.*;
public class SendEmailSMTP {
public static void main(String[] args) throws Exception {
String[] fromEmail = {"***@yeah.net", "yeah的昵称"};
String fromEmailPWD = "***";
Map<String, String> toEmail = new HashMap<>();
toEmail.put("***@foxmail.com", "foxmail昵称");
Map<String, String> ccEmail = new HashMap<>();
ccEmail.put("***@bis.com.cn", "bis昵称");
String subject = "带附件和带图片的的邮件4";
String content = "这是测试邮件发送<br/><img src='cid:Desert.jpg'>这是测试啊<br/><img src='cid:test.jpg'>";
List<String> partList = new ArrayList<>();
partList.add("D:\\测试附件.zip");
partList.add("D:\\测试附件2asda.xlsx");
Map<String, String> picMap = new HashMap<>();
picMap.put("D:\\Desert.jpg", "Desert.jpg");
picMap.put("D:\\测试图片.jpg", "test.jpg");
String savePath = "D:\\";
String host = "smtp.yeah.net";
String port = "25";
sendMixedMail(host, port, fromEmail, fromEmailPWD, toEmail, ccEmail, null, subject, content, partList, picMap, savePath);
}
public static void sendMixedMail(String host, String port, String[] fromEmail, String fromEmailPWD, Map<String, String> toEmail, Map<String, String> ccEmail, Map<String, String> bccEmail, String subject, String content, List<String> partList, Map<String, String> picMap, String savePath) throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.smtp.host", host);
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.port", port);
prop.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(prop, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail[0], fromEmailPWD);
}
});
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmail[0], fromEmail[1], "UTF-8"));
if (toEmail != null && toEmail.size() > 0) {
Address[] addr = new Address[toEmail.size()];
int i = 0;
for (Map.Entry<String, String> stringStringEntry : toEmail.entrySet()) {
addr[i] = new InternetAddress(stringStringEntry.getKey(), stringStringEntry.getValue(), "UTF-8");
i++;
}
message.setRecipients(Message.RecipientType.TO, addr);
if (ccEmail != null && ccEmail.size() > 0) {
int j = 0;
Address[] addr2 = new Address[ccEmail.size()];
for (Map.Entry<String, String> stringStringEntry : ccEmail.entrySet()) {
addr2[j] = new InternetAddress(stringStringEntry.getKey(), stringStringEntry.getValue(), "UTF-8");
j++;
}
message.setRecipients(Message.RecipientType.CC, addr2);
}
if (bccEmail != null && bccEmail.size() > 0) {
int j = 0;
Address[] addr2 = new Address[bccEmail.size()];
for (Map.Entry<String, String> stringStringEntry : bccEmail.entrySet()) {
addr2[j] = new InternetAddress(stringStringEntry.getKey(), stringStringEntry.getValue(), "UTF-8");
j++;
}
message.setRecipients(Message.RecipientType.BCC, addr2);
}
message.setSubject(subject);
MimeMultipart mp = new MimeMultipart();
MimeMultipart mp1 = new MimeMultipart();
MimeBodyPart text = new MimeBodyPart();
text.setContent(content, "text/html;charset=UTF-8");
mp1.addBodyPart(text);
if (picMap != null && picMap.size() > 0)
for (Map.Entry<String, String> entry : picMap.entrySet()) {
MimeBodyPart image = new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource(entry.getKey())));
image.setContentID(entry.getValue());
mp1.addBodyPart(image);
}
mp1.setSubType("related");
if (partList != null && partList.size() > 0)
for (String s : partList) {
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource(s));
attach.setDataHandler(dh);
attach.setFileName(MimeUtility.encodeText(dh.getName()));
mp.addBodyPart(attach);
}
MimeBodyPart contents = new MimeBodyPart();
contents.setContent(mp1);
mp.addBodyPart(contents);
mp.setSubType("mixed");
message.setContent(mp);
message.saveChanges();
if (StringUtils.isNotBlank(savePath))
message.writeTo(new FileOutputStream(savePath + subject + IDUtil.getID() + ".eml"));
Transport.send(message);
} else {
System.out.println("--sendMixedMail--邮箱接收人不能为空");
}
}
}