1

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.

3
  • How do you plan to use it once you get it? IOW, if you were able to extract "LMN", how do you plan to map that to the distance and where is the distance coming from - a file, db, hard coded?? Commented Dec 13, 2014 at 22:15
  • For now, hard coded. Eventually, I would like to plug it into the google maps API. IE; Set LMN = 123 Address Rd City, ST and then take that variable and import it into the google maps API, if that's even possible... I am just starting and may be biting off more than I can chew. Commented Dec 13, 2014 at 22:26
  • Give examples of input text and desired extracted text Commented Dec 13, 2014 at 22:30

2 Answers 2

1

What you need is RegEx. Regular Expression is used to search within a string by creating a pattern. That pattern is apply to the string. You can use RegEx for different situations.

In your example, you need to extract a part of your string: LMN

[A-Z]{3}

that represent a string of 3 characters alphabetic and uppercase.

You can specify your pattern to start with something:

^.*[ ]

Where ^ is the beginning of the string, . represent any characters and the * zero to infinity numbers of time. [ ] is the space before LMN.

combined these 2 RegEx give you:

^.*[ ][A-Z]{3}$

The $ represent the end of the string. So anything until a space and 3 characters uppercase at the end.

From there you want to extract only the 3 characters. You can group them:

^.*[ ]([A-Z]{3})$

Referring to that website, the Java look like this:

Pattern pattern = Pattern.compile("^.*[ ]([A-Z]{3})$");
Matcher m = pattern.matcher(message.getSubject());
if (m.matches()) {
  String location = m.group(1);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I believe this is exactly what I am looking for. I was testing it out and it gives an error when I put in the line that says: Matcher m = pattern.matcher(pattern); It says "The method matcher(CharSequence) in the type Pattern is not applicable for the arguments (Pattern)
Yes sorry for that the line is Matcher m = pattern.matcher(message.getSubject());. I edited my answer
0

If you want power and flexibility then Regular Expressions are the way to go. Oracle have an excellent tutorial on their site.

http://docs.oracle.com/javase/tutorial/essential/regex/index.html

Here's some sample code of how you would look for 'LMN' in a subject:

String match = "LMN";
String subject = "bla bla bla LMN bla bla";
Matcher matcher = Pattern.compile(match, Pattern.CASE_INSENSITIVE).matcher(subject);
boolean found = matcher.find();
if (found) {
    System.out.println(matcher.start()); // Tells you where the match begins
    System.out.println(matcher.end()); // Tells you where the match ends
}

1 Comment

Was able to get it to do what I wanted using this (for this portion anyway) Thanks so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.