I am trying to write a program that will pull the subjects of emails from a google account. I then would like to search those subjects and if they contain certain strings plug that string into an equation. My office uses email to track its technicians and we are responsible for keeping up with mileage. I would like to make a Java program that will take these email subjects and calculate mileage based off of them.
For example the subject of my email is: "Leaving LMN" I want to search that subject for the String LMN which I can set as the distance to the location of LMN.
I have:
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
public class EmailReader {
public static void main(String args[]) {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "emailaddress", "password");
System.out.println(store);
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
Message messages[] = inbox.getMessages();
for (Message message : messages) {
System.out.print("SUBJECT: ");
System.out.println(message.getSubject());
System.out.print("DATE: ");
System.out.println(message.getSentDate());
}
} catch (MessagingException e) {
e.printStackTrace();
System.exit(2);
} catch (Exception e) {
e.printStackTrace();
System.exit(2);
}
}
Which does pull the subjects correctly but I am having trouble manipulating the data from here. I was playing around with str.indexOf(), but wasn't able to get it to work.