📄 agilemailhelper.java
字号:
package com.lanx.app.mail;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import com.lanx.app.mail.entity.MailAttache;
import com.lanx.app.mail.entity.MailInfo;
import com.lanx.app.mail.util.AgileMailConstants;
import com.lanx.app.mail.util.AgileMailUtils;
public class AgileMailHelper {
public static void setParamter(MimeMessage mimeMsg,MailInfo mailInfo) throws AddressException, MessagingException {
String to = mailInfo.getMailTo(); // 收件人
String cc = mailInfo.getCc();//""; // 抄送人
String bcc = mailInfo.getBcc();//""; // 密送人
String subject = mailInfo.getSubject(); // 标题
// 设置收信人
if (to != null && !"".equals(to)) {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
//String[] splitTo = AgileMailUtils.split(to);
//InternetAddress[] address = new InternetAddress[splitTo.length];
//for (int i = 0; i < splitTo.length; i++) {
// address[i] = new InternetAddress(splitTo[i]);
//}
//mimeMsg.setRecipients(Message.RecipientType.TO, address);
}
// 设置抄送人
if (cc != null && !"".equals(cc)) {
mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
}
// 设置暗送人
if (bcc != null && !"".equals(bcc)) {
mimeMsg.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(bcc));
}
// 设置邮件主题
//BASE64Encoder enc = new BASE64Encoder();
//mimeMsg.setSubject("=?GB2312?B?" + enc.encode(subject.getBytes()) + "?=");
mimeMsg.setSubject(subject, "GBK");
// 设置邮件内容
// mimeMsg.setText(content, "gb2312");
// 发送日期
mimeMsg.setSentDate(new Date());
}
public static void addContent(Multipart mimeMultipart,MailInfo mailInfo) throws MessagingException, IOException {
MimeBodyPart mbp = new MimeBodyPart(); // 正文
String content = mailInfo.getMailContent();
String format = mailInfo.getMailFormat();
// 设置邮件正文格式(html/text)
if (AgileMailConstants.MailFormat.html.equals(format)) {
// 网页格式
content = AgileMailUtils.composeContent(mailInfo);
mbp.setDataHandler(new DataHandler(content,"text/html;charset=GBK"));
//mbp.setHeader("Content-Type","text/html; charset=GBK");
//mbp.setContent(content,"text/html; charset=GBK");
} else if (content != null && !"".equals(content.trim())) {// 如果正文不为空
// 普通格式
mbp.setText(content, "GBK");
}
// 正文
mimeMultipart.addBodyPart(mbp);
}
public static void addAttache(Multipart mimeMultipart,List<MailAttache> attaches) throws UnsupportedEncodingException, MessagingException{
if (attaches != null && attaches.size() > 0){
MimeBodyPart mbpAttache = null;// 附件
for (int i = 0; i < attaches.size() && i < 7; i++) {
mbpAttache = new MimeBodyPart();
String filename = attaches.get(i).getAttacheName();
FileDataSource fds = new FileDataSource(filename);
mbpAttache.setDataHandler(new DataHandler(fds));
// 解决附件中文问题
// mbp1.setFileName(fileds.getName(),"gb2312");
mbpAttache.setFileName(MimeUtility.encodeWord(fds.getName(),"GB2312", null));
mimeMultipart.addBodyPart(mbpAttache);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -