java发信_Java发邮件简单实现

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值