Search in sources :

Example 11 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 12 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 13 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 14 with MimeMessageHelper

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

Example 15 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project SpringStepByStep by JavaProgrammerLB.

the class AccountEmailServiceImpl method sendMail.

public void sendMail(String to, String subject, String htmlText) throws AccountEmailException {
    try {
        MimeMessage msg = javaMailSender.createMimeMessage();
        MimeMessageHelper msgHelper = new MimeMessageHelper(msg);
        msgHelper.setFrom(systemEmail);
        msgHelper.setTo(to);
        msgHelper.setSubject(subject);
        msgHelper.setText(htmlText, true);
        javaMailSender.send(msg);
    } catch (MessagingException e) {
        throw new AccountEmailException("Faild to send mail.", e);
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Aggregations

MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)18 MimeMessage (javax.mail.internet.MimeMessage)17 MailException (org.springframework.mail.MailException)9 MessagingException (javax.mail.MessagingException)8 Date (java.util.Date)7 Properties (java.util.Properties)3 OpenClinicaSystemException (org.akaza.openclinica.exception.OpenClinicaSystemException)3 JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)3 SendmailMessage (org.opennms.netmgt.config.javamail.SendmailMessage)2 MimeMessagePreparator (org.springframework.mail.javamail.MimeMessagePreparator)2 Async (org.springframework.scheduling.annotation.Async)2 File (java.io.File)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Stream (java.util.stream.Stream)1 InternetAddress (javax.mail.internet.InternetAddress)1 JavaMailerException (org.opennms.javamail.JavaMailerException)1 JavaSendMailer (org.opennms.javamail.JavaSendMailer)1