in signature/src/main/java/com/chemistry/enotebook/signature/email/EmailSender.java [98:158]
private void sendEmail(String email, String subject, String messageText, Multipart attachement) {
final String username = this.emailUser;
final String password = this.emailPassword;
Properties props = new Properties();
props.put("mail.smtp.connectiontimeout", TIMEOUT);
props.put("mail.smtp.timeout", TIMEOUT);
props.put("mail.smtp.writetimeout", TIMEOUT);
props.put("mail.smtp.auth", emailAuth);
props.put("mail.smtp.host", emailSmtpHost);
props.put("mail.smtp.port", emailSmtpPort);
// If send causes "Could not convert socket to TLS" exception,
// add props.put("mail.smtp.ssl.trust", emailSmtpHost); for SSL or TLS
if (Boolean.valueOf(emailTls)) {
props.put("mail.smtp.starttls.enable", "true");
} else if (Boolean.valueOf(emailSsl)) {
props.put("mail.smtp.ssl.enable", "true");
}
Session session;
if (Boolean.valueOf(emailAuth)) {
session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
} else {
session = Session.getInstance(props);
}
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(emailFrom));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
message.setSubject(subject);
if (attachement != null) {
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(messageText);
attachement.addBodyPart(messageBodyPart, 0);
message.setContent(attachement);
} else {
message.setText(messageText);
}
Transport.send(message);
log.info("Email to " + email + " has been sent.");
} catch (Exception e) {
log.error("Error sending email to " + email + ": " + e.getMessage());
}
}