common mail sender
This commit is contained in:
parent
373275063a
commit
4c7f4ca4c7
|
@ -18,14 +18,6 @@
|
|||
|
||||
<dependencies>
|
||||
|
||||
<!-- Mail -->
|
||||
<dependency>
|
||||
<groupId>javax.mail</groupId>
|
||||
<artifactId>mail</artifactId>
|
||||
<version>1.4.7</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- Openaire -->
|
||||
<dependency>
|
||||
<groupId>eu.dnetlib.dhp</groupId>
|
||||
|
|
|
@ -24,6 +24,12 @@
|
|||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Mail -->
|
||||
<dependency>
|
||||
<groupId>javax.mail</groupId>
|
||||
<artifactId>mail</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- for /metrics and /health controllers -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
package eu.dnetlib.common.exceptions;
|
||||
|
||||
public class DnetException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1318223089824261627L;
|
||||
|
||||
public DnetException(final String message, final Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public DnetException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
package eu.dnetlib.common.utils.mail;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.mail.Authenticator;
|
||||
import javax.mail.PasswordAuthentication;
|
||||
import javax.mail.Session;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import eu.dnetlib.common.exceptions.DnetException;
|
||||
|
||||
public class EmailFactory {
|
||||
|
||||
private String smtpHost = "localhost";
|
||||
private int smtpPort = 587;
|
||||
private String smtpUser = null;
|
||||
private String smtpPassword = "";
|
||||
|
||||
public EmailMessage prepareEmail(final String subject,
|
||||
final String content,
|
||||
final String fromMail,
|
||||
final String fromName,
|
||||
final String to,
|
||||
final String... ccs) throws DnetException {
|
||||
|
||||
final Session session = Session.getInstance(obtainProperties(), obtainAuthenticator());
|
||||
|
||||
try {
|
||||
return new EmailMessage(session, subject, content, fromMail, fromName, to, ccs);
|
||||
} catch (final Exception e) {
|
||||
throw new DnetException("Error preparing mail message", e);
|
||||
}
|
||||
}
|
||||
|
||||
private Properties obtainProperties() {
|
||||
final Properties p = new Properties();
|
||||
p.put("mail.transport.protocol", "smtp");
|
||||
p.put("mail.smtp.host", getSmtpHost());
|
||||
p.put("mail.smtp.port", getSmtpPort());
|
||||
p.put("mail.smtp.auth", Boolean.toString(StringUtils.isNotBlank(getSmtpUser())));
|
||||
return p;
|
||||
}
|
||||
|
||||
private Authenticator obtainAuthenticator() {
|
||||
if (StringUtils.isBlank(getSmtpUser())) { return null; }
|
||||
|
||||
return new Authenticator() {
|
||||
|
||||
private final PasswordAuthentication authentication =
|
||||
new PasswordAuthentication(getSmtpUser(), getSmtpPassword());
|
||||
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return authentication;
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
public String getSmtpHost() {
|
||||
return smtpHost;
|
||||
}
|
||||
|
||||
public void setSmtpHost(final String smtpHost) {
|
||||
this.smtpHost = smtpHost;
|
||||
}
|
||||
|
||||
public int getSmtpPort() {
|
||||
return smtpPort;
|
||||
}
|
||||
|
||||
public void setSmtpPort(final int smtpPort) {
|
||||
this.smtpPort = smtpPort;
|
||||
}
|
||||
|
||||
public String getSmtpUser() {
|
||||
return smtpUser;
|
||||
}
|
||||
|
||||
public void setSmtpUser(final String smtpUser) {
|
||||
this.smtpUser = smtpUser;
|
||||
}
|
||||
|
||||
public String getSmtpPassword() {
|
||||
return smtpPassword;
|
||||
}
|
||||
|
||||
public void setSmtpPassword(final String smtpPassword) {
|
||||
this.smtpPassword = smtpPassword;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package eu.dnetlib.common.utils.mail;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.mail.Message;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.Transport;
|
||||
import javax.mail.internet.AddressException;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
public class EmailMessage {
|
||||
|
||||
private static final Log log = LogFactory.getLog(EmailMessage.class);
|
||||
|
||||
private final MimeMessage mimeMessage;
|
||||
|
||||
protected EmailMessage(final Session session, final String subject, final String content, final String fromMail, final String fromName, final String to,
|
||||
final String... ccs)
|
||||
throws MessagingException, UnsupportedEncodingException, AddressException {
|
||||
|
||||
this.mimeMessage = new MimeMessage(session);
|
||||
|
||||
this.mimeMessage.setFrom(new InternetAddress(fromMail, fromName));
|
||||
this.mimeMessage.setSubject(subject);
|
||||
this.mimeMessage.setContent(content, "text/html; charset=utf-8");
|
||||
this.mimeMessage.setSentDate(new Date());
|
||||
|
||||
this.mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
|
||||
|
||||
for (final String cc : ccs) {
|
||||
this.mimeMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
|
||||
}
|
||||
}
|
||||
|
||||
public void sendMailAsync() {
|
||||
final Thread t = new Thread(() -> sendMail());
|
||||
t.start();
|
||||
}
|
||||
|
||||
public void sendMail() {
|
||||
try {
|
||||
log.info("Sending mail to " + Arrays.toString(mimeMessage.getAllRecipients()) + "...");
|
||||
Transport.send(this.mimeMessage);
|
||||
log.info("...sent");
|
||||
} catch (final MessagingException e) {
|
||||
log.error("Error sending mail", e);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue