use of org.springframework.mail.javamail.JavaMailSenderImpl 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.JavaMailSenderImpl in project webapp by elimu-ai.
the class Mailer method sendHtmlWithButton.
public static void sendHtmlWithButton(String to, String cc, String from, String subject, String title, String text, String buttonText, String buttonUrl) {
logger.info("sendHtmlWithButton");
if (to.contains(",")) {
to = to.replace(",", "");
}
if (to.contains(":")) {
to = to.replace(":", "");
}
JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl();
String smtpHost = ConfigHelper.getProperty("smtp.host");
javaMailSenderImpl.setHost(smtpHost);
MimeMessage mimeMessage = javaMailSenderImpl.createMimeMessage();
try {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setFrom(from);
mimeMessageHelper.setTo(to);
if (StringUtils.isNotBlank(cc)) {
mimeMessageHelper.setCc(cc);
}
mimeMessageHelper.setBcc(ADMIN_EMAIL);
mimeMessageHelper.setSubject(subject);
String html = "";
ResourceLoader resourceLoader = new ClassRelativeResourceLoader(Mailer.class);
logger.info("Loading file email_template_button.html...");
Resource resource = resourceLoader.getResource("email_template_button.html");
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
inputStream = resource.getInputStream();
inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains("${subject}")) {
line = line.replace("${subject}", subject);
}
if (line.contains("${title}")) {
line = line.replace("${title}", title);
}
if (line.contains("${text}")) {
line = line.replace("${text}", text);
}
if (line.contains("${buttonText}")) {
line = line.replace("${buttonText}", buttonText);
}
if (line.contains("${buttonUrl}")) {
line = line.replace("${buttonUrl}", buttonUrl);
}
html += line;
}
} catch (IOException ex) {
logger.error(null, ex);
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
logger.error(null, e);
}
}
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (IOException e) {
logger.error(null, e);
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
logger.error(null, e);
}
}
}
mimeMessageHelper.setText(text, html);
logger.info("Sending MIME message to " + to + " with subject \"" + subject + "\"...");
logger.info("title: " + title);
logger.info("text: " + text);
logger.info("buttonText: " + buttonText);
logger.info("buttonUrl: " + buttonUrl);
if (EnvironmentContextLoaderListener.env != Environment.DEV) {
javaMailSenderImpl.send(mimeMessage);
}
} catch (MessagingException ex) {
logger.error(null, ex);
}
}
use of org.springframework.mail.javamail.JavaMailSenderImpl in project webapp by elimu-ai.
the class Mailer method sendHtml.
public static void sendHtml(String to, String cc, String from, String subject, String title, String text) {
logger.info("sendPlainText");
if (to.contains(",")) {
to = to.replace(",", "");
}
if (to.contains(":")) {
to = to.replace(":", "");
}
JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl();
String smtpHost = ConfigHelper.getProperty("smtp.host");
javaMailSenderImpl.setHost(smtpHost);
MimeMessage mimeMessage = javaMailSenderImpl.createMimeMessage();
try {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setFrom(from);
mimeMessageHelper.setTo(to);
if (StringUtils.isNotBlank(cc)) {
mimeMessageHelper.setCc(cc);
}
mimeMessageHelper.setBcc(ADMIN_EMAIL);
mimeMessageHelper.setSubject(subject);
String html = "";
ResourceLoader resourceLoader = new ClassRelativeResourceLoader(Mailer.class);
logger.info("Loading file email_template.html...");
Resource resource = resourceLoader.getResource("email_template.html");
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
inputStream = resource.getInputStream();
inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains("${subject}")) {
line = line.replace("${subject}", subject);
} else if (line.contains("${title}")) {
line = line.replace("${title}", title);
} else if (line.contains("${text}")) {
line = line.replace("${text}", text);
}
html += line;
}
} catch (IOException ex) {
logger.error(null, ex);
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
logger.error(null, e);
}
}
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (IOException e) {
logger.error(null, e);
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
logger.error(null, e);
}
}
}
mimeMessageHelper.setText(text, html);
logger.info("Sending MIME message to " + to + " with subject \"" + subject + "\"...");
logger.info("title: " + title);
logger.info("text: " + text);
if (EnvironmentContextLoaderListener.env != Environment.DEV) {
javaMailSenderImpl.send(mimeMessage);
}
} catch (MessagingException ex) {
logger.error(null, ex);
}
}
use of org.springframework.mail.javamail.JavaMailSenderImpl in project transporter by wang4ever.
the class MailSenderService method afterPropertiesSet.
@Override
public void afterPropertiesSet() throws Exception {
try {
this.javaMailSender = new JavaMailSenderImpl();
this.javaMailSender.setDefaultEncoding("UTF-8");
this.javaMailSender.setHost(host);
this.javaMailSender.setUsername(sender);
this.javaMailSender.setPassword(passwd);
Properties props = new Properties();
// 这里要用字符串类型,不能用Boolean类型的true
props.put("mail.smtp.auth", this.smtpAuth.toString());
props.put("mail.smtp.timeout", this.smtpTimeout);
this.javaMailSender.setJavaMailProperties(props);
} catch (Exception e) {
logger.error("EmailSenderService初始化失败.", e);
}
}
use of org.springframework.mail.javamail.JavaMailSenderImpl in project alf.io by alfio-event.
the class SmtpMailer method toMailSender.
private JavaMailSender toMailSender(Event event) {
JavaMailSenderImpl r = new CustomJavaMailSenderImpl();
r.setDefaultEncoding("UTF-8");
r.setHost(configurationManager.getRequiredValue(Configuration.from(event.getOrganizationId(), event.getId(), SMTP_HOST)));
r.setPort(Integer.valueOf(configurationManager.getRequiredValue(Configuration.from(event.getOrganizationId(), event.getId(), SMTP_PORT))));
r.setProtocol(configurationManager.getRequiredValue(Configuration.from(event.getOrganizationId(), event.getId(), SMTP_PROTOCOL)));
r.setUsername(configurationManager.getStringConfigValue(Configuration.from(event.getOrganizationId(), event.getId(), SMTP_USERNAME), null));
r.setPassword(configurationManager.getStringConfigValue(Configuration.from(event.getOrganizationId(), event.getId(), SMTP_PASSWORD), null));
String properties = configurationManager.getStringConfigValue(Configuration.from(event.getOrganizationId(), event.getId(), SMTP_PROPERTIES), null);
if (properties != null) {
try {
Properties prop = PropertiesLoaderUtils.loadProperties(new EncodedResource(new ByteArrayResource(properties.getBytes(StandardCharsets.UTF_8)), "UTF-8"));
r.setJavaMailProperties(prop);
} catch (IOException e) {
log.warn("error while setting the mail sender properties", e);
}
}
return r;
}
Aggregations