Search in sources :

Example 16 with MailException

use of org.springframework.mail.MailException in project scoold by Erudika.

the class ScooldEmailer method sendEmail.

@Override
public boolean sendEmail(final List<String> emails, final String subject, final String body) {
    if (emails == null || emails.isEmpty()) {
        return false;
    }
    asyncExecute(() -> {
        emails.forEach(email -> {
            try {
                mailSender.send((MimeMessage mimeMessage) -> {
                    MimeMessageHelper msg = new MimeMessageHelper(mimeMessage);
                    msg.setTo(email);
                    msg.setSubject(subject);
                    msg.setFrom(Config.SUPPORT_EMAIL, Config.APP_NAME);
                    // body is assumed to be HTML
                    msg.setText(body, true);
                });
                logger.debug("Email sent to {}, {}", email, subject);
            } catch (MailException ex) {
                logger.error("Failed to send email to {} with body [{}]. {}", email, body, ex.getMessage());
            }
        });
    });
    return true;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MailException(org.springframework.mail.MailException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 17 with MailException

use of org.springframework.mail.MailException in project spring-framework by spring-projects.

the class JavaMailSenderImpl method send.

@Override
public void send(MimeMessagePreparator... mimeMessagePreparators) throws MailException {
    try {
        List<MimeMessage> mimeMessages = new ArrayList<>(mimeMessagePreparators.length);
        for (MimeMessagePreparator preparator : mimeMessagePreparators) {
            MimeMessage mimeMessage = createMimeMessage();
            preparator.prepare(mimeMessage);
            mimeMessages.add(mimeMessage);
        }
        send(mimeMessages.toArray(new MimeMessage[0]));
    } catch (MailException ex) {
        throw ex;
    } catch (MessagingException ex) {
        throw new MailParseException(ex);
    } catch (Exception ex) {
        throw new MailPreparationException(ex);
    }
}
Also used : MailPreparationException(org.springframework.mail.MailPreparationException) MimeMessage(jakarta.mail.internet.MimeMessage) MessagingException(jakarta.mail.MessagingException) ArrayList(java.util.ArrayList) MailParseException(org.springframework.mail.MailParseException) MailException(org.springframework.mail.MailException) MailParseException(org.springframework.mail.MailParseException) NoSuchProviderException(jakarta.mail.NoSuchProviderException) MailAuthenticationException(org.springframework.mail.MailAuthenticationException) MessagingException(jakarta.mail.MessagingException) MailSendException(org.springframework.mail.MailSendException) MailPreparationException(org.springframework.mail.MailPreparationException) MailException(org.springframework.mail.MailException) AuthenticationFailedException(jakarta.mail.AuthenticationFailedException)

Example 18 with MailException

use of org.springframework.mail.MailException in project OpenClinica by OpenClinica.

the class CoreSecureController method sendEmail.

public Boolean sendEmail(String to, String from, String subject, String body, Boolean htmlEmail, String successMessage, String failMessage, Boolean sendMessage, HttpServletRequest request) throws Exception {
    Boolean messageSent = true;
    try {
        JavaMailSenderImpl mailSender = (JavaMailSenderImpl) SpringServletAccess.getApplicationContext(getServletContext()).getBean("mailSender");
        // @pgawade 09-Feb-2012 #issue 13201 - setting the "mail.smtp.localhost" property to localhost when java API is not able to
        // retrieve the host name
        Properties javaMailProperties = mailSender.getJavaMailProperties();
        if (null != javaMailProperties) {
            if (javaMailProperties.get("mail.smtp.localhost") == null || ((String) javaMailProperties.get("mail.smtp.localhost")).equalsIgnoreCase("")) {
                javaMailProperties.put("mail.smtp.localhost", "localhost");
            }
        }
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, htmlEmail);
        helper.setFrom(from);
        helper.setTo(processMultipleImailAddresses(to.trim()));
        helper.setSubject(subject);
        helper.setText(body, true);
        mailSender.send(mimeMessage);
        if (successMessage != null && sendMessage) {
            addPageMessage(successMessage, request);
        }
        LOGGER.debug("Email sent successfully on {}", new Date());
    } catch (MailException me) {
        me.printStackTrace();
        if (failMessage != null && sendMessage) {
            addPageMessage(failMessage, request);
        }
        LOGGER.debug("Email could not be sent on {} due to: {}", new Date(), me.toString());
        messageSent = false;
    }
    return messageSent;
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) MimeMessage(javax.mail.internet.MimeMessage) MailException(org.springframework.mail.MailException) Properties(java.util.Properties) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Date(java.util.Date)

Example 19 with MailException

use of org.springframework.mail.MailException in project zhcet-web by zhcet-amu.

the class EmailVerificationHelper method sendMail.

/**
 * Sends mail to the user to be verified and saves the user with the email and status to be unverified
 * Also, publishes an event notifying the same
 * @param token {@link VerificationToken} token to be sent email with respect to
 */
@Async
public void sendMail(VerificationToken token) {
    String relativeUrl = "/login/email/verify?auth=" + token.getToken();
    log.debug("Verification link generated : {}", relativeUrl);
    try {
        linkMailService.sendEmail(getPayLoad(token.getEmail(), token.getUser(), relativeUrl), false);
        // Now we set the email to the user and disable email verified
        User user = token.getUser();
        log.debug("Saving user email {} -> {}", user.getUserId(), token.getEmail());
        user.setEmail(token.getEmail());
        user.setEmailVerified(false);
        userService.save(user);
        log.debug("Saved user email");
        eventPublisher.publishEvent(new EmailVerifiedEvent(user));
    } catch (MailException mailException) {
        // There was an error sending the mail, so remove the token and sent time
        verificationTokenRepository.delete(token);
        emailCache.invalidate(token.getUser().getUserId());
        log.warn("Email sending for {} '{}' failed, so we removed the verification token", token.getUser(), token.getEmail(), mailException);
    }
}
Also used : User(amu.zhcet.data.user.User) MailException(org.springframework.mail.MailException) Async(org.springframework.scheduling.annotation.Async)

Example 20 with MailException

use of org.springframework.mail.MailException in project uhgroupings by uhawaii-system-its-ti-iam.

the class EmailService method send.

public void send(Feedback feedback) {
    logger.info("Feedback received in EmailService: " + feedback);
    if (isEnabled) {
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setTo(to);
        msg.setFrom(from);
        String text = "";
        String header = "UH Groupings service feedback [" + feedback.getType() + "]";
        text += "Submitted name: " + feedback.getName() + "\n\n";
        text += "Submitted email: <" + feedback.getEmail() + ">\n\n";
        text += "Feedback type: " + feedback.getType() + "\n\n";
        text += "--------------------------" + "\n\n";
        text += "Feedback: " + feedback.getMessage() + "\n\n";
        if (!feedback.getExceptionMessage().isEmpty()) {
            text += "Stack Trace: " + feedback.getExceptionMessage();
        }
        msg.setText(text);
        msg.setSubject(header);
        try {
            javaMailSender.send(msg);
        } catch (MailException ex) {
            logger.error("Error", ex);
        }
    }
}
Also used : SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MailException(org.springframework.mail.MailException)

Aggregations

MailException (org.springframework.mail.MailException)47 MimeMessage (javax.mail.internet.MimeMessage)18 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)17 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)15 MessagingException (javax.mail.MessagingException)10 Date (java.util.Date)8 Properties (java.util.Properties)3 InternetAddress (javax.mail.internet.InternetAddress)3 OpenClinicaSystemException (org.akaza.openclinica.exception.OpenClinicaSystemException)3 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 AuthenticationFailedException (jakarta.mail.AuthenticationFailedException)2 MessagingException (jakarta.mail.MessagingException)2 NoSuchProviderException (jakarta.mail.NoSuchProviderException)2 MimeMessage (jakarta.mail.internet.MimeMessage)2 PrintWriter (java.io.PrintWriter)2 URI (java.net.URI)2 Matcher (java.util.regex.Matcher)2 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)2 MolgenisDataException (org.molgenis.data.MolgenisDataException)2