Start shifting over the mail controller sending

This commit is contained in:
Luedtke 2021-07-09 15:44:54 +02:00
parent 0fa887052a
commit 4f1725251c
2 changed files with 113 additions and 4 deletions

View File

@ -17,8 +17,8 @@ import java.util.concurrent.atomic.AtomicLong;
@RestController
public class MailController {
private static final String template = "Hello %s! This is a test mail from the Fachschaft WIAI.";
private static final String user = "ex311199c";
private static final String pw = "jX4b82jnvWe7EeNR";
private static final String user = "";
private static final String pw = "";
private final AtomicLong counter = new AtomicLong(); //from the tutorial... deprc
@GetMapping("/mail")
@ -87,13 +87,11 @@ public class MailController {
return mail;
} catch (javax.mail.internet.AddressException jMailException) {
System.err.println("Address Exception whilst sending the mail");
jMailException.printStackTrace();
return new Email(-1, "", "", "");
} catch (javax.mail.MessagingException jMailException) {
System.err.println("Messaging Exception whilst sending the mail");
jMailException.printStackTrace();
return new Email(-1, "", "", "");

View File

@ -0,0 +1,111 @@
package de.fswiai.klausurenmodul.service;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import java.io.File;
import java.util.Properties;
public class MailService {
private String username = "";
private String password = "";
private int smtpPort = 0;
private String smtpHost = "";
private String tlsVersion = "TLSv1.2"; //maybe already use 1.3?
private boolean useSTARTTLS = true;
public MailService(String username, String password, int smtpPort, String smtpHost, String tlsVersion, boolean useSTARTTLS) {
this.username = username;
this.password = password;
this.smtpPort = smtpPort;
this.smtpHost = smtpHost;
this.tlsVersion = tlsVersion;
this.useSTARTTLS = useSTARTTLS;
}
//TODO switch boolean to some result class
public boolean sendSimpleMail(String subject, String body)
throws javax.mail.internet.AddressException, javax.mail.MessagingException {
return true;
}
//TODO switch boolean to some result class
public boolean sendMailWithAttachments(String subject, String body, File[] files)
throws javax.mail.internet.AddressException, javax.mail.MessagingException {
return true;
}
private Session configureSession(){
Properties prop = new Properties();
prop.put("mail.smtp.auth", true);
if(this.useSTARTTLS){
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.starttls.required", "true");
} else {
prop.put("mail.smtp.starttls.enable", "false");
prop.put("mail.smtp.starttls.required", "false");
}
prop.put("mail.smtp.host", this.smtpHost);
prop.put("mail.smtp.port", this.smtpPort);
prop.put("mail.smtp.ssl.protocols", this.tlsVersion);
prop.put("mail.smtp.ssl.trust", "exhub.uni-bamberg.de");
Session session = Session.getInstance(prop, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(MailService.this.username, MailService.this.password);
}
});
return session;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getSmtpPort() {
return smtpPort;
}
public void setSmtpPort(int smtpPort) {
this.smtpPort = smtpPort;
}
public String getSmtpHost() {
return smtpHost;
}
public void setSmtpHost(String smtpHost) {
this.smtpHost = smtpHost;
}
public String getTlsVersion() {
return tlsVersion;
}
public void setTlsVersion(String tlsVersion) {
this.tlsVersion = tlsVersion;
}
public boolean isUseSTARTTLS() {
return useSTARTTLS;
}
public void setUseSTARTTLS(boolean useSTARTTLS) {
this.useSTARTTLS = useSTARTTLS;
}
}