use of org.springframework.mail.javamail.MimeMessageHelper in project portal by ixinportal.
the class SendEmailImpl method sendEmail.
private void sendEmail(EmailServer emailServer, String mailContent, String[] toEmail, String subject, boolean isHtml) throws Exception {
if (emailServer.getIsAuth()) {
props.put("mail.smtp.auth", true);
}
props.put("mail.transport.protocol", "smtp");
mailSender.setJavaMailProperties(props);
mailSender.setUsername(emailServer.getAccountName());
mailSender.setPassword(emailServer.getAccountPasswd());
mailSender.setPort(emailServer.getServerPort());
mailSender.setHost(emailServer.getServerHost());
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, // 防止出现中文乱码,
"UTF-8");
// 设置发送方地址
message.setFrom(emailServer.getAccountName());
// 设置接收方的email地址
message.setTo(toEmail);
// 设置邮件主题
message.setSubject(subject);
message.setText(mailContent, isHtml);
mailSender.send(mimeMessage);
}
use of org.springframework.mail.javamail.MimeMessageHelper in project opennms by OpenNMS.
the class JavaSendMailer method buildMimeMessage.
/**
* Builds the mime message.
*
* @param msg the sendmail message
* @return the mime message
*/
public MimeMessage buildMimeMessage(final SendmailMessage msg) {
MimeMessage mimeMsg = new MimeMessage(m_session);
if (m_config.getSendmailMessage() != msg) {
m_config.setSendmailMessage(msg);
}
if (m_config.getSendmailMessage() != null) {
final SendmailMessage configMsg = m_config.getSendmailMessage();
try {
final String charset = m_config.getSendmailProtocol() != null ? m_config.getSendmailProtocol().getCharSet() : Charset.defaultCharset().name();
final MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, false, charset);
helper.setFrom(configMsg.getFrom());
helper.setTo(configMsg.getTo());
helper.setSubject(configMsg.getSubject());
} catch (final MessagingException e) {
LOG.warn("found a problem building message: {}", e.getMessage());
}
} else {
LOG.warn("Missing sendmail message configuration. This MIME message will probably be wrong.");
}
return mimeMsg;
}
use of org.springframework.mail.javamail.MimeMessageHelper in project fake-smtp-server by gessnerfl.
the class TestDataCreator method createHtmlEmail.
private static void createHtmlEmail(int i) {
try {
JavaMailSender sender = getEmailSender();
MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setTo("receiver@exmaple.com");
helper.setFrom("sender@example.com");
helper.setSubject("Test-Mail " + i);
helper.setText("<html><head></head><body>This is the test mail number " + i + "</body>", true);
sender.send(message);
} catch (MessagingException e) {
throw new RuntimeException("Failed to create mail", e);
}
}
use of org.springframework.mail.javamail.MimeMessageHelper in project PublicCMS-preview by sanluan.
the class SendTask method send.
/**
* @param toAddress
* @param fromAddress
* @param title
* @param content
* @param isHtml
* @return whether to send successfully
* @throws MessagingException
*/
private boolean send(short siteId, String toAddress, String title, String content, boolean isHtml) throws MessagingException {
Map<String, String> config = configComponent.getConfigData(siteId, CONFIG_CODE);
if (CommonUtils.notEmpty(config) && CommonUtils.notEmpty(config.get(CONFIG_FROMADDRESS))) {
JavaMailSender mailSender = getMailSender(siteId, config);
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(message, false, DEFAULT_CHARSET_NAME);
messageHelper.setTo(toAddress);
messageHelper.setFrom(config.get(CONFIG_FROMADDRESS));
messageHelper.setSubject(title);
messageHelper.setText(content, isHtml);
pool.execute(new SendTask(mailSender, message));
return true;
}
return false;
}
use of org.springframework.mail.javamail.MimeMessageHelper in project gravitee-management-rest-api by gravitee-io.
the class EmailServiceImpl method addResourcesInMessage.
private String addResourcesInMessage(final MimeMessageHelper mailMessage, final String htmlText) throws Exception {
final Document document = Jsoup.parse(htmlText);
final List<String> resources = new ArrayList<>();
final Elements imageElements = document.getElementsByTag("img");
resources.addAll(imageElements.stream().filter(imageElement -> imageElement.hasAttr("src")).map(imageElement -> {
final String src = imageElement.attr("src");
imageElement.attr("src", "cid:" + src);
return src;
}).collect(Collectors.toList()));
final String html = document.html();
mailMessage.setText(html, true);
for (final String res : resources) {
final FileSystemResource templateResource = new FileSystemResource(new File(templatesPath, res));
mailMessage.addInline(res, templateResource, MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(res));
}
return html;
}
Aggregations