1、 接口
publicinterfaceMailSendServDu {
publicvoidsendEmail(String addressee, String subject, String content);
}
2、 实现
importjavax.mail.*;
importjavax.mail.internet.InternetAddress;
importjavax.mail.internet.MimeMessage;
importjava.util.Date;
importjava.util.Map;
importjava.util.Properties;
/**
* 邮件发送接口
* @param
* @author Wu Jianguo
* @version V1.0
* @Description:
* @modificationHistory=================重大变更调整记录
* @modify by user: Wu Jianguo
* @modify by reason:{方法名}:{原因}
* @return
* @throws
*/
publicclassMailSendServDuImplimplementsMailSendServDu{
privateLogger logger = Logger.getLogger(this.getClass());
// 初始化连接邮件服务器的会话信息
privateProperties properties =null;
// 创建Session实例对象
privateSession session =null;
String fromAddress = null;
String fromName = null;
String charset = null;
publicMailSendServDuImpl() {
logger.info("发送邮件相关配置初始化.......");
Map map = PropertiesUtils.getPropertiesValues("/properties/mail.properties");
String turnon = map.get("mail.turnon");
if(Boolean.parseBoolean(turnon)) {
String protocol = map.get("mail.protocol");
fromAddress = map.get("mail.fromAddress");
fromName = map.get("mail.fromName");
String host = map.get("mail.host");
String port = map.get("mail.port");
String auth = map.get("mail.auth");
String username = map.get("mail.username");
String password = map.get("mail.password");
String debug = map.get("mail.debug");
charset = map.get("mail.charset");
properties = newProperties();
properties.setProperty("mail.transport.protocol", protocol);
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.smtp.port", port);
properties.setProperty("mail.smtp.auth", auth);
properties.setProperty("mail.debug", debug);
if(Boolean.parseBoolean(auth)) {
session = Session.getDefaultInstance(properties, newHatomAuthenticator(username, password));
} else{
session = Session.getDefaultInstance(properties, newHatomAuthenticator());
}
}
}
@Override
publicvoidsendEmail(String addressee, String subject, String content) {
logger.info("发送邮件");
MailSendServDuImpl ps = newMailSendServDuImpl();
try{
if(null!= properties) {
// 创建MimeMessage实例对象
MimeMessage message = newMimeMessage(session);
// 设置发件人
message.setFrom(newInternetAddress(fromAddress, fromName));
// 设置邮件主题
message.setSubject(subject);
// 设置收件人
message.setRecipient(Message.RecipientType.TO, newInternetAddress(addressee));
// 设置发送时间
message.setSentDate(newDate());
// 设置html内容为邮件正文,指定MIME类型为text/html类型,并指定字符编码
message.setContent(content, "text/html;charset="+ charset);
// 保存并生成最终的邮件内容
message.saveChanges();
// 发送邮件
Transport.send(message);
}
} catch(Exception e) {
System.err.println(e.getMessage());
logger.info("发送邮件异常");
}
}
/**
* 向邮件服务器提交认证信息
*/
classHatomAuthenticatorextendsAuthenticator {
privateString username;
privateString password;
publicHatomAuthenticator() {
super();
}
publicHatomAuthenticator(String username, String password) {
super();
this.username = username;
this.password = password;
}
@Override
protectedPasswordAuthentication getPasswordAuthentication() {
returnnewPasswordAuthentication(username, password);
}
}
}
3、 配置文件
# 是否打开邮件发送
mail.turnon=true
# 邮件发送协议
mail.protocol=smtp
# 发信邮箱
mail.fromAddress=XXXX@163.com
# 发信人
mail.fromName=XX
# smtp端口号
mail.host=smtp.163.com
mail.port=25
# 是否需要验证
mail.auth=true
# smtp账号
mail.username=XXXX@163.com
# smtp密码
mail.password=
# 调试级别,0关闭,1一般,2较高
mail.debug=0
# 编码
mail.charset=UTF-8