-
-
Save ydarias/72e3761cabcdb76c170e to your computer and use it in GitHub Desktop.
package com.runnics.mailing; | |
import com.funius.core.PropertyLoader; | |
import org.joda.time.DateTime; | |
import javax.mail.*; | |
import javax.mail.search.AndTerm; | |
import javax.mail.search.ComparisonTerm; | |
import javax.mail.search.ReceivedDateTerm; | |
import javax.mail.search.SearchTerm; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Properties; | |
import java.util.stream.Collectors; | |
public class GmailClient { | |
private Store store; | |
public GmailClient() { | |
Properties gmailProperties = new PropertyLoader().loadProperties("gmail.properties"); | |
String user = gmailProperties.getProperty("gmail.user"); | |
String password = gmailProperties.getProperty("gmail.password"); | |
Properties properties = System.getProperties(); | |
properties.setProperty("mail.store.protocol", "imaps"); | |
properties.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); | |
properties.setProperty("mail.imap.socketFactory.fallback", "false"); | |
Session session = Session.getDefaultInstance(properties, null); | |
try { | |
store = session.getStore("imaps"); | |
store.connect("imap.gmail.com", user, password); | |
} catch (MessagingException e) { | |
} | |
} | |
public List<MailWrapper> getLastHoursMails(int hours) throws GmailClientException { | |
return getLastHoursMails(hours, "[Gmail]/Todos"); | |
} | |
public List<MailWrapper> getLastHoursMails(int hours, String inboxName) throws GmailClientException { | |
Folder inbox = null; | |
try { | |
inbox = store.getFolder(inboxName); | |
inbox.open(Folder.READ_ONLY); | |
Message[] messages = inbox.search(lastHoursSearchTerm(hours)); | |
return Arrays.stream(messages) | |
.map(MailWrapper::instance) | |
.collect(Collectors.toList()); | |
} catch (MessagingException e) { | |
throw new GmailClientException(e); | |
} finally { | |
closeFolder(inbox); | |
} | |
} | |
public List<MailWrapper> getMailsSince(DateTime fromDate, String inboxName) throws GmailClientException { | |
Folder inbox = null; | |
try { | |
inbox = store.getFolder(inboxName); | |
inbox.open(Folder.READ_ONLY); | |
Message[] messages = inbox.search(sinceSearchTerm(fromDate)); | |
return Arrays.stream(messages) | |
.map(MailWrapper::instance) | |
.collect(Collectors.toList()); | |
} catch (MessagingException e) { | |
throw new GmailClientException(e); | |
} finally { | |
closeFolder(inbox); | |
} | |
} | |
private SearchTerm sinceSearchTerm(DateTime fromDate) { | |
DateTime rightNow = new DateTime(); | |
SearchTerm olderThan = new ReceivedDateTerm(ComparisonTerm.LE, rightNow.toDate()); | |
SearchTerm newerThan = new ReceivedDateTerm(ComparisonTerm.GE, fromDate.toDate()); | |
return new AndTerm(newerThan, olderThan); | |
} | |
private SearchTerm lastHoursSearchTerm(int hours) { | |
DateTime rightNow = new DateTime(); | |
DateTime past = rightNow.minusHours(hours); | |
SearchTerm olderThan = new ReceivedDateTerm(ComparisonTerm.LE, rightNow.toDate()); | |
SearchTerm newerThan = new ReceivedDateTerm(ComparisonTerm.GE, past.toDate()); | |
return new AndTerm(newerThan, olderThan); | |
} | |
private void closeFolder(Folder inbox) { | |
try { | |
if (inbox != null) | |
inbox.close(false); | |
} catch (MessagingException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Hello @dakssss
This Gist has some time, and I don't even remember why I published :-D Probably as support from some blog-post.
Most of the code is standard Java, but the PropertyLoader
and DateTime
. The first one was an internal tool at the company where I created the code, but it is just a PropertyLoader to inject configuration, you can replace by your own. DateTime
is a class that comes at Joda Time library.
where can i find the com.funius.core.PropertyLoader;
Hello @Nicky5
That was an internal library from the company I worked for at the moment we created the Gmail client. It is nothing more than the standard property loader with some custom functions to load a file during the booting process. My recommendation is to replace that part of the code for environment variables that you can configure at runtime.
is there any jar file needed here?