Created
March 9, 2019 19:36
-
-
Save DeBukkIt/19f194935a2007c8ac0a64e798cd7fb8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.blogspot.debukkitsblog.mailing; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Properties; | |
import java.util.Random; | |
import javax.mail.Flags; | |
import javax.mail.Message; | |
import javax.mail.MessagingException; | |
import javax.mail.Multipart; | |
import javax.mail.Part; | |
import javax.mail.Session; | |
import javax.mail.Store; | |
import com.sun.mail.iap.ProtocolException; | |
import com.sun.mail.imap.IMAPFolder; | |
import com.sun.mail.imap.protocol.IMAPProtocol; | |
public class StartMailPushEmpfaenger { | |
public static void main(String[] args) { | |
MailPushEventHandler mailEventHandler = new MailPushEventHandler("imap.gmail.com", Zugangsdaten.USERNAME(), Zugangsdaten.PASSWORD()); | |
} | |
public static class MailPushEventHandler extends MailPushEmpfaenger { | |
public MailPushEventHandler(String host, String username, String password) { | |
super(host, username, password); | |
} | |
@Override | |
public void onMailReceived(Object[] messages) { | |
if(messages != null && messages.length > 0) { | |
for(Object msg : messages) { | |
try { | |
String textFromMessage = getTextFromMessage((Message) msg); | |
System.out.println(textFromMessage); | |
} catch (MessagingException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
} | |
public static abstract class MailPushEmpfaenger { | |
private String randomInstanceID; | |
private boolean stopped; | |
private IMAPFolder inbox; | |
private Thread listeningThread; | |
private Thread reconnectionThread; | |
public MailPushEmpfaenger(String host, String username, String password) { | |
randomInstanceID = String.valueOf(new Random().nextInt(Integer.MAX_VALUE)); | |
listeningThread = new Thread(new Runnable() { | |
@Override | |
public void run() { | |
while(!stopped) { | |
try { | |
System.out.println("[MailRX " + randomInstanceID + "] Connecting and logging in..."); | |
// Connect with IMAP Server | |
Properties props = new Properties(); | |
props.setProperty("mail.store.protocol", "imaps"); | |
Session session = Session.getDefaultInstance(props, null); | |
Store store = session.getStore("imaps"); | |
store.connect(host, username, password); | |
System.out.println("[MailRX " + randomInstanceID + "] Connected."); | |
// Open inbox | |
inbox = (IMAPFolder) store.getFolder("INBOX"); | |
inbox.open(IMAPFolder.READ_WRITE); | |
// initial Mail Check | |
checkNewMessages(); | |
while(!stopped) { | |
System.out.println("[MailRX " + randomInstanceID + "] IDLE-ing"); | |
inbox.idle(true); | |
checkNewMessages(); | |
} | |
} catch (MessagingException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
}); | |
reconnectionThread = new Thread(new Runnable() { | |
@Override | |
public void run() { | |
while(!stopped) { | |
try { | |
// Sleep 9 minutes | |
Thread.sleep(9 * 60 * 1000); | |
System.out.println("[MailRX " + randomInstanceID + "] NOOP"); | |
inbox.doCommand(new IMAPFolder.ProtocolCommand() { | |
@Override | |
public Object doCommand(IMAPProtocol protocol) throws ProtocolException { | |
protocol.simpleCommand("NOOP", null); | |
return null; | |
} | |
}); | |
} catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
}); | |
listeningThread.start(); | |
reconnectionThread.start(); | |
} | |
private void checkNewMessages() throws MessagingException { | |
System.out.println("[MailRX " + randomInstanceID + "] Checking for new messages..."); | |
if(!inbox.isOpen()) { | |
inbox.open(IMAPFolder.READ_WRITE); | |
} | |
if(inbox.getMessageCount() > 0) { | |
ArrayList<Message> msgs = new ArrayList<Message>(); | |
Message[] tempMsgs = inbox.getMessages(inbox.getMessageCount()-10, inbox.getMessageCount()); | |
for(Message tempMsg : tempMsgs) { | |
if(!tempMsg.isSet(Flags.Flag.SEEN)) { | |
msgs.add(tempMsg); | |
tempMsg.setFlag(Flags.Flag.SEEN, true); | |
} | |
} | |
onMailReceived(msgs.toArray()); | |
System.out.println("[MailRX " + randomInstanceID + "] Forwarded " + msgs.size() + " E-Mails."); | |
} | |
} | |
public void onMailReceived(Object[] messages) { | |
// Overwrite me | |
} | |
/** | |
* Return the primary text content of the message.</br> | |
* Source: https://javaee.github.io/javamail/FAQ#mainbody | |
* | |
* @param p | |
* The message | |
* @return The message's main body text content | |
* @throws MessagingException | |
* If something went wrong | |
* @throws IOException | |
* If something else went wrong | |
*/ | |
public String getTextFromMessage(Part p) throws MessagingException, IOException { | |
if (p.isMimeType("text/*")) { | |
String s = (String) p.getContent(); | |
return s; | |
} | |
if (p.isMimeType("multipart/alternative")) { | |
Multipart mp = (Multipart) p.getContent(); | |
String text = null; | |
for (int i = 0; i < mp.getCount(); i++) { | |
Part bp = mp.getBodyPart(i); | |
if (bp.isMimeType("text/plain")) { | |
if (text == null) | |
text = getTextFromMessage(bp); | |
continue; | |
} else if (bp.isMimeType("text/html")) { | |
String s = getTextFromMessage(bp); | |
if (s != null) | |
return s; | |
} else { | |
return getTextFromMessage(bp); | |
} | |
} | |
return text; | |
} else if (p.isMimeType("multipart/*")) { | |
Multipart mp = (Multipart) p.getContent(); | |
for (int i = 0; i < mp.getCount(); i++) { | |
String s = getTextFromMessage(mp.getBodyPart(i)); | |
if (s != null) | |
return s; | |
} | |
} | |
return null; | |
} | |
public void stop() { | |
this.stopped = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment