diff --git a/src/main/java/de/fswiai/klausurenmodul/controller/MailController.java b/src/main/java/de/fswiai/klausurenmodul/controller/MailController.java index db3eb08..a09cf7b 100644 --- a/src/main/java/de/fswiai/klausurenmodul/controller/MailController.java +++ b/src/main/java/de/fswiai/klausurenmodul/controller/MailController.java @@ -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, "", "", ""); diff --git a/src/main/java/de/fswiai/klausurenmodul/service/MailService.java b/src/main/java/de/fswiai/klausurenmodul/service/MailService.java new file mode 100644 index 0000000..37880e2 --- /dev/null +++ b/src/main/java/de/fswiai/klausurenmodul/service/MailService.java @@ -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; + } +}