3
votes

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 = "[email protected]"
    Dim SentFrom As String = "[email protected]"
    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 <[email protected]>... 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?

1
You can do a query against the AD domain when the email addresses are added. There should be plenty of examples out there. But you will still have errors when email addresses are changed or destroyed unless your admins update your database too. - Bill
@Bill ~ I didn't consider when email addresses change or get deleted. I'll just do my due diligence to get a valid email from AD upon submission, and then catch errors when sending. Thanks - zeroef

1 Answers

2
votes

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.