use of javax.mail.internet.MimeMessage in project OpenClinica by OpenClinica.
the class QueryServiceImpl method sendEmail.
private void sendEmail(String to, String subject, String body, Boolean htmlEmail) throws Exception {
try {
JavaMailSenderImpl mailSender = (JavaMailSenderImpl) appContext.getBean("mailSender");
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(EmailEngine.getAdminEmail());
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.error("Email could not be sent on {} due to: {}", new Date(), me.getMessage());
}
}
use of javax.mail.internet.MimeMessage in project jdk8u_jdk by JetBrains.
the class MailTest method sendMail.
void sendMail() {
try {
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props);
session.setDebug(true);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipients(Message.RecipientType.TO, to);
message.setSubject("this is a multipart test");
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText("please send also this Content\n ciao!");
multipart.addBodyPart(messageBodyPart1);
BodyPart messageBodyPart2 = new MimeBodyPart();
messageBodyPart2.setContent("<b>please</b> send also this Content <br>ciao!", "text/html; charset=UTF-8");
multipart.addBodyPart(messageBodyPart2);
message.setContent(multipart);
/*
Transport tr = session.getTransport("smtp");
tr.connect(host,user, password);
tr.sendMessage(message,InternetAddress.parse(to));
tr.close();
*/
ByteArrayOutputStream baos = new ByteArrayOutputStream();
message.writeTo(baos);
String output = baos.toString();
System.out.println("output = " + output);
if (output.contains("also this Content")) {
System.out.println("Test PASSED.");
} else {
System.out.println("Test FAILED, missing content.");
throw new IllegalStateException("Test FAILED, missing content.");
}
} catch (MessagingException ignored) {
} catch (IOException ignored) {
}
}
use of javax.mail.internet.MimeMessage in project symmetric-ds by JumpMind.
the class MailService method sendEmail.
protected String sendEmail(String subject, String text, String recipients, Properties prop, String transportType, boolean useAuth, String user, String password) {
Session session = Session.getInstance(prop);
ByteArrayOutputStream ba = null;
if (log.isDebugEnabled()) {
session.setDebug(true);
ba = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(ba);
session.setDebugOut(ps);
}
Transport transport;
try {
transport = session.getTransport(transportType);
} catch (NoSuchProviderException e) {
log.error("Failure while obtaining transport", e);
return getNestedErrorMessage(e);
}
try {
if (useAuth) {
transport.connect(user, password);
} else {
transport.connect();
}
} catch (MessagingException e) {
log.error("Failure while connecting to transport", e);
return getNestedErrorMessage(e);
}
try {
MimeMessage message = new MimeMessage(session);
message.setSentDate(new Date());
message.setRecipients(RecipientType.BCC, recipients);
message.setSubject(subject);
message.setText(text);
try {
transport.sendMessage(message, message.getAllRecipients());
} catch (MessagingException e) {
log.error("Failure while sending notification", e);
return getNestedErrorMessage(e);
}
} catch (MessagingException e) {
log.error("Failure while preparing notification", e);
return e.getMessage();
} finally {
try {
transport.close();
} catch (MessagingException e) {
}
}
if (log.isDebugEnabled()) {
log.debug(ba.toString());
}
return null;
}
use of javax.mail.internet.MimeMessage in project GNS by MobilityFirst.
the class Email method simpleMail.
/**
*
* @param subject
* @param recipient
* @param text
* @param suppressWarning
* @return true if the mail was sent
*/
public static boolean simpleMail(String subject, String recipient, String text, boolean suppressWarning) {
try {
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
Properties props = new Properties();
props.setProperty("mail.smtp.ssl.enable", "true");
//props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
Session session = Session.getInstance(props);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(Config.getGlobalString(GNSConfig.GNSC.SUPPORT_EMAIL)));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
message.setSubject(subject);
message.setText(text);
SMTPTransport t = (SMTPTransport) session.getTransport("smtp");
try {
t.connect(SMTP_HOST, Config.getGlobalString(GNSConfig.GNSC.ADMIN_EMAIL), Config.getGlobalString(GNSConfig.GNSC.ADMIN_PASSWORD));
t.sendMessage(message, message.getAllRecipients());
getLogger().log(Level.FINE, "Email response: {0}", t.getLastServerResponse());
} finally {
t.close();
}
getLogger().log(Level.FINE, "Successfully sent email to {0} with message: {1}", new Object[] { recipient, text });
return true;
} catch (GeneralSecurityException | MessagingException e) {
if (!suppressWarning) {
getLogger().log(Level.WARNING, "Unable to send email: {0}", e);
}
return false;
}
}
use of javax.mail.internet.MimeMessage in project libresonic by Libresonic.
the class RecoverController method emailPassword.
/*
* e-mail user new password via configured Smtp server
*/
private boolean emailPassword(String password, String username, String email) {
/* Default to protocol smtp when SmtpEncryption is set to "None" */
String prot = "smtp";
if (settingsService.getSmtpServer() == null || settingsService.getSmtpServer().isEmpty()) {
LOG.warn("Can not send email; no Smtp server configured.");
return false;
}
Properties props = new Properties();
if (settingsService.getSmtpEncryption().equals("SSL/TLS")) {
prot = "smtps";
props.put("mail." + prot + ".ssl.enable", "true");
} else if (settingsService.getSmtpEncryption().equals("STARTTLS")) {
prot = "smtp";
props.put("mail." + prot + ".starttls.enable", "true");
}
props.put("mail." + prot + ".host", settingsService.getSmtpServer());
props.put("mail." + prot + ".port", settingsService.getSmtpPort());
/* use authentication when SmtpUser is configured */
if (settingsService.getSmtpUser() != null && !settingsService.getSmtpUser().isEmpty()) {
props.put("mail." + prot + ".auth", "true");
}
Session session = Session.getInstance(props, null);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(settingsService.getSmtpFrom()));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
message.setSubject("Libresonic Password");
message.setText("Hi there!\n\n" + "You have requested to reset your Libresonic password. Please find your new login details below.\n\n" + "Username: " + username + "\n" + "Password: " + password + "\n\n" + "--\n" + "Your Libresonic server\n" + "libresonic.org");
message.setSentDate(new Date());
Transport trans = session.getTransport(prot);
try {
if (props.get("mail." + prot + ".auth") != null && props.get("mail." + prot + ".auth").equals("true")) {
trans.connect(settingsService.getSmtpServer(), settingsService.getSmtpUser(), settingsService.getSmtpPassword());
} else {
trans.connect();
}
trans.sendMessage(message, message.getAllRecipients());
} finally {
trans.close();
}
return true;
} catch (Exception x) {
LOG.warn("Failed to send email.", x);
return false;
}
}
Aggregations