开发者

How do you get an email address within a string

开发者 https://www.devze.com 2023-03-15 04:57 出处:网络
I am pulling many ema开发者_如何学Pythonils from an Exchange 2003 server and from those emails, trying to determine which are bounce-backs (invalid) so I can remove them from our contacts.

I am pulling many ema开发者_如何学Pythonils from an Exchange 2003 server and from those emails, trying to determine which are bounce-backs (invalid) so I can remove them from our contacts.

What would be the most efficient method of searching the email bodies to find email addresses on the bounce backs?


You might want to look at this page, which has several variants of regexes for matching email addresses and explains the trade-offs for selecting each. You should definitely read it before picking one here.


Just use a regex.

\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b


This is the regex that we use in a lot of our applications for email validation;

public static bool CheckEmail(string email)
    {
        //validate Email
        Regex regex = new Regex(@"^([a-zA-Z0-9_\-\.\']+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$", RegexOptions.IgnoreCase);
        Match match = regex.Match(email);
        return match.Success;
    }

The actual process for correctly identifying a bounced email, rather than an auto-reply or genuine message is a little more complicated, but this will at least give you the email address.


I pulled a few of the answers here into something like this. It actually returns each email address from the string (sometimes there are multiples from the mail host and target address). I can then match each of the email addresses up against the outbound addresses we sent, to verify. I used the article from @plinth to get a better understanding of the regular expression and modified the code from @Chris Bint

However, I'm still wondering if this is the fastest way to monitor 10,000+ emails? Are there any more efficient methods (while still using c#)? The live code won't recreate the Regex object every time within the loop.

public static MatchCollection CheckEmail(string email)
{
  Regex regex = new Regex(@"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b", RegexOptions.IgnoreCase);          
  MatchCollection matches = regex.Matches(email);

  return matches;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消