Search in sources :

Example 76 with MimeMessageHelper

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());
        }
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Async(org.springframework.scheduling.annotation.Async)

Example 77 with MimeMessageHelper

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);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 78 with MimeMessageHelper

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;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Date(java.util.Date) JobExecutionException(org.quartz.JobExecutionException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 79 with MimeMessageHelper

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);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 80 with MimeMessageHelper

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);
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) FileSystemResource(org.springframework.core.io.FileSystemResource) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) File(java.io.File)

Aggregations

MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)111 MimeMessage (javax.mail.internet.MimeMessage)103 MessagingException (javax.mail.MessagingException)47 Async (org.springframework.scheduling.annotation.Async)20 File (java.io.File)16 MailException (org.springframework.mail.MailException)16 FileSystemResource (org.springframework.core.io.FileSystemResource)12 Date (java.util.Date)11 JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)11 InternetAddress (javax.mail.internet.InternetAddress)10 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)9 MimeMessagePreparator (org.springframework.mail.javamail.MimeMessagePreparator)9 MailSendException (org.springframework.mail.MailSendException)7 IOException (java.io.IOException)6 Template (freemarker.template.Template)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Properties (java.util.Properties)5 DataSource (javax.activation.DataSource)5 Test (org.junit.Test)5