Click here to Skip to main content
14,770,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Protected Sub SendMail_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles SendMail.Click

        Try
            Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            SmtpServer.Credentials = New  _
Net.NetworkCredential("fiona@gmail.com", "donkeyboat")
            SmtpServer.Port = 587
            SmtpServer.Host = "smtp.gmail.com"
            SmtpServer.EnableSsl = True
            mail = New MailMessage()
            mail.From = New MailAddress("fiona@gmail.com")
            mail.To.Add("fionatyl@hotmail.com")
            mail.Subject = "Test Mail"
            mail.IsBodyHtml = True
            mail.Body = "This is for testing SMTP mail from GMAIL"

            SmtpServer.Send(mail)
            MsgBox("Mail Send")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

Above is my current code. I would like to do changes for the line to send the mail to. right now i can only send it to one recipient. how do i edit it so that i can send it to many other recipients separated with ';' ?
urgent help is needed. thank you
Posted
Updated 14-Jun-12 11:40am
v2

Try this:

string addresses = "abc@123.com;def@123.com;ghi@123.com";
foreach(string address in addresses.split(';'))
  mail.To.Add(address)
   
Comments
Maciej Los 14-Jun-12 17:52pm
   
Good answer, my 5!
VJ Reddy 14-Jun-12 20:35pm
   
The answer is good.
But Add method accepts comma separated addresses string as shown in answer, which is succinct I think.
VJ Reddy 14-Jun-12 20:30pm
   
Good answer. 5!
But Add method accepts comma separated addresses string as shown in answer.
Manas Bhardwaj 15-Jun-12 7:31am
   
5ed!
The MailMessage.To property returns a MailMessageCollection as explained here http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.to[^].

The Add method of MailMessageCollection accepts a string of mail addresses separated by comma character as explained here http://msdn.microsoft.com/en-us/library/ms144695.aspx[^]

Hence, to send mail to multiple mail addresses use Add method of To property of MailMessage object with the Addresses in a string separated by , as shown below

mail.To.Add("add1@hotmail.com,add2@hotmail.com,add3@hotmail.com")
   
Comments
Manas Bhardwaj 15-Jun-12 7:30am
   
5ed!
VJ Reddy 15-Jun-12 7:34am
   
Thank you, Manas :)
Espen Harlinn 23-Jun-12 4:07am
   
5'ed! :-D
VJ Reddy 23-Jun-12 8:10am
   
Thank you, Espen :)

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