代码:
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import org.apache.commons.lang3.StringUtils;
public class EmailUtil {
/**
* 发送邮件
* @param to 接收邮件邮箱号
* @param cc 抄送邮件邮箱号
* @param subject 邮件标题
* @param content 邮件内容
* @return
*/
public boolean sendEmail(String to, String cc, String subject, String content) {
if (StringUtils.isEmpty(to)) {
return false;
}
EmailConfig config = createEmailConfig(to, cc);
try {
return send(config, subject, content);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private static EmailConfig createEmailConfig(String to, String cc) {
EmailConfig config = new EmailConfig();
config.setHost("发送邮件服务器");
config.setPort("服务器发送端口");
config.setEmail("系统使用邮箱号");
config.setUserName("系统发送邮件用户名");
config.setPassword("系统发送邮件所用邮箱密码");
config.setToEmails("接收邮件邮箱号");
config.setCcEmails("抄送邮件邮箱号");
return config;
}
private static boolean send(EmailConfig config, String subject, String content) {
Session session = EmailUtil.creatSession(config);
try {
MimeMessage message = new MimeMessage(session);
// 设置邮件头及编码格式
subject = MimeUtility.encodeWord(subject, "UTF-8", "Q");
message.setSubject(subject);
// 邮件署名
message.setFrom(new InternetAddress(config.getEmail(), config.getUserName()));
// 消息接收者(收件地址)
if (StringUtils.isNotBlank(config.getToEmails())) {
String[] emails = config.getToEmails().split(",");
InternetAddress[] addresses = new InternetAddress[emails.length];
for (int i = 0; i < emails.length; i++) {
addresses[i] = new InternetAddress(emails[i]);
}
message.addRecipients(Message.RecipientType.TO, addresses);
}
// 消息抄送者(收件地址)
if (StringUtils.isNotBlank(config.getCcEmails())) {
String[] emails = config.getCcEmails().split(",");
InternetAddress[] addresses = new InternetAddress[emails.length];
for (int i = 0; i < emails.length; i++) {
addresses[i] = new InternetAddress(emails[i]);
}
message.addRecipients(Message.RecipientType.CC, addresses);
}
message.saveChanges();
// 设置邮件内容及编码格式
// 后一个参数可以不指定编码,如"text/plain",但是将不能显示中文字符
message.setContent(content, "text/plain;charset=UTF-8");
// 发送
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
private static Session creatSession(EmailConfig config) {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "SMTP");
props.setProperty("mail.smtp.host", config.getHost());
props.setProperty("mail.smtp.port", config.getPort());
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.ssl.enable", "true");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.socketFactory.port", config.getPort());
props.setProperty("mail.smtp.connectiontimeout", "5000");
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 登录用户名密码
return new PasswordAuthentication(config.getEmail(), config.getPassword());
}
});
// session.setDebug(true);
return session;
}
}