first milestone:

We are sucessfully receiving push mails and sending notifications upon receiving emails.
Next step:
Enable Online configuration
This commit is contained in:
2022-04-13 16:44:03 +02:00
parent c9019b63f3
commit b2d9a115b9
5 changed files with 28 additions and 14 deletions

View File

@@ -15,10 +15,9 @@ public class Application {
var config = Files.readString(new File("/tmp/config.json").toPath()); var config = Files.readString(new File("/tmp/config.json").toPath());
JSONObject json = (JSONObject) parser.parse(config); JSONObject json = (JSONObject) parser.parse(config);
//String testSender = (String) json.get("sender"); MessageHandler forward = new Forwarder(json);
//String testReceiver = (String) json.get("receiver"); new ImapClient(json)
// new SmtpClient(json).send(json,testSender,"Stephan Richter",testReceiver,"Test","Dies ist ein Test"); .addListener(forward)
MessageHandler forward = new Forwarder(); .start();
new ImapClient(json).addListener(forward).start();
} }
} }

View File

@@ -6,4 +6,5 @@ public class Constants {
public static final String HOST = "host"; public static final String HOST = "host";
public static final String USER = "user"; public static final String USER = "user";
public static final String PASSWORD = "password"; public static final String PASSWORD = "password";
public static final String INBOX = "inbox";
} }

View File

@@ -1,15 +1,34 @@
package de.srsoftware.widerhall; package de.srsoftware.widerhall;
import org.json.simple.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import javax.mail.Message; import javax.mail.Message;
import javax.mail.MessagingException; import javax.mail.MessagingException;
import java.io.UnsupportedEncodingException;
public class Forwarder implements MessageHandler { public class Forwarder implements MessageHandler {
private static final Logger LOG = LoggerFactory.getLogger(Forwarder.class); 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 @Override
public void onMessageReceived(Message message) throws MessagingException { public void onMessageReceived(Message message) throws MessagingException {
LOG.debug("forwarding {}",message.getSubject()); 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();
}
} }
} }

View File

@@ -24,11 +24,6 @@ public class ImapClient {
private class ListeningThread extends Thread { private class ListeningThread extends Thread {
private static final Logger LOG = LoggerFactory.getLogger(ListeningThread.class); private static final Logger LOG = LoggerFactory.getLogger(ListeningThread.class);
private final JSONObject config;
ListeningThread(JSONObject config){
this.config = config;
}
@Override @Override
public void run() { public void run() {
@@ -50,13 +45,14 @@ public class ImapClient {
String host = (String) config.get(Constants.HOST); String host = (String) config.get(Constants.HOST);
String username = (String) config.get(Constants.USER); String username = (String) config.get(Constants.USER);
String password = (String) config.get(Constants.PASSWORD); String password = (String) config.get(Constants.PASSWORD);
String folderName = (String) config.get(Constants.INBOX);
LOG.debug("Connecting and logging in…"); LOG.debug("Connecting and logging in…");
Properties props = imapProps(); Properties props = imapProps();
Session session = Session.getInstance(props); Session session = Session.getInstance(props);
Store store = session.getStore(Constants.IMAPS); Store store = session.getStore(Constants.IMAPS);
store.connect(host,username,password); store.connect(host,username,password);
LOG.debug("Connected, opening inbox:"); LOG.debug("Connected, opening {}:",folderName);
inbox = (IMAPFolder)store.getFolder("INBOX"); inbox = (IMAPFolder)store.getFolder(folderName);
inbox.open(IMAPFolder.READ_WRITE); inbox.open(IMAPFolder.READ_WRITE);
while (!stopped){ while (!stopped){
@@ -116,7 +112,7 @@ public class ImapClient {
public void start() { public void start() {
LOG.debug("Creating ListeningThread…"); LOG.debug("Creating ListeningThread…");
new ListeningThread(config).start(); new ListeningThread().start();
LOG.debug("Creating Heartbeat…"); LOG.debug("Creating Heartbeat…");
new Heartbeat().start(); new Heartbeat().start();
} }

View File

@@ -35,7 +35,6 @@ public class SmtpClient {
props.put(PORT,port); props.put(PORT,port);
props.put(AUTH,true); props.put(AUTH,true);
props.put(SSL,true); props.put(SSL,true);
// props.put("mail.smtp.localhost","mail.keawe.de");
session = Session.getInstance(props); session = Session.getInstance(props);
LOG.debug("Created new {}: {}", getClass().getSimpleName(),session); LOG.debug("Created new {}: {}", getClass().getSimpleName(),session);