java web邮件发送
Posted 十一路客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java web邮件发送相关的知识,希望对你有一定的参考价值。
一. 相关配置
.properties文件
## mail
mail.smtp.host=smtp.XXXXX.com
mail.smtp.user=XXXXXXX@XXXX.com
mail.smtp.password=XXXXXX
mail.smtp.port=465
mail.smtp.auth=true
mail.smtp.timeout=25000
mail.smtp.ssl.enable=true
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.port=465
mail.smtp.socketFactory.fallback=false
mail.recipient.cc=XXXXXXXXXX@XXXXX.com
配置类
@Component
public class AppConfig
// mail
@Value("$mail.smtp.host")
private String mailHost;
@Value("$mail.smtp.user")
private String mailUser;
@Value("$mail.smtp.password")
private String mailPassword;
@Value("$mail.smtp.port")
private String mailPort;
@Value("$mail.smtp.auth")
private String mailAuth;
@Value("$mail.smtp.timeout")
private String mailTimeout;
@Value("$mail.smtp.ssl.enable")
private String mailSsl;
@Value("$mail.recipient.cc")
private String mailCc;
public String getMailHost()
return mailHost;
public void setMailHost(String mailHost)
this.mailHost = mailHost;
public String getMailUser()
return mailUser;
public void setMailUser(String mailUser)
this.mailUser = mailUser;
public String getMailPassword()
return mailPassword;
public void setMailPassword(String mailPassword)
this.mailPassword = mailPassword;
public String getMailPort()
return mailPort;
public void setMailPort(String mailPort)
this.mailPort = mailPort;
public String getMailAuth()
return mailAuth;
public void setMailAuth(String mailAuth)
this.mailAuth = mailAuth;
public String getMailTimeout()
return mailTimeout;
public void setMailTimeout(String mailTimeout)
this.mailTimeout = mailTimeout;
public String getMailSsl()
return mailSsl;
public void setMailSsl(String mailSsl)
this.mailSsl = mailSsl;
public String getMailCc()
return mailCc;
public void setMailCc(String mailCc)
this.mailCc = mailCc;
二. 邮件发送
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.google.common.base.Strings;
@Component
public class MailUtil implements InitializingBean
@Autowired
private AppConfig appConfig;
private static final String SMTP = "smtp";
private static final String CONTENT_TYPE = "text/html;charset=utf-8";
private static final List<String> CC = new ArrayList<String>();
private static final Logger logger = Logger.getLogger(MailUtil.class);
public void sendMail(List<String> toList, List<String> cc, String subject, String body)
throws AddressException, MessagingException
Properties props = System.getProperties();
props.put("mail.smtp.ssl.enable", appConfig.getMailSsl());
props.put("mail.smtp.host", appConfig.getMailHost());
props.put("mail.smtp.user", appConfig.getMailUser());
props.put("mail.smtp.password", appConfig.getMailPassword());
props.put("mail.smtp.port", appConfig.getMailPort());
props.put("mail.smtp.auth", appConfig.getMailAuth());
Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(appConfig.getMailUser()));
logger.info("mail subject : " + subject);
List<String> to = new ArrayList<String>();
for (int i = 0; i < toList.size(); i++) //去重
String tempMail = toList.get(i);
logger.info("toList mail i = " + i + " : " + tempMail);
if(!to.contains(tempMail))
to.add(tempMail);
logger.info("after removing the repeat mail, to list size: " + to.size());
logger.info("is in dev mode: " + appConfig.isInDevelopMode());
logger.info("is in prod mode: " + appConfig.isInProductionMode());
//20160202 in product
if(appConfig.isInProductionMode())
for (int i = 0; i < to.size(); i++)
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to.get(i)));
if (cc != null)
for (int i = 0; i < cc.size(); i++)
message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc.get(i)));
//没什么用 注释掉
/*for (int i = 0; i < CC.size(); i++)
message.addRecipient(Message.RecipientType.CC, new InternetAddress(CC.get(i)));
*/
//20160202 in develop
if(appConfig.isInDevelopMode())
//message.addRecipient(Message.RecipientType.TO, new InternetAddress("chexian-manager@alibaba-inc.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(Constants.ADMIN_EMAIL));
message.setSubject(subject);
message.setContent(body, CONTENT_TYPE);
Transport transport = session.getTransport(SMTP);
transport.connect(appConfig.getMailHost(), appConfig.getMailUser(), appConfig.getMailPassword());
transport.sendMessage(message, message.getAllRecipients());
transport.close();
@Override
public void afterPropertiesSet() throws Exception
// TODO Auto-generated method stub
if (!Strings.isNullOrEmpty(appConfig.getMailCc()))
String []cc = appConfig.getMailCc().split(";");
for (int i = 0; i < cc.length; i++)
CC.add(cc[i].trim());
以上是关于java web邮件发送的主要内容,如果未能解决你的问题,请参考以下文章