首页 > 代码库 > Java发邮件简单实现
Java发邮件简单实现
给出一个Java发送邮件的简单实现。
下载
Java代码
1、 接口
public interface MailSendServDu {
public void sendEmail(String addressee, String subject, String content);
}
2、 实现
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Map;
import java.util.Properties;
/**
* 邮件发送接口
* @param
* @author Wu Jianguo
* @version V1.0
* @Description:
* @modificationHistory=================重大变更调整记录
* @modify by user: Wu Jianguo
* @modify by reason:{方法名}:{原因}
* @return
* @throws
*/
public class MailSendServDuImpl implements MailSendServDu{
private Logger logger = Logger.getLogger(this.getClass());
// 初始化连接邮件服务器的会话信息
private Properties properties = null;
// 创建Session实例对象
private Session session = null;
String fromAddress = null;
String fromName = null;
String charset = null;
public MailSendServDuImpl() {
logger.info("发送邮件相关配置初始化.......");
Map<String,String> 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 = new Properties();
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, new HatomAuthenticator(username, password));
} else {
session = Session.getDefaultInstance(properties, new HatomAuthenticator());
}
}
}
@Override
public void sendEmail(String addressee, String subject, String content) {
logger.info("发送邮件");
MailSendServDuImpl ps = new MailSendServDuImpl();
try {
if (null != properties) {
// 创建MimeMessage实例对象
MimeMessage message = new MimeMessage(session);
// 设置发件人
message.setFrom(new InternetAddress(fromAddress, fromName));
// 设置邮件主题
message.setSubject(subject);
// 设置收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress(addressee));
// 设置发送时间
message.setSentDate(new Date());
// 设置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("发送邮件异常");
}
}
下载
/**
* 向邮件服务器提交认证信息
*/
class HatomAuthenticator extends Authenticator {
private String username;
private String password;
public HatomAuthenticator() {
super();
}
public HatomAuthenticator(String username, String password) {
super();
this.username = username;
this.password = password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(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
Java发邮件简单实现