use of org.springframework.mail.javamail.MimeMessageHelper in project bamboobsc by billchen198318.
the class MailClientUtils method send.
public static void send(String from, String to, String[] cc, String[] bcc, String[] fileNames, File[] files, String subject, String text) throws MailException, Exception {
if (mailSender == null) {
throw new Exception("null mailSender!");
}
if (StringUtils.isBlank(from) || StringUtils.isBlank(to)) {
throw new Exception("from and to is required!");
}
if (fileNames != null && files != null) {
if (fileNames.length != files.length) {
throw new Exception("File parameter error!");
}
}
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, Constants.BASE_ENCODING);
helper.setFrom(from);
helper.setTo(to.endsWith(";") ? to.substring(0, to.length() - 1) : to);
helper.setSubject(subject);
helper.setText(text, true);
if (null != cc && cc.length > 0) {
helper.setCc(cc);
}
if (null != bcc && bcc.length > 0) {
helper.setBcc(bcc);
}
for (int i = 0; fileNames != null && i < fileNames.length; i++) {
helper.addAttachment(fileNames[i], files[i]);
}
mailSender.send(message);
}
use of org.springframework.mail.javamail.MimeMessageHelper 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.MimeMessageHelper 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.MimeMessageHelper in project ArachneCentralAPI by OHDSI.
the class ArachneMailSender method send.
public void send(ArachneMailMessage mailMessage) {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(message, true);
helper.setSubject(mailMessage.getSubject());
helper.setFrom(from, mailMessage.getFromPersonal());
helper.setTo(mailMessage.getUser().getEmail());
helper.setText(buildContent(mailMessage.getTemplate(), mailMessage.getParameters()), true);
mailSender.send(message);
} catch (MailConnectException e) {
LOG.error(e.getMessage(), e);
throw new MailSendException(e.getMessage());
} catch (MessagingException | UnsupportedEncodingException ex) {
LOG.error(ex.getMessage(), ex);
} catch (MailSendException e) {
LOG.error(e.getMessage(), e);
throw new MailSendException("Failed to send e-mail. Please, contact to the administrator.");
}
}
use of org.springframework.mail.javamail.MimeMessageHelper in project uplace.es by Uplace.
the class MailService method sendEmail.
@Async
public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) throws MessagingException {
log.debug("Send email[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}", isMultipart, isHtml, to, subject, content);
// Prepare message using a Spring helper
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, CharEncoding.UTF_8);
message.setTo(to);
message.setFrom(jHipsterProperties.getMail().getFrom());
message.setSubject(subject);
message.setText(content, isHtml);
javaMailSender.send(mimeMessage);
log.debug("Sent email to User '{}'", to);
}
Aggregations