Stanley Mungai
Stanley Mungai

Reputation: 4150

Find email address in a bounced back email

I am trying to get the email addresses for which my mails bounced and the only way I could find was to read the bounced Email using javamail and try finding the Email addresses Contained in the Message. I am able to read the Emails and now My challenge is finding the Emails therein. Here is My method:

public String findEmailInString(String message) {
        String email = "";
        Pattern pat = Pattern.compile("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$");
        Matcher match = pat.matcher(message);
        while (match.matches()) {
            email = match.group();
        }
        return email;
    }

EDIT My messages are in this Format: "

This is an informative message sent by mail.simbatech.biz.

The server was not able to deliver your email message 

  Subject: GAtungo
  Date: Fri, 14 Nov 2014 15:52:58 +0300



to the following addresses:

  <[email protected]> (mail.simbatech.biz: Mailbox does not exist)"

This Method is returning null despite the fact that there are email addresses in the message body. Is there a better way to achieve this?

Upvotes: 0

Views: 200

Answers (1)

Steve Smith
Steve Smith

Reputation: 5211

I know that this doesn't answer the question directly but the normal practice here isn't to attempt to parse the bounce message at all.

Instead when you send your emails out you set a different Return-Path header for each email that you send. You can then uniquely identify which user this message is from.

This technique is called VERP.

Upvotes: 1

Related Questions