Spring中javaMail通过SMTP发送邮件

时间:2021-05-19 14:17:15

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;
}

}