Skip to content

Commit

Permalink
Use Jakarta Mail
Browse files Browse the repository at this point in the history
  • Loading branch information
Croway authored and avano committed Sep 11, 2023
1 parent d9f7d49 commit aaea7b4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
8 changes: 4 additions & 4 deletions system-x/services/mail/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<name>TNB :: System-X :: Services :: Mail</name>

<properties>
<commons-email.version>1.5</commons-email.version>
<angus-mail-version>2.0.2</angus-mail-version>
</properties>

<dependencies>
Expand All @@ -23,9 +23,9 @@
<artifactId>testcontainers</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>${commons-email.version}</version>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-mail</artifactId>
<version>${angus-mail-version}</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
import software.tnb.common.utils.HTTPUtils;
import software.tnb.common.validation.Validation;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import jakarta.mail.Authenticator;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.PasswordAuthentication;
import jakarta.mail.Session;
import jakarta.mail.Transport;
import jakarta.mail.internet.MimeMessage;
import okhttp3.MediaType;
import okhttp3.RequestBody;

Expand All @@ -34,18 +38,29 @@ public void sendEmail(String subject, String message, String sender, String rece
}
String[] parts = smtpHostname.split(":");
try {
Email email = new SimpleEmail();
email.setHostName(parts[0]);
email.setSmtpPort(Integer.parseInt(parts[1]));
email.setAuthenticator(new DefaultAuthenticator(sender, accounts.get(sender)));
email.setFrom(sender);
email.setSubject(subject);
email.setMsg(message);
email.addTo(receiver);
email.send();
} catch (EmailException ex) {
LOG.error(ex.getMessage(), ex);
throw new RuntimeException(ex);
Authenticator auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(sender, accounts.get(sender));
}
};

Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", parts[0]);
properties.put("mail.smtp.port", Integer.parseInt(parts[1]));

MimeMessage mimeMessage = new MimeMessage(Session.getInstance(properties, auth));

mimeMessage.setFrom(sender);
mimeMessage.setRecipients(Message.RecipientType.TO, receiver);
mimeMessage.setText(message);
mimeMessage.setSubject(subject);

Transport.send(mimeMessage);
} catch (MessagingException e) {
LOG.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}

Expand Down

0 comments on commit aaea7b4

Please sign in to comment.