Added basic EmailSender draft

This commit is contained in:
Katerina Iatropoulou 2017-10-09 11:36:07 +00:00
parent 2272f14978
commit 93d6c4b3e2
2 changed files with 75 additions and 0 deletions

View File

@ -44,6 +44,11 @@
<artifactId>uoa-user-management</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
</project>

View File

@ -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);
}
}
}