From a8cd4681d5e3f5612afea249ff2ce997f23cfba9 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Wed, 13 Apr 2022 13:34:28 +0200 Subject: [PATCH] first working version of smtp class --- .gitignore | 1 + pom.xml | 4 +- .../de/srsoftware/widerhall/SmtpClient.java | 63 ++++++++++++------- 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index eb5a316..ee44a96 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +.idea target diff --git a/pom.xml b/pom.xml index 244b293..d1add8b 100644 --- a/pom.xml +++ b/pom.xml @@ -15,8 +15,8 @@ - javax.mail - javax.mail-api + com.sun.mail + javax.mail 1.6.2 diff --git a/src/main/java/de/srsoftware/widerhall/SmtpClient.java b/src/main/java/de/srsoftware/widerhall/SmtpClient.java index 2612286..752f6e6 100644 --- a/src/main/java/de/srsoftware/widerhall/SmtpClient.java +++ b/src/main/java/de/srsoftware/widerhall/SmtpClient.java @@ -6,50 +6,69 @@ import org.json.simple.parser.ParseException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.json.stream.JsonParser; -import javax.mail.Authenticator; -import javax.mail.PasswordAuthentication; -import javax.mail.Session; +import javax.mail.*; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; import java.io.File; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.nio.file.Files; +import java.util.Date; +import java.util.Map; import java.util.Properties; public class SmtpClient { private static final Logger LOG = LoggerFactory.getLogger(SmtpClient.class); private static final String HOST = "mail.smtp.host"; private static final String PORT = "mail.smtp.port"; - private static final String SOCK = "mail.smtp.socketFactory.port"; - private static final String CLASS = "mail.smtp.socketFactory.class"; - private static final String SOCKET_FACTORY = javax.net.ssl.SSLSocketFactory.class.getCanonicalName(); private static final String AUTH = "mail.smtp.auth"; + private static final String SSL = "mail.smtp.ssl.enable"; + private static final String UTF8 = "UTF-8"; private Session session; - public void login(String host, int port, String username, String password){ + public SmtpClient(Map config){ + String host = (String) config.get("host"); + long port = (long) config.get("port"); Properties props = new Properties(); props.put(HOST,host); props.put(PORT,port); - props.put(SOCK,port); props.put(AUTH,true); - props.put(CLASS,SOCKET_FACTORY); + props.put(SSL,true); +// props.put("mail.smtp.localhost","mail.keawe.de"); - Authenticator auth = new Authenticator() { - @Override - protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication(username,password); - } - }; + session = Session.getInstance(props); + LOG.debug("Created new {}: {}", getClass().getSimpleName(),session); + } + + public void send(JSONObject config, String senderAdress, String senderName, String receivers, String subject, String content) throws MessagingException, UnsupportedEncodingException { + MimeMessage message = new MimeMessage(session); + message.addHeader("Content-Type","text/plain; charset="+UTF8); + message.addHeader("format","flowed"); + message.addHeader("Content-Transfer-Encoding","8bit"); + + message.setFrom(new InternetAddress(senderAdress,senderName)); + message.setReplyTo(InternetAddress.parse(senderAdress,false)); + message.setSubject(subject,UTF8); + message.setText(content,UTF8); + message.setSentDate(new Date()); + message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(receivers,false)); - session = Session.getDefaultInstance(props,auth); - LOG.debug("Created new session: {}", session); + String username = (String) config.get("user"); + String password = (String) config.get("password"); + + LOG.debug("Versende Mail…"); + Transport.send(message,username,password); + LOG.debug("…versendet"); } - public static void main(String[] args) throws IOException, ParseException { + public static void main(String[] args) throws IOException, ParseException, MessagingException { var parser = new JSONParser(); var config = Files.readString(new File("/tmp/config.json").toPath()); - LOG.debug("config: {}",config); - var json = parser.parse(config); - LOG.debug("json: {}",json); + JSONObject json = (JSONObject) parser.parse(config); + + String testSender = (String) json.get("sender"); + String testReceiver = (String) json.get("receiver"); + new SmtpClient(json).send(json,testSender,"Stephan Richter",testReceiver,"Test","Dies ist ein Test"); } }