Please help me to find simple solution to be able to send mails to users. I have created a component for a website to send emails to users.
This video has really helped me with that: http://www.asp.net/web-forms/videos/how-do-i/how-do-i-create-a-reusable-component-for-sending-email-to-a-distribution-list
I need to read a list of recipients from a data source, assuming that database is Sql database using SqlCommand to return DataTable.
How can I modify the code to read a list of recipients from a data source and send an email message to the recipients using the DataSource ?
Here's the code I have so far:
Public Class DistributionList
Dim smtpClient As New System.Net.Mail.SmtpClient()
Dim sentEmails As Integer
Public Function SendMail(ByVal mailMessage As System.Net.Mail.MailMessage) As Integer
AddHandler smtpClient.SendCompleted, AddressOf SmtpClient_OnCompleted
sentEmails = 0
Dim recipients As SqlDataSource = GetRecipients()
For Each recipient As DataRow In recipients.Rows
mailMessage.To.Clear()
mailMessage.To.Add(New System.Net.Mail.MailAddress(recipient("UserName"), recipient("EmailAddress")))
SendMessage(mailMessage)
Next
Return sentEmails
End Function
Private Function GetRecipients() As SqlDataSource
Dim table As New SqlDataSource()
table.ConnectionString = ConfigurationManager.ConnectionStrings("ANGELIKAConnectionString1").ToString()
table.SelectCommand = "Select FROM Valuation(UserName,EmailAddress) VALUES(@UserName,@EmailAddress)"
table.SelectParameters.Add("UserName")
table.SelectParameters.Add("EmailAddress")
Dim row As DataRow = table.NewRow()
row("UserName") = "John Doe"
row("EmailAddress") = "John@example.com"
table.Rows.Add(row)
Return table
End Function
Private Sub SendMessage(ByVal mailMessage As System.Net.Mail.MailMessage)
Dim userState As Object = mailMessage
Try
smtpClient.SendAsync(mailMessage, userState)
Catch failedRec As System.Net.Mail.SmtpFailedRecipientsException
'Choose to resend?
'Log failure of send here.
Catch smtpExc As System.Net.Mail.SmtpException
'Log failure of SMTP client here.
Catch ex As Exception
'Log exception here.
End Try
End Sub
Public Sub SmtpClient_OnCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
Dim mailMessage As System.Net.Mail.MailMessage = CType(e.UserState, System.Net.Mail.MailMessage)
If (e.Error Is Nothing) Then
sentEmails = sentEmails + 1
Else
'Write to your error log here that an email failed.
'Possibly include information from the mailMessage.
End If
End Sub
End Class
Hopefully someone can help me on the way to adjust the code.
Thanks in advance.
I am waiting to hear from you as soon as possible Angelika.
e-mail : auroran@yandex.ru
Angelika
Member
22 Points
89 Posts
How can I modify the code to read a list of recipients from a data source and send an email messa...
Mar 30, 2012 07:48 PM|LINK
Dear Programmers,
Please help me to find simple solution to be able to send mails to users. I have created a component for a website to send emails to users.
This video has really helped me with that: http://www.asp.net/web-forms/videos/how-do-i/how-do-i-create-a-reusable-component-for-sending-email-to-a-distribution-list
I need to read a list of recipients from a data source, assuming that database is Sql database using SqlCommand to return DataTable.
How can I modify the code to read a list of recipients from a data source and send an email message to the recipients using the DataSource ?
Here's the code I have so far:
Public Class DistributionList Dim smtpClient As New System.Net.Mail.SmtpClient() Dim sentEmails As Integer Public Function SendMail(ByVal mailMessage As System.Net.Mail.MailMessage) As Integer AddHandler smtpClient.SendCompleted, AddressOf SmtpClient_OnCompleted sentEmails = 0 Dim recipients As SqlDataSource = GetRecipients() For Each recipient As DataRow In recipients.Rows mailMessage.To.Clear() mailMessage.To.Add(New System.Net.Mail.MailAddress(recipient("UserName"), recipient("EmailAddress"))) SendMessage(mailMessage) Next Return sentEmails End Function Private Function GetRecipients() As SqlDataSource Dim table As New SqlDataSource() table.ConnectionString = ConfigurationManager.ConnectionStrings("ANGELIKAConnectionString1").ToString() table.SelectCommand = "Select FROM Valuation(UserName,EmailAddress) VALUES(@UserName,@EmailAddress)" table.SelectParameters.Add("UserName") table.SelectParameters.Add("EmailAddress") Dim row As DataRow = table.NewRow() row("UserName") = "John Doe" row("EmailAddress") = "John@example.com" table.Rows.Add(row) Return table End Function Private Sub SendMessage(ByVal mailMessage As System.Net.Mail.MailMessage) Dim userState As Object = mailMessage Try smtpClient.SendAsync(mailMessage, userState) Catch failedRec As System.Net.Mail.SmtpFailedRecipientsException 'Choose to resend? 'Log failure of send here. Catch smtpExc As System.Net.Mail.SmtpException 'Log failure of SMTP client here. Catch ex As Exception 'Log exception here. End Try End Sub Public Sub SmtpClient_OnCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Dim mailMessage As System.Net.Mail.MailMessage = CType(e.UserState, System.Net.Mail.MailMessage) If (e.Error Is Nothing) Then sentEmails = sentEmails + 1 Else 'Write to your error log here that an email failed. 'Possibly include information from the mailMessage. End If End Sub End ClassHopefully someone can help me on the way to adjust the code.
Thanks in advance.
I am waiting to hear from you as soon as possible Angelika.
e-mail : auroran@yandex.ru