use of org.springframework.mail.javamail.MimeMessageHelper in project hello-world by haoziapple.
the class MailService method sendEmail.
@Async
public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) {
log.debug("Send email[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}", isMultipart, isHtml, to, subject, content);
// Prepare message using a Spring helper
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, CharEncoding.UTF_8);
message.setTo(to);
message.setFrom(jHipsterProperties.getMail().getFrom());
message.setSubject(subject);
message.setText(content, isHtml);
javaMailSender.send(mimeMessage);
log.debug("Sent email to User '{}'", to);
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.warn("Email could not be sent to user '{}'", to, e);
} else {
log.warn("Email could not be sent to user '{}': {}", to, e.getMessage());
}
}
}
use of org.springframework.mail.javamail.MimeMessageHelper in project wombat by PLOS.
the class EmailMessage method send.
public void send(JavaMailSender mailSender) {
for (InternetAddress toEmailAddress : toEmailAddresses) {
mailSender.send((MimeMessage mimeMessage) -> {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, encoding);
message.setTo(toEmailAddress);
if (bccAddress.isPresent()) {
message.setBcc(bccAddress.get());
}
message.setFrom(senderAddress);
message.setSubject(subject);
mimeMessage.setContent(content);
});
log.debug("Mail sent to: {}", toEmailAddress);
}
}
use of org.springframework.mail.javamail.MimeMessageHelper in project syncope by apache.
the class DefaultNotificationJobDelegate method executeSingle.
@Transactional
@Override
public TaskExec executeSingle(final NotificationTask task) {
TaskExec execution = entityFactory.newEntity(TaskExec.class);
execution.setTask(task);
execution.setStart(new Date());
boolean retryPossible = true;
if (StringUtils.isBlank(task.getSubject()) || task.getRecipients().isEmpty() || StringUtils.isBlank(task.getHtmlBody()) || StringUtils.isBlank(task.getTextBody())) {
String message = "Could not fetch all required information for sending e-mails:\n" + task.getRecipients() + "\n" + task.getSender() + "\n" + task.getSubject() + "\n" + task.getHtmlBody() + "\n" + task.getTextBody();
LOG.error(message);
execution.setStatus(NotificationJob.Status.NOT_SENT.name());
retryPossible = false;
if (task.getTraceLevel().ordinal() >= TraceLevel.FAILURES.ordinal()) {
execution.setMessage(message);
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("About to send e-mails:\n" + task.getRecipients() + "\n" + task.getSender() + "\n" + task.getSubject() + "\n" + task.getHtmlBody() + "\n" + task.getTextBody() + "\n");
}
status.set("Sending notifications to " + task.getRecipients());
for (String to : task.getRecipients()) {
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setFrom(task.getSender());
helper.setSubject(task.getSubject());
helper.setText(task.getTextBody(), task.getHtmlBody());
mailSender.send(message);
execution.setStatus(NotificationJob.Status.SENT.name());
StringBuilder report = new StringBuilder();
switch(task.getTraceLevel()) {
case ALL:
report.append("FROM: ").append(task.getSender()).append('\n').append("TO: ").append(to).append('\n').append("SUBJECT: ").append(task.getSubject()).append('\n').append('\n').append(task.getTextBody()).append('\n').append('\n').append(task.getHtmlBody()).append('\n');
break;
case SUMMARY:
report.append("E-mail sent to ").append(to).append('\n');
break;
case FAILURES:
case NONE:
default:
}
if (report.length() > 0) {
execution.setMessage(report.toString());
}
notificationManager.createTasks(AuditElements.EventCategoryType.TASK, "notification", null, "send", AuditElements.Result.SUCCESS, null, null, task, "Successfully sent notification to " + to);
} catch (Exception e) {
LOG.error("Could not send e-mail", e);
execution.setStatus(NotificationJob.Status.NOT_SENT.name());
if (task.getTraceLevel().ordinal() >= TraceLevel.FAILURES.ordinal()) {
execution.setMessage(ExceptionUtils2.getFullStackTrace(e));
}
notificationManager.createTasks(AuditElements.EventCategoryType.TASK, "notification", null, "send", AuditElements.Result.FAILURE, null, null, task, "Could not send notification to " + to, e);
}
execution.setEnd(new Date());
}
}
if (hasToBeRegistered(execution)) {
execution = notificationManager.storeExec(execution);
if (retryPossible && (NotificationJob.Status.valueOf(execution.getStatus()) == NotificationJob.Status.NOT_SENT)) {
handleRetries(execution);
}
} else {
notificationManager.setTaskExecuted(execution.getTask().getKey(), true);
}
return execution;
}
use of org.springframework.mail.javamail.MimeMessageHelper in project Flare-event-calendar by PollubCafe.
the class EmailSenderImpl method send.
public void send(HtmlEmail email) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED, StandardCharsets.UTF_8.name());
helper.setFrom(email.getFrom());
helper.setTo(email.getTo());
helper.setSubject(email.getSubject());
helper.setText(email.getHtmlContent(), true);
mailSender.send(message);
}
use of org.springframework.mail.javamail.MimeMessageHelper in project spring-boot-examples by ityouknow.
the class MailServiceImpl method sendInlineResourceMail.
/**
* 发送正文中有静态资源(图片)的邮件
* @param to
* @param subject
* @param content
* @param rscPath
* @param rscId
*/
public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);
mailSender.send(message);
logger.info("嵌入静态资源的邮件已经发送。");
} catch (MessagingException e) {
logger.error("发送嵌入静态资源的邮件时发生异常!", e);
}
}
Aggregations