from static to POO

This commit is contained in:
Francois G 2023-09-04 13:10:33 +02:00
parent bd0460e754
commit 234a4a4795
2 changed files with 16 additions and 49 deletions

View file

@ -1,39 +0,0 @@
package dev.peerat.backend;
public class MailConfiguration {
String username;
String password;
String smtpHost;
String smtpPort;
String fromAddress;
public MailConfiguration(String initUsername, String initPassword, String initSmtpHost, String initSmtpPort, String initFromAddress) {
username = initUsername;
password = initPassword;
smtpHost = initSmtpHost;
smtpPort = initSmtpPort;
fromAddress = initFromAddress.isBlank() ? "cyberbottle@peerat.dev" : initFromAddress;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getSmtpHost() {
return smtpHost;
}
public String getSmtpPort() {
return smtpPort;
}
public String getFromAddress() {
return fromAddress;
}
}

View file

@ -2,7 +2,6 @@ package dev.peerat.backend.utils;
import java.util.Properties; import java.util.Properties;
import dev.peerat.backend.MailConfiguration;
import jakarta.mail.Message; import jakarta.mail.Message;
import jakarta.mail.MessagingException; import jakarta.mail.MessagingException;
import jakarta.mail.PasswordAuthentication; import jakarta.mail.PasswordAuthentication;
@ -13,25 +12,32 @@ import jakarta.mail.internet.MimeMessage;
public class Mail { public class Mail {
public static void send(String toAddress, String subject, String text, MailConfiguration mailConfiguration) { private Session session;
private String fromAddress;
public Mail(String initUsername, String initPassword, String initSmtpHost, String initSmtpPort, String initFromAddress) {
Properties props = new Properties(); Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true"); props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", mailConfiguration.getSmtpHost()); props.put("mail.smtp.host", initSmtpHost);
props.put("mail.smtp.port", mailConfiguration.getSmtpPort()); props.put("mail.smtp.port", initSmtpPort);
Session session = Session.getInstance(props, session = Session.getInstance(props,
new jakarta.mail.Authenticator() { new jakarta.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() { protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mailConfiguration.getUsername(), mailConfiguration.getPassword()); return new PasswordAuthentication(initUsername, initPassword);
} }
}); });
fromAddress = initFromAddress.isBlank() ? "cyberbottle@peerat.dev" : initFromAddress;
}
public void send(String toAddress, String subject, String text) {
try { try {
Message message = new MimeMessage(session); Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(mailConfiguration.getFromAddress())); message.setFrom(new InternetAddress(fromAddress));
message.setRecipients(Message.RecipientType.TO, message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toAddress)); InternetAddress.parse(toAddress));
message.setSubject(subject); message.setSubject(subject);