Search in sources :

Example 16 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project uPortal by Jasig.

the class EmailPasswordResetNotificationImpl method sendNotification.

@Override
public void sendNotification(URL resetUrl, ILocalAccountPerson account, Locale locale) {
    log.debug("Sending password reset instructions to user with url {}", resetUrl.toString());
    try {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        String email = (String) account.getAttributeValue(ILocalAccountPerson.ATTR_MAIL);
        String subject = messageSource.getMessage(subjectMessageKey, new Object[] {}, locale);
        String body = formatBody(resetUrl, account, locale);
        helper.addTo(email);
        helper.setText(body, true);
        helper.setSubject(subject);
        helper.setFrom(portalEmailAddress, messageSource.getMessage("portal.name", new Object[] {}, locale));
        log.debug("Sending message to {} from {} subject {}", email, Arrays.toString(message.getFrom()), message.getSubject());
        this.mailSender.send(helper.getMimeMessage());
    } catch (Exception e) {
        log.error("Unable to send password reset email", e);
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 17 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project OpenClinica by OpenClinica.

the class SystemController method sendEmail.

public String sendEmail(JavaMailSenderImpl mailSender, String emailSubject, String message) throws OpenClinicaSystemException {
    logger.info("Sending email...");
    try {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
        helper.setFrom(EmailEngine.getAdminEmail());
        helper.setTo("oc123@openclinica.com");
        helper.setSubject(emailSubject);
        helper.setText(message);
        mailSender.send(mimeMessage);
        return "ACTIVE";
    } catch (MailException me) {
        return "INACTIVE";
    } catch (MessagingException me) {
        return "INACTIVE";
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) MailException(org.springframework.mail.MailException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 18 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project OpenClinica by OpenClinica.

the class SecureController method sendEmail.

public Boolean sendEmail(String to, String from, String subject, String body, Boolean htmlEmail, String successMessage, String failMessage, Boolean sendMessage) throws Exception {
    Boolean messageSent = true;
    try {
        JavaMailSenderImpl mailSender = (JavaMailSenderImpl) SpringServletAccess.getApplicationContext(context).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);
        }
        logger.debug("Email sent successfully on {}", new Date());
    } catch (MailException me) {
        me.printStackTrace();
        if (failMessage != null && sendMessage) {
            addPageMessage(failMessage);
        }
        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 MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project opennms by OpenNMS.

the class JavaMailDeliveryService method deliverReport.

/* (non-Javadoc)
     * @see org.opennms.netmgt.reporting.service.ReportDeliveryService#deliverReport(org.opennms.netmgt.config.reportd.Report, java.lang.String)
     */
@Override
public void deliverReport(Report report, String fileName) throws ReportDeliveryException {
    try {
        SendmailConfig config = null;
        if (report.getMailer().isPresent()) {
            final String mailer = report.getMailer().get();
            LOG.debug("deliverReport with mailer={}", mailer);
            config = m_JavamailConfigDao.getSendMailConfig(mailer);
        } else {
            LOG.debug("deliverReport with default sendmail config");
            config = m_JavamailConfigDao.getDefaultSendmailConfig();
        }
        JavaSendMailer sm = new JavaSendMailer(config);
        MimeMessage msg = new MimeMessage(sm.getSession());
        if (config.getSendmailMessage() != null && config.getSendmailProtocol() != null) {
            final SendmailMessage sendmailMessage = config.getSendmailMessage();
            final SendmailProtocol sendmailProtocol = config.getSendmailProtocol();
            MimeMessageHelper helper = new MimeMessageHelper(msg, true, sendmailProtocol.getCharSet());
            helper.setFrom(sendmailMessage.getFrom());
            helper.setTo(report.getRecipients().toArray(new String[0]));
            helper.setSubject("OpenNMS Report: " + report.getReportName());
            if ("text/html".equals(sendmailProtocol.getMessageContentType().toLowerCase())) {
                helper.setText(sendmailMessage.getBody().replaceAll("\\<[^>]*>", ""), sendmailMessage.getBody());
            } else {
                helper.setText(sendmailMessage.getBody());
            }
            helper.addAttachment(fileName, new File(fileName));
            sm.send(msg);
        } else {
            LOG.error("sendmail-message or sendmail-protocol is not configured!");
        }
    } catch (JavaMailerException e) {
        LOG.error("Problem with JavaMailer {}", e.getMessage(), e);
        throw new ReportDeliveryException("Caught JavaMailerException: " + e.getMessage());
    } catch (MessagingException e) {
        LOG.error("Problem with Messaging {}", e.getMessage(), e);
        throw new ReportDeliveryException("Caught MessagingException: " + e.getMessage());
    } catch (Throwable e) {
        LOG.error("Unexpected exception: {}", e.getMessage(), e);
        throw new ReportDeliveryException("Caught unexpected " + e.getClass().getName() + ": " + e.getMessage());
    }
}
Also used : SendmailProtocol(org.opennms.netmgt.config.javamail.SendmailProtocol) MimeMessage(javax.mail.internet.MimeMessage) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) MessagingException(javax.mail.MessagingException) JavaSendMailer(org.opennms.javamail.JavaSendMailer) SendmailConfig(org.opennms.netmgt.config.javamail.SendmailConfig) JavaMailerException(org.opennms.javamail.JavaMailerException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) File(java.io.File)

Example 20 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project OpenClinica by OpenClinica.

the class OpenClinicaMailSender method sendEmail.

public void sendEmail(String to, String from, String subject, String body, Boolean htmlEmail) throws OpenClinicaSystemException {
    try {
        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);
        logger.debug("Email sent successfully on {}", new Date());
    } catch (MailException me) {
        logger.debug("Email could not be sent on {} due to: {}", new Date(), me.toString());
        throw new OpenClinicaSystemException(me.getMessage());
    } catch (MessagingException e) {
        logger.debug("Email could not be sent on {} due to: {}", new Date(), e.toString());
        throw new OpenClinicaSystemException(e.getMessage());
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) MailException(org.springframework.mail.MailException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) Date(java.util.Date)

Aggregations

MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)26 MimeMessage (javax.mail.internet.MimeMessage)24 MessagingException (javax.mail.MessagingException)12 MailException (org.springframework.mail.MailException)9 Date (java.util.Date)7 Properties (java.util.Properties)5 JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)5 Async (org.springframework.scheduling.annotation.Async)5 OpenClinicaSystemException (org.akaza.openclinica.exception.OpenClinicaSystemException)3 MimeMessagePreparator (org.springframework.mail.javamail.MimeMessagePreparator)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 SendmailMessage (org.opennms.netmgt.config.javamail.SendmailMessage)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)2 Service (org.springframework.stereotype.Service)2 Checks (com.khartec.waltz.common.Checks)1 IOUtilities (com.khartec.waltz.common.IOUtilities)1 StringUtilities (com.khartec.waltz.common.StringUtilities)1 OpcBizException (com.paascloud.provider.exceptions.OpcBizException)1