java队列发送邮件_Java Mail + 消息队列 高效率发送邮件

该博客介绍了如何使用Java Mail API结合SMTP服务器高效发送邮件。通过配置邮件基本属性,如SMTP主机、端口和授权信息,实现了SSL安全连接。并展示了在遇到异常时的日志记录策略,确保邮件发送过程的稳定性。
摘要由CSDN通过智能技术生成

packagecom.test.utils.mail;importjava.io.UnsupportedEncodingException;importjava.security.GeneralSecurityException;importjava.util.ArrayList;importjava.util.List;importjavax.mail.MessagingException;importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeMessage;importjavax.mail.internet.MimeUtility;importorg.apache.log4j.Logger;importorg.springframework.util.StringUtils;importcom.sun.mail.smtp.SMTPSendFailedException;importcom.sun.mail.util.MailSSLSocketFactory;importcom.test.constant.Config;importcom.test.utils.PropUtil;/*** @描述:

*

*@author作者 :**/

public classMailUtil {private static Logger logger = Logger.getLogger(MailUtil.class);public static booleansendMail(MailVo mailVo){boolean flag = false;

PropUtil pro= newPropUtil(Config.CONFIG_PATH);

String smtpHost= "";

String smtpport= "";

String from= "";

String password= "";

String to= "";

String subject= "";

String messageText= "";try{

smtpHost=pro.getValue(Config.MAIL_SMTP_HOST);

smtpport=pro.getValue(Config.MAIL_SMTP_PORT);

from=pro.getValue(Config.MAIL_SENDER);

password=pro.getValue(Config.MAIL_SMTP_PASSWORD);

to=mailVo.getReceiver();

subject=mailVo.getSubject();

messageText=mailVo.getContent();int sendFlag =sendMessage(smtpHost, smtpport, from, password, to, subject, messageText);

flag= true;if(sendFlag == 200){

logger.debug("邮件发送成功:发件人 = " + from + "; 收件人 = " + to + "; 主题 = " + subject + "; 内容 = " +messageText);

}else{

logger.debug("邮件发送失败:发件人 = " + from + "; 收件人 = " + to + "; 主题 = " + subject + "; 内容 = " +messageText);

}

}catch(Exception e) {

logger.debug("发送邮件失败,失败内容:smtpHost = " +smtpHost);

logger.debug("发送邮件失败,失败内容:smtpport = " +smtpport);

logger.debug("发送邮件失败,失败内容:from = " +from);

logger.debug("发送邮件失败,失败内容:password = " +password);

logger.debug("发送邮件失败,失败内容:to = " +to);

logger.debug("发送邮件失败,失败内容:smtpHost = " +smtpHost);

logger.debug("发送邮件失败,失败内容:subject = " +subject);

logger.debug("发送邮件失败,失败内容:messageText = " +messageText);

logger.error("发送邮件失败", e);

}returnflag;

}public static intsendMessage(String smtpHost,String smtpport,

String from, String password, String to,

String subject, String messageText){try{//1、配置邮件基本属性

java.util.Properties props = newjava.util.Properties();//2、判断是否需要开启SSL

if(!StringUtils.isEmpty(smtpport) && smtpport.equals("465")){

MailSSLSocketFactory sf;try{

sf= newMailSSLSocketFactory();

sf.setTrustAllHosts(true);

props.put("mail.smtp.ssl.enable", "true");//props.put("mail.smtp.ssl.checkserveridentity", "true");

props.put("mail.smtp.ssl.socketFactory", sf);

}catch(GeneralSecurityException e1) {

logger.error("开始SSL设置出错", e1);

}

}

props.setProperty("mail.smtp.auth", "true");//指定是否需要SMTP验证

props.setProperty("mail.smtp.host", smtpHost);//指定SMTP服务器

props.setProperty("mail.smtp.port", smtpport);//指定SMTP服务器端口

props.put("mail.transport.protocol", "smtp");

Session mailSession=Session.getInstance(props);

mailSession.setDebug(true);//是否在控制台显示debug信息//3、构建发送邮件的具体消息体

logger.debug("Constructing message - from=" + from + " to=" +to);

String nick= javax.mail.internet.MimeUtility.encodeText("邮件用户昵称,根据自己实际定义");//设置发送人

InternetAddress fromAddress = new InternetAddress(nick+" ");//设置接收人

InternetAddress toAddress;

toAddress= newInternetAddress(to);

MimeMessage testMessage= newMimeMessage(mailSession);try{

testMessage.setFrom(fromAddress);

}catch(MessagingException e) {

logger.error(e);return 535;

}try{

testMessage.addRecipient(javax.mail.Message.RecipientType.TO, toAddress);

}catch(MessagingException e) {

logger.error(e);return 534;

}

testMessage.setSentDate(newjava.util.Date());

testMessage.setSubject(MimeUtility.encodeText(subject,"gb2312","B"));//设置消息文本

testMessage.setContent(messageText, "text/html;charset=gb2312");

System.out.println("Message constructed");

Transport transport;

transport= mailSession.getTransport("smtp");//设置邮箱服务器 用户名 密码

transport.connect(smtpHost, from, password);

transport.sendMessage(testMessage, testMessage.getAllRecipients());

transport.close();return 200;

}catch(SMTPSendFailedException e){

logger.error(e);return 550;

}catch(MessagingException e) {

logger.error(e);return 500;

} catch(UnsupportedEncodingException e) {

logger.error(e);return 501;//文本消息有错

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值