-1

With the mailing application I'm working on, I catch SendFailedException to handle validation of recipient email addresses. After reading a bit more though, I learned that catch blocks are quite expensive. It made me wonder if there was any way to determine if an address is invalid before the message is sent. This is more of an optimization problem of sorts.

  • 1
    You can check a regex. So if email address is correctly inputed – dharr May 26 '14 at 08:55
  • from what I've gathered from regex, it is still not a sure way of validating an address. But thanks though, I might add regex in my app – user3643117 May 26 '14 at 09:05
  • 2
    Sending an email or checking whether it exist involve network transactions, which are more expensive than a `catch` block. If you pre-check e-mail valididy, do it for correctness, *not supposed bad performance of a catch block*. – Darkhogg May 26 '14 at 09:06
  • Yes, that might be the case too. Regex is possible and does not involve network transactions. It is not a foolproof way of doing it though – user3643117 May 26 '14 at 09:09

1 Answers1

2

It depends on how you define "invalid". There is an Apache library that contains an email validator, but this does little more than checking for an "@" sign, and checking that the before and after the @ sign fields don't exceed the limits. There is no easy way to validate that the address exists on the target mail server.

The SMTP protocol depends on passing the message to the next mail server in what can be a long chain. From the sender point of view, the mail is "sent" once the next server in the chain says "I got that".

Errors can be reported days later, and don't always mean the address is invalid. For example, the email mailbox may be full and reject further messages. Processing such replies requires a state machine, as you are monitoring a long-running transaction.

kiwiron
  • 1,677
  • 11
  • 17
  • By "invalid", I mean it does not exist. It may not have a domain or it may contain characters which cannot be used in email addresses – user3643117 May 26 '14 at 09:19
  • 1
    I suggest you take a look at the Wikipedia article "Email address" and read the RFC at http://tools.ietf.org/html/rfc5321. These should crystalize the possibilities for validating an email address. – kiwiron May 26 '14 at 09:25