From b2d9a115b9d345d6233c75e95b213a2aff7b8c64 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Wed, 13 Apr 2022 16:44:03 +0200 Subject: [PATCH] first milestone: We are sucessfully receiving push mails and sending notifications upon receiving emails. Next step: Enable Online configuration --- .../de/srsoftware/widerhall/Application.java | 9 ++++----- .../de/srsoftware/widerhall/Constants.java | 1 + .../de/srsoftware/widerhall/Forwarder.java | 19 +++++++++++++++++++ .../de/srsoftware/widerhall/ImapClient.java | 12 ++++-------- .../de/srsoftware/widerhall/SmtpClient.java | 1 - 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/main/java/de/srsoftware/widerhall/Application.java b/src/main/java/de/srsoftware/widerhall/Application.java index 27d44a6..32398b3 100644 --- a/src/main/java/de/srsoftware/widerhall/Application.java +++ b/src/main/java/de/srsoftware/widerhall/Application.java @@ -15,10 +15,9 @@ public class Application { var config = Files.readString(new File("/tmp/config.json").toPath()); 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"); - MessageHandler forward = new Forwarder(); - new ImapClient(json).addListener(forward).start(); + MessageHandler forward = new Forwarder(json); + new ImapClient(json) + .addListener(forward) + .start(); } } diff --git a/src/main/java/de/srsoftware/widerhall/Constants.java b/src/main/java/de/srsoftware/widerhall/Constants.java index ffe7c8c..796f2ba 100644 --- a/src/main/java/de/srsoftware/widerhall/Constants.java +++ b/src/main/java/de/srsoftware/widerhall/Constants.java @@ -6,4 +6,5 @@ public class Constants { public static final String HOST = "host"; public static final String USER = "user"; public static final String PASSWORD = "password"; + public static final String INBOX = "inbox"; } diff --git a/src/main/java/de/srsoftware/widerhall/Forwarder.java b/src/main/java/de/srsoftware/widerhall/Forwarder.java index 5d59c6d..2bd48f1 100644 --- a/src/main/java/de/srsoftware/widerhall/Forwarder.java +++ b/src/main/java/de/srsoftware/widerhall/Forwarder.java @@ -1,15 +1,34 @@ package de.srsoftware.widerhall; +import org.json.simple.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.mail.Message; import javax.mail.MessagingException; +import java.io.UnsupportedEncodingException; public class Forwarder implements MessageHandler { private static final Logger LOG = LoggerFactory.getLogger(Forwarder.class); + private final SmtpClient smtp; + private final JSONObject config; + + public Forwarder(JSONObject config) { + this.config = config; + SmtpClient smtp = new SmtpClient(config); + this.smtp = smtp; + } + @Override public void onMessageReceived(Message message) throws MessagingException { LOG.debug("forwarding {}",message.getSubject()); + String testSender = (String) config.get("sender"); + String testReceiver = (String) config.get("receiver"); + + try { + smtp.send(config,testSender,"Stephan Richter",testReceiver,"Info: "+message.getSubject(),"Neue Mail eingegangen!"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } } } diff --git a/src/main/java/de/srsoftware/widerhall/ImapClient.java b/src/main/java/de/srsoftware/widerhall/ImapClient.java index 569071c..283b97a 100644 --- a/src/main/java/de/srsoftware/widerhall/ImapClient.java +++ b/src/main/java/de/srsoftware/widerhall/ImapClient.java @@ -24,11 +24,6 @@ public class ImapClient { private class ListeningThread extends Thread { private static final Logger LOG = LoggerFactory.getLogger(ListeningThread.class); - private final JSONObject config; - - ListeningThread(JSONObject config){ - this.config = config; - } @Override public void run() { @@ -50,13 +45,14 @@ public class ImapClient { String host = (String) config.get(Constants.HOST); String username = (String) config.get(Constants.USER); String password = (String) config.get(Constants.PASSWORD); + String folderName = (String) config.get(Constants.INBOX); LOG.debug("Connecting and logging in…"); Properties props = imapProps(); Session session = Session.getInstance(props); Store store = session.getStore(Constants.IMAPS); store.connect(host,username,password); - LOG.debug("Connected, opening inbox:"); - inbox = (IMAPFolder)store.getFolder("INBOX"); + LOG.debug("Connected, opening {}:",folderName); + inbox = (IMAPFolder)store.getFolder(folderName); inbox.open(IMAPFolder.READ_WRITE); while (!stopped){ @@ -116,7 +112,7 @@ public class ImapClient { public void start() { LOG.debug("Creating ListeningThread…"); - new ListeningThread(config).start(); + new ListeningThread().start(); LOG.debug("Creating Heartbeat…"); new Heartbeat().start(); } diff --git a/src/main/java/de/srsoftware/widerhall/SmtpClient.java b/src/main/java/de/srsoftware/widerhall/SmtpClient.java index 3fcaf50..a56b23c 100644 --- a/src/main/java/de/srsoftware/widerhall/SmtpClient.java +++ b/src/main/java/de/srsoftware/widerhall/SmtpClient.java @@ -35,7 +35,6 @@ public class SmtpClient { props.put(PORT,port); props.put(AUTH,true); props.put(SSL,true); -// props.put("mail.smtp.localhost","mail.keawe.de"); session = Session.getInstance(props); LOG.debug("Created new {}: {}", getClass().getSimpleName(),session);