Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,17 @@
</dependency>
<!-- Mail Service -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<groupId>org.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
<version>8.12.4</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<artifactId>jakarta.mail</artifactId>
<version>2.0.1</version>
</dependency>


<!--Logging: will transitively add logback core and slf4j-api-->
<dependency>
<groupId>ch.qos.logback</groupId>
Expand Down
48 changes: 24 additions & 24 deletions src/main/java/org/reactome/server/tools/indexer/util/MailUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import org.simplejavamail.api.mailer.Mailer;
import org.simplejavamail.api.email.Email;
import org.simplejavamail.email.EmailBuilder;
import org.simplejavamail.mailer.MailerBuilder;

/**
* Mail Utility class.
* Mail Utility class using Simple Java Mail.
*
* Note: If your SMTP server requires authentication,
* supply the username and password to withSMTPServer(...).
*
* @author Guilherme S. Viteri <gviteri@ebi.ac.uk>
*/
public class MailUtil {
private static final Logger logger = LoggerFactory.getLogger("importLogger");
private static MailUtil mailUtil;
private Properties properties;
private final Mailer mailer;

private MailUtil(String host, Integer port) {
properties = new Properties();
properties.setProperty("mail.smpt.host", host);
properties.setProperty("mail.smtp.port", String.valueOf(port));
// Configure the Mailer using the SMTP host and port.
// If no authentication is required, pass null for username and password.
this.mailer = MailerBuilder
.withSMTPServer(host, port, null, null)
.buildMailer();
}

public static MailUtil getInstance(String host, Integer port) {
Expand All @@ -35,17 +35,17 @@ public static MailUtil getInstance(String host, Integer port) {
}

public void send(String from, String to, String subject, String text) {
Session session = Session.getDefaultInstance(properties);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(text);
Transport.send(message);
} catch (MessagingException e) {
logger.error("Error sending notification message");
Email email = EmailBuilder.startingBlank()
.from(from)
.to(to)
.withSubject(subject)
.withPlainText(text)
.buildEmail();

mailer.sendMail(email);
} catch (Exception e) {
logger.error("Error sending notification message", e);
}
}
}