how do I catch a bad email address before sending?

K

Keith G Hicks

I'm using the following code to send out email messages to a list of people
in a database. My problem is that if I'm sending to 100 people and the 40th
address is bad, it crashes on that one and doesn't run the rest. I want to
log the one that crashed so I can display it to the user and then continue
sending to the remaining people in the list.

If an address is bad, the error occurs here: EmailMsg.To.Add(New
MailAddress(RecipientEmail, RecipientDisplayName)) and goes to the exception
block. I am not sure the best way to set this up so that the error is
caught, logged and then the code continues. Can anyone let me know the best
way to do this? (apart from this problem, all the code below runs fine as
needed).

Thanks,

Ketih



Try
cnn = New SqlConnection(cnnStr)
cnn.Open()

cmd = New SqlCommand("SELECT TOP 1 EmailFromAddress_Dev AS
EmailFromAddress, " _
& " EmailSenderAddress_Dev AS EmailSenderAddress,
ReunionEmailClient_Dev AS ReunionEmailClient " _
& " FROM Reunion.vwSystemData", cnn)

cmd.CommandType = CommandType.Text

rdr = cmd.ExecuteReader
While rdr.Read
EmailFromAddress =
rdr.GetValue(rdr.GetOrdinal("EmailFromAddress")).ToString()
EmailSenderAddress =
rdr.GetValue(rdr.GetOrdinal("EmailSenderAddress")).ToString()
ReunionEmailClient =
rdr.GetValue(rdr.GetOrdinal("ReunionEmailClient")).ToString()
End While
rdr.Close()

cmd = Nothing
cmd = New SqlCommand("SELECT DisplayName, Email FROM
Reunion.vwEmailRecipients WHERE " & rblWhichList.Text & " = 1", cnn)
cmd.CommandType = CommandType.Text

rdr = cmd.ExecuteReader
While rdr.Read

i = i + 1

EmailMsg.From = New MailAddress(EmailFromAddress, "Reunion
Committee")
EmailMsg.Sender = New MailAddress(EmailSenderAddress,
"Reunion Committee")

RecipientEmail =
rdr.GetValue(rdr.GetOrdinal("Email")).ToString
RecipientDisplayName =
rdr.GetValue(rdr.GetOrdinal("DisplayName")).ToString

EmailMsg.To.Clear()
EmailMsg.To.Add(New MailAddress(RecipientEmail,
RecipientDisplayName))

EmailMsg.Subject = txtEmailSubject.Text

EmailMsg.Body = htmleditEmailBody.Html.ToString

EmailMsg.IsBodyHtml = True
EmailMsg.BodyEncoding = System.Text.Encoding.UTF8

If chkRequestReadReceipt.Checked Then
EmailMsg.Headers.Add("Disposition-Notification-To",
EmailFromAddress)
End If

Dim EmailClient As New SmtpClient()
EmailClient.Send(EmailMsg)

'error: The specified string is not in the form required for
an e-mail address.

End While

cmd.Connection.Close()

lblStatus.Text = "Mail Message Sent to " & Trim(Str(i)) & "
recipient(s)."
lblStatus.ForeColor = Drawing.Color.Blue

Catch ex As SqlException
lblStatus.Text = "Database error: " & ex.ErrorCode.ToString
lblStatus.ForeColor = Drawing.Color.Red
If cmd.Connection.State <> ConnectionState.Closed Then
cmd.Connection.Close()
End If

Catch ex As Exception
lblStatus.Text = "General error: " & ex.Message
lblStatus.ForeColor = Drawing.Color.Red
If cmd.Connection.State <> ConnectionState.Closed Then
cmd.Connection.Close()
End If

End Try
 
K

Keith G Hicks

Thanks Mark. That helps a lot.

I have a question. Your validation string is quite a bit longer than the one
that is preset in the RegularExpressionValidator for testing emails in a
text box:
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"

I'm not very experienced with RE's so they might mean the same thing. I'm
just wondering why such a big difference. Does "w" handle a-zA-Z0-9 and the
Underscore character?

Thanks,

Keith


Mark Rae said:
I'm using the following code to send out email messages to a list of
people
in a database. My problem is that if I'm sending to 100 people and the
40th
address is bad, it crashes on that one and doesn't run the rest. I want to
log the one that crashed so I can display it to the user and then continue
sending to the remaining people in the list.

If an address is bad, the error occurs here: EmailMsg.To.Add(New
MailAddress(RecipientEmail, RecipientDisplayName)) and goes to the
exception
block. I am not sure the best way to set this up so that the error is
caught, logged and then the code continues. Can anyone let me know the
best
way to do this? (apart from this problem, all the code below runs fine as
needed).

Fairly simple. Just validate that the email address is valid before trying
to send it:
http://www.google.co.uk/search?sourceid=navclient&hl=en-GB&ie=UTF-8&rlz=1T4G
ZEZ_en-GBGB252GB252&q=%2eNET+email+address+validate

If the email address fails validation, log it as "bad" and move on to the
next one.

I usually use a regular expression for this, e.g.

public static bool isValidEmail(string pstrEmailAddress)
{
string strRegex = @"^([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})(\]?)$";
Regex objRegex = new Regex(strRegex);
if (objRegex.IsMatch(pstrEmailAddress))
{
return true;
}
else
{
return false;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top