Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Good day. I need an idea on how can I send mail to multiple recipient. I able to send it to single recipient. List of mail recipient will be getting from my sql query and thus I will send these mail recipient by a click of button. Appreciate if anyone would help.

Please find my code here for send mail function
VB
Public Function sendEmailToUser(ByVal emailaddress As String(), ByVal title As String, ByVal contents As String, Optional ByVal html As Boolean = False, Optional ByVal Engineer As String = "") As Boolean
        Dim email As MailMessage
        Dim smtp As SmtpClient
        Dim EmailCredentail As NetworkCredential
        Dim sender As MailAddress
        Dim replyTo As MailAddress
        Dim strSQL As New StringBuilder
        Dim ReplytoEmail As String = ConfigurationManager.AppSettings("Email").ToString
        Dim ReplytoName As String = ConfigurationManager.AppSettings("SenderName").ToString
        sender = New MailAddress(ConfigurationManager.AppSettings("Email").ToString, ConfigurationManager.AppSettings("SenderName").ToString)

        smtp = New SmtpClient(ConfigurationManager.AppSettings("SMTP").ToString, CInt(ConfigurationManager.AppSettings("Port").ToString))
        smtp.EnableSsl = CBool(ConfigurationManager.AppSettings("SSL").ToString)
        EmailCredentail = New NetworkCredential(ConfigurationManager.AppSettings("EmailAccount").ToString, ConfigurationManager.AppSettings("EmailPassword").ToString, "")
        smtp.UseDefaultCredentials = False
        smtp.Credentials = EmailCredentail
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network

        email = New MailMessage()
        email.Sender = sender
        email.From = sender
        replyTo = New MailAddress(ReplytoEmail, ReplytoName)
        email.ReplyTo = replyTo
        email.Subject = title
        email.Body = contents
        email.Headers.Add("Disposition-Notification-To", "test.test@gmail.com")

        If html = True Then
            email.IsBodyHtml = True
        Else
            email.IsBodyHtml = False
        End If
        For Each s As String In emailaddress
            Try
                email.To.Add(New MailAddress(s.Trim))
            Catch ex As Exception
                errorMessage(ex, "Error when add email address, may be email address wrong format.")
            End Try
        Next

        Try
            smtp.Send(email)
            Return True
        Catch ex As Exception
            errorMessage(ex, "Fail to send email at function sendEmailToUser.")
            Return False
        End Try

        Return False
    End Function
Posted
v2
Comments
Praveen Kumar Upadhyay 23-Dec-14 2:32am    
You can have the array of MailAddress and can pass multiple email ids
So, what is the issue in the below code...

For Each s As String In emailaddress
Try
email.To.Add(New MailAddress(s.Trim))
Catch ex As Exception
errorMessage(ex, "Error when add email address, may be email address wrong format.")
End Try
Next
Nikolai.Kimi 23-Dec-14 19:05pm    
Thanks Tadit...It works
Praveen Kumar Upadhyay 23-Dec-14 3:29am    
What is the values(email ids) of emailAddress object??

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900