How to send an email to multiple recipients using asp.net?

I have a sendmail function that works for a single recipient. If I pass something like " email1@test.com ; email2@test.com " to ToEmail, then I get an error message; not allowed in the message header. What am I doing wrong?

Here is my SendMail function:

Public Function SendMail(ByVal ToEmail As String, ByVal FromEmail As String, ByVal Subject As String, ByVal Body As String, Optional ByVal bccEmail As String = "", Optional ByVal bIsHTML As Boolean = False) As Boolean Try Dim msgMail As New MailMessage(FromEmail, ToEmail, Subject, Body) msgMail.IsBodyHtml = bIsHTML If bccEmail <> "" Then msgMail.Bcc.Add(bccEmail) End If Dim smtp As New SmtpClient smtp.Host = "myServer" smtp.Send(msgMail) SendMail = True Catch ex As Exception DoTrace(ex.Source, ex.Message) SendMail = False End Try End Function 
+4
source share
4 answers

Addresses should be separated by commas, not a semicolon.

+6
source

You need to use the To property, which is MailAddressCollection , and call the Add() method to add the email addresses individually.

If you pass your email addresses to your function as a colon delimited list, then just do String.Split() on them and add them to the To property in the iteration loop.

+5
source

I would recommend using MailMergeLib
http://www.codeproject.com/KB/IP/MailMergeLib.aspx

It also fixes many bugs in .NET classes.

+1
source

change your toEmail from line to MailAddressCollection and you're done

0
source

All Articles