forget to add new file
This commit is contained in:
parent
eeac351b02
commit
bd0460e754
2 changed files with 84 additions and 0 deletions
39
src/dev/peerat/backend/MailConfiguration.java
Normal file
39
src/dev/peerat/backend/MailConfiguration.java
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
45
src/dev/peerat/backend/utils/Mail.java
Normal file
45
src/dev/peerat/backend/utils/Mail.java
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
package dev.peerat.backend.utils;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import dev.peerat.backend.MailConfiguration;
|
||||||
|
import jakarta.mail.Message;
|
||||||
|
import jakarta.mail.MessagingException;
|
||||||
|
import jakarta.mail.PasswordAuthentication;
|
||||||
|
import jakarta.mail.Session;
|
||||||
|
import jakarta.mail.Transport;
|
||||||
|
import jakarta.mail.internet.InternetAddress;
|
||||||
|
import jakarta.mail.internet.MimeMessage;
|
||||||
|
|
||||||
|
|
||||||
|
public class Mail {
|
||||||
|
public static void send(String toAddress, String subject, String text, MailConfiguration mailConfiguration) {
|
||||||
|
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.put("mail.smtp.starttls.enable", "true");
|
||||||
|
props.put("mail.smtp.auth", "true");
|
||||||
|
props.put("mail.smtp.host", mailConfiguration.getSmtpHost());
|
||||||
|
props.put("mail.smtp.port", mailConfiguration.getSmtpPort());
|
||||||
|
|
||||||
|
Session session = Session.getInstance(props,
|
||||||
|
new jakarta.mail.Authenticator() {
|
||||||
|
protected PasswordAuthentication getPasswordAuthentication() {
|
||||||
|
return new PasswordAuthentication(mailConfiguration.getUsername(), mailConfiguration.getPassword());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
Message message = new MimeMessage(session);
|
||||||
|
message.setFrom(new InternetAddress(mailConfiguration.getFromAddress()));
|
||||||
|
message.setRecipients(Message.RecipientType.TO,
|
||||||
|
InternetAddress.parse(toAddress));
|
||||||
|
message.setSubject(subject);
|
||||||
|
message.setText(text);
|
||||||
|
|
||||||
|
Transport.send(message);
|
||||||
|
} catch (MessagingException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue