Skip to content

Instantly share code, notes, and snippets.

@ydarias
Created January 25, 2016 09:30
Show Gist options
  • Save ydarias/72e3761cabcdb76c170e to your computer and use it in GitHub Desktop.
Save ydarias/72e3761cabcdb76c170e to your computer and use it in GitHub Desktop.
Java class to get mails from a Gmail account using JavaMail API (the MailWrapper is an internal Runnics class to make it easy the process)
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();
}
}
}
@dakssss
Copy link

dakssss commented Nov 3, 2020

is there any jar file needed here?

@ydarias
Copy link
Author

ydarias commented Nov 3, 2020

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.

@Nicky5
Copy link

Nicky5 commented Dec 18, 2020

where can i find the com.funius.core.PropertyLoader;

@ydarias
Copy link
Author

ydarias commented Dec 20, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment