2

MIME message senders appear in formats such as:

"John Doe" <johndoe@gmail.com>
<johndoe@gmail.com>

I'm trying to figure out how to extract the string "johndoe@gmail.com" in the above examples, although I will also need the "johndoe" and "gmail.com" parts (per RFC I'm pretty sure splitting on @ is all that's needed from here). Obviously regex-ing up my own parser is one (not great) option.

It seemed this may be possible using javax.mail.internet.MimeMessage. All of the constructors require a Folder which I do not have (well, I sort of do, it exists in the IMAP layer), e.g.

MimeMessage(Folder folder, InputStream is, int msgnum) 

Which makes me feel I'm using this class wrong. Nonetheless, if I parse this way I do get access to the getFrom() method which returns an array of Address, which itself doesn't offer methods of use to me.

Using mime4j it's easy to get this far:

case T_FIELD: // field means header
    if(token.getName() == "from") {
        // get raw string as above - unparsed

So using mime4j or using java, javax etc. utilities it should be possible to extract the "a@b.com" part of the address from there, but I haven't found a class within javax or mime4j that is responsible for this yet.

djechlin
  • 59,258
  • 35
  • 162
  • 290

1 Answers1

2

I think you need InternetAddress class from javax.mail: http://docs.oracle.com/javaee/6/api/javax/mail/internet/InternetAddress.html#getAddress()

Minimum working example:

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

public class JavaMailExample {

    public static void main(String[] args) throws AddressException {
        String fullemail = "\"John Doe\" <johndoe@gmail.com>";
        InternetAddress addr = new InternetAddress(fullemail);
        System.out.println(addr.getPersonal()); // John Doe
        System.out.println(addr.getAddress());  // johndoe@gmail.com
   }
}
abak
  • 21
  • 3
  • Yes thanks, got it from here. To make this a good answer though you should include how to use it - I upvoted because this helped me but it's close to a link only answer. – djechlin May 27 '13 at 19:19