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";
}
}
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;
}
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());
}
}
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;
}
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);
}
}
Aggregations