邮件发送工具类
package com.util;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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 SendMail {
private String mail_host = "";
private String mail_port = "25";
private String userName = "";
private String password = "";
public void sendTextMail(String to, String subject, Date sentDate,
String text) throws Exception {
Properties pro = System.getProperties();
pro.put("mail.smtp.host", mail_host);
pro.put("mail.smtp.port", mail_port);
pro.put("mail.smtp.auth", "true");
Session sendMailSession = Session.getDefaultInstance(pro,
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
Message mailMessage = new MimeMessage(sendMailSession);
mailMessage.setFrom(new InternetAddress(userName));
mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(
to));
mailMessage.setSubject(subject);
mailMessage.setSentDate(sentDate);
mailMessage.setText(text);
Transport.send(mailMessage);
}
public void sendHtmlMail(List<GgCodeTransferDto> toList,
List<GgCodeTransferDto> ccList, String subject, Date sentDate,
String text) throws Exception {
Properties pro = System.getProperties();
pro.put("mail.smtp.host", mail_host);
pro.put("mail.smtp.port", mail_port);
pro.put("mail.smtp.auth", "true");
Session sendMailSession = Session.getDefaultInstance(pro,
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
Message mailMessage = new MimeMessage(sendMailSession);
mailMessage.setFrom(new InternetAddress(userName));
InternetAddress[] address = new InternetAddress[toList.size()];
InternetAddress[] addressCc = new InternetAddress[ccList.size()];
if (toList != null && toList.size() > 0) {
for (int i = 0; i < toList.size(); i++) {
address[i] = new InternetAddress(toList.get(i).getOuterCode());
}
}
if (ccList != null && ccList.size() > 0) {
for (int j = 0; j < ccList.size(); j++) {
addressCc[j] = new InternetAddress(ccList.get(j).getOuterCode());
}
}
if (address != null && address.length > 0) {
mailMessage.setRecipients(Message.RecipientType.TO, address);
if (addressCc != null && addressCc.length > 0) {
mailMessage.setRecipients(Message.RecipientType.CC, addressCc);
}
} else {
if (addressCc != null && addressCc.length > 0) {
mailMessage.setRecipients(Message.RecipientType.TO, addressCc);
}
}
mailMessage.setSubject(subject);
mailMessage.setSentDate(sentDate);
Multipart mainPart = new MimeMultipart();
BodyPart html = new MimeBodyPart();
html.setContent(text, "text/html; charset=utf-8");
mainPart.addBodyPart(html);
mailMessage.setContent(mainPart);
Transport.send(mailMessage);
}
public void sendHtmlMail2(String to, String cto, String subject,
Date sentDate, String text) throws Exception {
Properties pro = System.getProperties();
pro.put("mail.smtp.host", mail_host);
pro.put("mail.smtp.port", mail_port);
pro.put("mail.smtp.auth", "true");
Session sendMailSession = Session.getDefaultInstance(pro,
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
Message mailMessage = new MimeMessage(sendMailSession);
mailMessage.setFrom(new InternetAddress(userName));
InternetAddress[] address = new InternetAddress[1];
address[0] = new InternetAddress(to);
mailMessage.setRecipients(Message.RecipientType.TO, address);
if (cto != null) {
InternetAddress[] addressCc = new InternetAddress[1];
addressCc[0] = new InternetAddress(cto);
mailMessage.setRecipients(Message.RecipientType.CC, addressCc);
}
mailMessage.setSubject(subject);
mailMessage.setSentDate(sentDate);
Multipart mainPart = new MimeMultipart();
BodyPart html = new MimeBodyPart();
html.setContent(text, "text/html; charset=utf-8");
mainPart.addBodyPart(html);
mailMessage.setContent(mainPart);
Transport.send(mailMessage);
}
public void sendImageMail() throws Exception {
Properties pro = System.getProperties();
pro.put("mail.smtp.host", mail_host);
pro.put("mail.smtp.port", mail_port);
pro.put("mail.smtp.auth", "true");
Session sendMailSession = Session.getDefaultInstance(pro,
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
Message mailMessage = new MimeMessage(sendMailSession);
mailMessage.setFrom(new InternetAddress(userName));
mailMessage.setSubject("Test Email");
mailMessage.setSentDate(new Date());
MimeMultipart multipart = new MimeMultipart("related");
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<html><body><img src='cid:image'/><div>this is a HTML email.</div></body></html>";
messageBodyPart.setContent(htmlText, "text/html; charset=utf-8");
multipart.addBodyPart(messageBodyPart);
MimeBodyPart imageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource("1_jianggujin.jpg");
imageBodyPart.setDataHandler(new DataHandler(fds));
multipart.addBodyPart(imageBodyPart);
mailMessage.setContent(multipart);
Transport.send(mailMessage);
}
public void sendAttachmentMail() throws Exception {
Properties pro = System.getProperties();
pro.put("mail.smtp.host", mail_host);
pro.put("mail.smtp.port", mail_port);
pro.put("mail.smtp.auth", "true");
Session sendMailSession = Session.getDefaultInstance(pro,
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
Message mailMessage = new MimeMessage(sendMailSession);
mailMessage.setFrom(new InternetAddress(userName));
mailMessage.setSubject("Test Email");
mailMessage.setSentDate(new Date());
MimeMultipart multipart = new MimeMultipart("mixed");
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<html><body><div>this is a Attachment email.this email has a attachment!</div></body></html>";
messageBodyPart.setContent(htmlText, "text/html; charset=utf-8");
multipart.addBodyPart(messageBodyPart);
MimeBodyPart imageBodyPart = new MimeBodyPart();
File imageFile = new File("1_jianggujin.jpg");
DataSource fds = new FileDataSource(imageFile);
imageBodyPart.setDataHandler(new DataHandler(fds));
imageBodyPart.setFileName(MimeUtility.encodeWord(imageFile.getName()));
multipart.addBodyPart(imageBodyPart);
mailMessage.setContent(multipart);
Transport.send(mailMessage);
}
public void sendImageAndAttachmentMail() throws Exception {
Properties pro = System.getProperties();
pro.put("mail.smtp.host", mail_host);
pro.put("mail.smtp.port", mail_port);
pro.put("mail.smtp.auth", "true");
Session sendMailSession = Session.getDefaultInstance(pro,
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
Message mailMessage = new MimeMessage(sendMailSession);
mailMessage.setFrom(new InternetAddress(userName));
mailMessage.setSubject("Test Email");
mailMessage.setSentDate(new Date());
MimeBodyPart text = new MimeBodyPart();
text.setContent("我的头像:<img src='cid:headImg'>",
"text/html;charset=UTF-8");
MimeBodyPart image = new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource(
"1_jianggujin.jpg")));
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("1_jianggujin.jpg"));
attach.setDataHandler(dh);
attach.setFileName(MimeUtility.encodeWord(dh.getName()));
MimeMultipart mp1 = new MimeMultipart();
mp1.addBodyPart(text);
mp1.addBodyPart(image);
mp1.setSubType("related");
MimeBodyPart content = new MimeBodyPart();
content.setContent(mp1);
MimeMultipart multipart = new MimeMultipart("mixed");
multipart.addBodyPart(content);
multipart.addBodyPart(attach);
mailMessage.setContent(multipart);
Transport.send(mailMessage);
}
}