Spring中javaMail通过SMTP发送邮件

Posted 蓝色天空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring中javaMail通过SMTP发送邮件相关的知识,希望对你有一定的参考价值。

public final class Emails {

/** The java mail sender. */
private static JavaMailSender javaMailSender;

/**
* Send.
*
* @param emailData
* the email data
*/
public static void send(EmailConfig.EmailData emailData) {
EmailOrSmsLogEntity emailOrSmsLogEntity = new EmailOrSmsLogEntity();
emailOrSmsLogEntity.setMessageType(MessageType.Email);
emailOrSmsLogEntity.revodeIp();
try {
JavaMailSender javaMailSender = mailDefaultSender();
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(message,
true, Constants.DEFAULT_ENCODING);
messageHelper.setFrom(emailData.getFrom());
emailOrSmsLogEntity.setFrom(emailData.getFrom());
messageHelper.setTo(emailData.to());
emailOrSmsLogEntity.setTo(StringUtils.join(emailData.to(), ","));
messageHelper.setSubject(emailData.getTitle());
emailOrSmsLogEntity.setSubject(emailData.getTitle());
messageHelper.setText(emailData.getContent(), true);
emailOrSmsLogEntity.setText(emailData.getContent());
Collection<File> files = emailData.getAttachment();
if (files != null) {
List<String> fileNames = Lists.newArrayList();
for (File file : files) {
messageHelper.addAttachment(file.getName(), file);
fileNames.add(file.getName());
}
emailOrSmsLogEntity.addFeatures(EmailOrSmsLogEntity.FILETAG,
StringUtils.join(fileNames, ","));
}
javaMailSender.send(message);
emailOrSmsLogEntity.setSuccess(true);
} catch (Exception e) {
e.printStackTrace();
emailOrSmsLogEntity.setSuccess(false);
emailOrSmsLogEntity.setFailure(Throwables.getStackTraceAsString(e));
}
emailOrSmsLogEntity.createOrUpdate();
}

/**
* Aysn send.
*
* @param emailData
* the email data
*/
public static void aysnSend(final EmailConfig.EmailData emailData) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
send(emailData);
}
});
}

/**
* Mail default sender.
*
* @return the java mail sender
*/
public static JavaMailSender mailDefaultSender() {
return mailSender(SysConfigEntity.config());
}

/**
* Mail sender.
*
* @param config
* the config
* @return the java mail sender
*/
private static JavaMailSender mailSender(EmailConfig config) {
JavaMailSender javaMailSender = mailSender();
if (javaMailSender instanceof JavaMailSenderImpl && config != null) {
JavaMailSenderImpl impl = (JavaMailSenderImpl) javaMailSender;
if (config.getMailHost() != null)
impl.setHost(config.getMailHost());
if (config.getMailPost() != null)
impl.setPort(config.getMailPost());
if (config.getMailUserName() != null)
impl.setUsername(config.getMailUserName());
if (config.getMailPassWord() != null)
impl.setPassword(CryptoUtils.aesDecryptFromHex(
config.getMailPassWord(), KeyConstants.mailPassWord));
}
return javaMailSender;
}

/**
* Mail sender.
*
* @return the java mail sender
*/
private static JavaMailSender mailSender() {
if (javaMailSender != null)
return javaMailSender;
synchronized (Emails.class) {
javaMailSender = Utils.Container.getBean(JavaMailSender.class);
}
return javaMailSender;
}

}

以上是关于Spring中javaMail通过SMTP发送邮件的主要内容,如果未能解决你的问题,请参考以下文章

Java编程系列使用JavaMail通过SMTP协议发送局域网(内网)邮件

Spring JavaMail发送邮件

JavaMail---邮件发送

java中使用javamail发送邮件

[Java] JavaMail 简单案例

JavaMail邮件发送