diff --git a/pom.xml b/pom.xml index 0ba1fda..b979371 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,11 @@ uoa-user-management 2.0.0-SNAPSHOT + + javax.mail + mail + 1.4 + diff --git a/src/main/java/eu/dnetlib/openaire/usermanagement/email/EmailSender.java b/src/main/java/eu/dnetlib/openaire/usermanagement/email/EmailSender.java new file mode 100644 index 0000000..7de744a --- /dev/null +++ b/src/main/java/eu/dnetlib/openaire/usermanagement/email/EmailSender.java @@ -0,0 +1,70 @@ +package eu.dnetlib.openaire.usermanagement.email; + +import org.apache.log4j.Logger; + +import javax.mail.*; + +import javax.mail.internet.AddressException; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; +import java.util.Properties; + +/** + * Created by kiatrop on 9/10/2017. + */ +public class EmailSender { + + private static String username; + private static String password; + private static String host; + private static String port; + private static String from; + + Logger logger = Logger.getLogger(EmailSender.class); + + public void sendEmail(String recipient, String subject, String body) { + + // Get system properties + Properties properties = System.getProperties(); + properties.setProperty("mail.smtp.host", host); + properties.put("mail.smtp.port", port); + properties.put("mail.smtp.auth", "true"); //enable authentication + properties.put("mail.smtp.starttls.enable", "true"); + + Session session = javax.mail.Session.getInstance(properties, + new Authenticator() { + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + + try { + // Create a default MimeMessage object. + MimeMessage message = new MimeMessage(session); + + // Set From: header field of the header. + message.setFrom(new InternetAddress(from)); + + // Set To: header field of the header. + message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient)); + + // Set Subject: header field + message.setSubject(subject); + + // For simple text setText() can be used instead of setContent() + + // Send the actual HTML message, as big as you like + message.setContent(body, "text/html"); + + // Send message + Transport.send(message); + logger.debug("Sent message successfully....\n"); + + } catch (AddressException ae) { + logger.error("Email could not be send.", ae); + + } catch (MessagingException me) { + logger.error("Email could not be send.", me); + } + } +}