diff options
-rw-r--r-- | dozentenmodulserver/pom.xml | 5 | ||||
-rw-r--r-- | dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/SmtpMailer.java | 150 |
2 files changed, 155 insertions, 0 deletions
diff --git a/dozentenmodulserver/pom.xml b/dozentenmodulserver/pom.xml index 288d7b0d..dc0067ce 100644 --- a/dozentenmodulserver/pom.xml +++ b/dozentenmodulserver/pom.xml @@ -148,6 +148,11 @@ <version>2.3.1</version> <scope>compile</scope> </dependency> + <dependency> + <groupId>commons-net</groupId> + <artifactId>commons-net</artifactId> + <version>3.3</version> + </dependency> </dependencies> </project> diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/SmtpMailer.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/SmtpMailer.java new file mode 100644 index 00000000..52c6d9b6 --- /dev/null +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/SmtpMailer.java @@ -0,0 +1,150 @@ +package org.openslx.bwlp.sat.maintenance; + +import java.io.IOException; +import java.io.Writer; +import java.net.InetAddress; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.spec.InvalidKeySpecException; + +import javax.security.auth.login.LoginException; + +import org.apache.commons.net.smtp.AuthenticatingSMTPClient; +import org.apache.commons.net.smtp.AuthenticatingSMTPClient.AUTH_METHOD; +import org.apache.commons.net.smtp.SMTPReply; +import org.apache.commons.net.smtp.SimpleSMTPHeader; + +public class SmtpMailer { + + // TODO Logging + + public enum EncryptionMode { + NONE, + IMPLICIT, + EXPLICIT + } + + private final String from; + private final String replyTo; + private final AuthenticatingSMTPClient client; + + public SmtpMailer(String host, int port, EncryptionMode ssl, String from, String replyTo, + String username, String password) throws UnknownHostException, SocketException, IOException, + LoginException, InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException { + InetAddress[] ips = InetAddress.getAllByName(host); + if (ips == null || ips.length == 0) + throw new UnknownHostException(host); + if (ssl == EncryptionMode.EXPLICIT || ssl == EncryptionMode.NONE) { + client = new AuthenticatingSMTPClient("TLSv1.2", false, "UTF-8"); + } else { + client = new AuthenticatingSMTPClient("TLSv1.2", true, "UTF-8"); + } + boolean cleanup = true; + try { + client.setConnectTimeout(5000); + IOException conEx = null; + for (InetAddress ip : ips) { + try { + client.connect(ip, port); + if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) { + client.disconnect(); + continue; + } + conEx = null; + break; + } catch (IOException e) { + conEx = e; + } + } + if (conEx != null) + throw conEx; + if (!client.elogin("bwlehrpool.sat")) { + throw new LoginException("SMTP server rejected EHLO"); + } + if (ssl == EncryptionMode.EXPLICIT && !client.execTLS()) { + throw new LoginException("STARTTLS (explicit TLS) failed"); + } + boolean authed = false; + try { + authed = client.auth(AUTH_METHOD.CRAM_MD5, username, password); + } catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException e) { + e.printStackTrace(); + } + if (!authed && !client.auth(AUTH_METHOD.PLAIN, username, password)) { + throw new LoginException("Server rejected AUTH command. Invalid username or password?"); + } + cleanup = false; + this.from = from; + this.replyTo = replyTo; + } finally { + if (cleanup) + cleanup(); + } + } + + private void cleanup() { + try { + client.logout(); + } catch (Exception e) { + } + try { + client.disconnect(); + } catch (Exception e) { + } + } + + private void abort() throws IOException { + if (!client.reset()) + throw new IOException("Cannot abort current mail transaction"); + } + + public boolean send(String recipient, String subject, String message) { + Writer writer; + SimpleSMTPHeader header; + + try { + header = new SimpleSMTPHeader(from, recipient, subject); + if (replyTo != null && !replyTo.isEmpty()) { + header.addHeaderField("Reply-To", replyTo); + } + + if (!client.setSender(from)) { + abort(); + return false; + } + if (!client.addRecipient(recipient)) { + abort(); + return false; + } + writer = client.sendMessageData(); + if (writer == null) { + abort(); + return false; + } + + writer.write(header.toString()); + writer.write(message); + writer.close(); + client.completePendingCommand(); + + return true; + } catch (IOException e) { + cleanup(); + return false; + } + } + + public boolean isConnected() { + if (!client.isConnected()) + return false; + try { + client.sendNoOp(); + return true; + } catch (IOException e) { + return false; + } + } + +} |