I’m allowing users to manage a distribution list stored in a database. Users are only allowed to enter emails that are @mydomain.com. A web based application then takes the distribution list and sends emails. I’d like to validate that the email is valid before sending an email from the application.
To send an email I’m using this code:
Dim SendTo As String = "ThisIsNotARealEmailAddress@mydomain.com"
Dim SentFrom As String = "me@mydomain.com"
Dim MessageBody As String = "blah blah blah"
Dim MessageSubject As String = "This is the subject"
Dim mm As New MailMessage(SentFrom, SendTo)
mm.Subject = MessageSubject
mm.IsBodyHtml = False
mm.Priority = MailPriority.High
mm.Body = MessageBody
Dim smtp As New SmtpClient()
smtp.Send(mm)
If the SendTo is not a valid email address the server returns this error:
Mailbox unavailable. The server response was: 5.1.1 <ThisIsNotARealEmailAddress@mydomain.com>... User unknown
Is there anyway to validate the email when the email address is added to the database, instead of a try catch block when sending the email?
The users are only sending to your domain? And you control the domain? And its a windows domain? Just query the AD and get their email address from the AD without asking them. Would that be valid? I presume this is a windows application, not a web app.