java发送邮件

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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 org.apache.log4j.Logger;

public class MailSend {
/**
* 以文本格式发送邮件
*
* @param mailInfo
* 待发送的邮件的信息
*/
private static Logger logger = Logger.getLogger(MailSend.class);

public boolean sendTextMail(MailUserInfo mailInfo) {
MailAuth authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new MailAuth(mailInfo.getUserName(),
mailInfo.getPassword());
}
Session sendMailSession = Session
.getDefaultInstance(pro, authenticator);
try {
Message mailMessage = new MimeMessage(sendMailSession);
Address from = new InternetAddress(mailInfo.getFromAddress());
mailMessage.setFrom(from);
Address[] to = mailInfo.getToAddress();
mailMessage.addRecipients(Message.RecipientType.TO, to);
mailMessage.setSubject(mailInfo.getSubject());
mailMessage.setSentDate(new Date());
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
logger.error(ex.getMessage(), ex);
}
return false;
}

/**
* 以HTML格式发送邮件
*
* @param mailInfo
* 待发送的邮件信息
*/
@SuppressWarnings("rawtypes")
public boolean sendHtmlMail(MailUserInfo mailInfo) {
MailAuth authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new MailAuth(mailInfo.getUserName(),
mailInfo.getPassword());
}
Session sendMailSession = Session
.getDefaultInstance(pro, authenticator);
try {
Message mailMessage = new MimeMessage(sendMailSession);
Address from = new InternetAddress(mailInfo.getFromAddress());
mailMessage.setFrom(from);
Address[] to = mailInfo.getToAddress();
mailMessage.setRecipients(Message.RecipientType.TO, to);
mailMessage.setSubject(mailInfo.getSubject());
mailMessage.setSentDate(new Date());
Multipart mainPart = new MimeMultipart();
BodyPart html = new MimeBodyPart();
html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
mailMessage.setContent(mainPart);
ArrayList files = new ArrayList();
files = mailInfo.getFilesName();
for (int i = 0; i < files.size(); i++) {
html = new MimeBodyPart();
String filename = (String) files.get(i);
FileDataSource fds = new FileDataSource(filename);
html.setDataHandler(new DataHandler(fds)); // 得到附件本身并至入BodyPart
html.setFileName(fds.getName()); // 得到文件名同样至入BodyPart
mainPart.addBodyPart(html);
}
// 发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
logger.error(ex.getMessage(), ex);
}
return false;
}

public static void send(String content, String subject, Object files,
List<String> toAddress) {
// 这个类主要是设置邮件
MailUserInfo mailInfo = new MailUserInfo();

mailInfo.setMailServerHost((String) PropertyUtil
.getProperty("mail_host"));
mailInfo.setMailServerPort((String) PropertyUtil
.getProperty("mail_port"));
mailInfo.setValidate(Boolean.getBoolean((String) PropertyUtil
.getProperty("mail_validate")));
mailInfo.setUserName((String) PropertyUtil.getProperty("mail_username"));
mailInfo.setPassword((String) PropertyUtil.getProperty("mail_password"));
mailInfo.setFromAddress((String) PropertyUtil.getProperty("mail_from"));
if (files != null) {
mailInfo.setFilesName(files);
}

mailInfo.setToAddress(toAddress);
mailInfo.setSubject(subject);
mailInfo.setContent(content);
MailSend sms = new MailSend();
sms.sendHtmlMail(mailInfo);

}
}

原文地址:https://www.cnblogs.com/csk-1/p/4996617.html