调用如下:
public boolean sendMail() throws Exception {
List receivers = new ArrayList();
receivers.add("sunhao0550@163.com");
receivers.add("sunhao0550@sina.cn");
EmailServer.getInstance().sendHtmlMail("message_admin@163.com", "这里是密码", "测试发送HTML邮件",
"测试发送HTML邮件链接到百度", receivers);
return EmailServer.getInstance().sendTextMail("message_admin@163.com", "这里是密码", "测试发送文本邮件",
"测试发送文本邮件测试发送文本邮件测试发送文本邮件", receivers);
}
PS:正在考虑扩展,如果把这几个类封在jar包中,如何进行邮件服务器配置的扩展.
如有不好之处,欢迎拍砖.
1.[图片] 调用方式.png
2.[代码][Java]代码
package com.message.base.email;
import com.message.base.spring.ApplicationHelper;
import com.message.base.utils.StringUtils;
import com.message.base.utils.ValidateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Properties;
/**
* 发送邮件服务器.
*
* @author sunhao(sunhao.java@gmail.com)
* @version V1.0, 13-3-25 上午6:19
*/
public class EmailServer {
private static final Logger logger = LoggerFactory.getLogger(EmailServer.class);
//spring中配置
/**邮箱服务器配置**/
private List config;
/**是否开启debug调试模式**/
private boolean isDebug = false;
/**是否启用身份验证**/
private boolean isAuth = true;
/**验证类型**/
private String authType = "auth";
/**
* 私有化默认构造器,使外部不可实例化
*/
private EmailServer(){}
/**
* 单例,保证上下文中只有一个EmailServer
*
* @return EmailServer
*/
public static EmailServer getInstance(){
return ApplicationHelper.getInstance().getBean(EmailServer.class);
}
/**
* 发送普通邮件(单个接收人)
*
* @param username 发件人用户名
* @param password 发件人密码
* @param title 邮件标题
* @param content 邮件正文
* @param receiver 单个接收人
* @return
*/
public boolean sendTextMail(String username, String password, String title, String content, String receiver){
return this.sendTextMail(username, password, title, content, Collections.singletonList(receiver));
}
/**
* 发送普通邮件(多个接收人)
*
* @param username 发件人用户名
* @param password 发件人密码
* @param title 邮件标题
* @param content 邮件正文
* @param receivers 多个接收人
* @return
*/
public boolean sendTextMail(String username, String password, String title, String content, List receivers){
Authentication auth = null;
if(this.isAuth()){
//如果需要身份认证,则创建一个密码验证器
auth = new Authentication(username, password);
}
Properties props = new Properties();
props.setProperty("mail.smtp.auth", this.isAuth() ? "true" : "false");
props.setProperty("mail.transport.protocol", "auth");
EmailConfig config = this.getEmailConfig(username);
props.setProperty("mail.smtp.host", config.getSmtp());
props.setProperty("mail.smtp.port", config.getPort() + "");
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(this.isDebug);
Message message = this.makeTextMail(session, title, content, username, receivers, false);
try {
Transport.send(message);
return true;
} catch (MessagingException e) {
logger.error(e.getMessage(), e);
return false;
}
}
/**
* 发送HTML邮件(单个接收人)
*
* @param username 发件人用户名
* @param password 发件人密码
* @param title 邮件标题
* @param content 邮件正文
* @param receiver 单个接收人
* @return
*/
public boolean sendHtmlMail(String username, String password, String title, String content, String receiver){
return this.sendHtmlMail(username, password, title, content, Collections.singletonList(receiver));
}
/**
* 发送HTML邮件(多个接收人)
*
* @param username 发件人用户名
* @param password 发件人密码
* @param title 邮件标题
* @param content 邮件正文
* @param receivers 多个接收人
* @return
*/
public boolean sendHtmlMail(String username, String password, String title, String content, List receivers){
Authentication auth = null;
if(this.isAuth()){
//如果需要身份认证,则创建一个密码验证器
auth = new Authentication(username, password);
}
Properties props = new Properties();
props.setProperty("mail.smtp.auth", this.isAuth() ? "true" : "false");
props.setProperty("mail.transport.protocol", "auth");
EmailConfig config = this.getEmailConfig(username);
props.setProperty("mail.smtp.host", config.getSmtp());
props.setProperty("mail.smtp.port", config.getPort() + "");
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(this.isDebug);
Message message = this.makeTextMail(session, title, content, username, receivers, true);
try {
Transport.send(message);
return true;
} catch (MessagingException e) {
logger.error(e.getMessage(), e);
return false;
}
}
/**
* 获取邮件服务器配置
*
* @param email 邮箱地址
* @return
*/
private EmailConfig getEmailConfig(String email){
String mailServiceDomainName = this.getMailServiceDomainName(email);
for(EmailConfig config : this.config) {
if(config.getName().equals(mailServiceDomainName)){
return config;
}
}
return null;
}
/**
* 创建邮件message
*
* @param session 根据配置获得的session
* @param title 邮件主题
* @param content 邮件的内容
* @param from 发件者
* @param receivers 收件者
* @param isHtmlMail 是否是html邮件
*/
private Message makeTextMail(Session session, String title, String content, String from, List receivers, boolean isHtmlMail){
Message message = new MimeMessage(session);
try {
/**标题**/
logger.info("this mail's title is {}", title);
message.setSubject(title);
/**内容**/
logger.info("this mail's content is {}", content);
if(isHtmlMail){
//是html邮件
message.setContent(content, "text/html;charset=utf-8");
} else {
//普通邮件
message.setText(content);
}
/**发件者地址**/
logger.info("this mail's sender is {}", from);
Address fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);
/**接收者地址**/
Address[] tos = new InternetAddress[receivers.size()];
for(int i = 0; i < receivers.size(); i++){
String receiver = receivers.get(i);
if(ValidateUtils.isEmail(receiver)){
tos[i] = new InternetAddress(receiver);
}
}
/**发件时间**/
message.setSentDate(new Date());
message.setRecipients(Message.RecipientType.TO, tos);
} catch (MessagingException e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
return message;
}
/**
* 获取邮箱域名
*
* @param email 邮箱
* @return
*/
private String getMailServiceDomainName(String email){
if(StringUtils.isEmpty(email)){
return "";
} else {
int firstIndex = StringUtils.indexOf(email, "@");
int secondIndex = StringUtils.lastIndexOf(email, ".");
return StringUtils.substring(email, firstIndex + 1, secondIndex);
}
}
public List getConfig() {
return config;
}
public void setConfig(List config) {
this.config = config;
}
public boolean isDebug() {
return isDebug;
}
public void setDebug(boolean debug) {
isDebug = debug;
}
public boolean isAuth() {
return isAuth;
}
public void setAuth(boolean auth) {
isAuth = auth;
}
public String getAuthType() {
return authType;
}
public void setAuthType(String authType) {
this.authType = authType;
}
}
3.[文件] email.zip ~ 26KB 下载(264)