I've implemented a java program using the JavaMail API to only fetch unread emails from a particular user (and mark it as read). This will allow you to periodically get emails from your ship traffic service. This is inspired by this blog post on Stephen Wolfram's email analytics.
1. Java code to fetch unread email from a sender
Below is the java code, which is saved in the file FetchMail.java
.
import java.io.IOException ;
import java.util.Properties ;
import javax.mail.* ;
import javax.mail.search.FlagTerm ;
import javax.mail.internet.InternetAddress ;
public class FetchMail {
public static String[] UnreadMail(
String server ,
String folder ,
String from ,
String username ,
String password
) {
Properties props = System.getProperties() ;
props.setProperty("mail.store.protocol", "imaps") ;
try {
Session session = Session.getDefaultInstance(props, null) ;
Store store = session.getStore("imaps") ;
store.connect(server, username, password) ;
Folder inbox = store.getFolder(folder) ;
inbox.open(Folder.READ_WRITE) ;
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false) ;
Message msgs[] = inbox.search(ft) ;
String[] emails = new String[msgs.length] ;
String[] content = new String[msgs.length] ;
if (msgs.length > 0)
{
for (int i = 0; i < msgs.length ; i++)
{
emails[i] = ((InternetAddress) msgs[i].getFrom()[0]).getAddress() ;
if (emails[i].equals(from))
{
content[i] = msgs[i].getContent().toString() ;
}
}
}
store.close() ;
return content ;
}
catch (NoSuchProviderException e) {
return null ;
}
catch (MessagingException e) {
return null ;
}
catch (IOException e) {
return null ;
}
}
}
Next, compile the above file. You will need the mail.jar
file from the above API download. Drop it in the same folder and execute
javac -classpath ./mail.jar FetchMail.java
2. Calling it from Mathematica
After compiling, include the FetchMail.class
file in the same directory as your Mathematica notebook. Here's a convenient function that uses this class file to fetch unread mail from the Gmail server from a particular sender
Needs["JLink`"];
AddToClassPath[NotebookDirectory[]];
Clear[unreadMail]
unreadMail[server_String, email_String, pass_String, folder_String, from_String] :=
DynamicModule[{messages, monthname2num, datelists, parsedate},
LoadJavaClass["FetchMail"];
messages = FetchMail`UnreadMail[server, folder, from, email, pass];
DeleteCases[messages, Null]
][[2]];
3. Usage and caveats
Usage is fairly straightforward from the arguments. As a sample demonstration, I just marked the last email from you as unread and ran this function and voila!
unreadMail["imap.gmail.com", "myEmail@gmail.com", "hunter2", "Inbox", "szabolcsEmail"]

However, do note that
- If you're running this as a
ScheduledTask
, you will have to enter your password as a string argument. Not necessarily a wise idea to do with your primary email ID, but you could always create a throwaway for this specific purpose and use that instead.
- Unread mail from this sender is automatically marked as read when you fetch it. This is necessary so that you don't keep retrieving the same mail again and again. However if you're automating it, be aware that there might be some emails that you haven't read and might have missed.
MailReceiverFunction
which can be deployed to Wolfram Cloud and be used to effectively solve this problem. (Not gmail, but the analysis of email notifications.) $\endgroup$ – C. E. Aug 21 '16 at 8:59