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
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