views:

37

answers:

4

I have a sendmail funciton that works for one recipient. If I pass something like "[email protected];[email protected]" in ToEmail then I get an error that says ; not allowed in 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
        If Clng0(AppConfig("isProduction")) = 1 Then
            smtp.Host = "p3smtpout.secureserver.net"
        Else
            smtp.Host = "FPSRVR"
        End If

        smtp.Send(msgMail)
        SendMail = True
    Catch ex As Exception
        DoTrace(ex.Source, ex.Message)
        SendMail = False
    End Try
End Function
A: 

change your toEmail from string to MailAddressCollection and you are done

lakhlaniprashant.blogspot.com
+2  A: 

The addresses need to be separated with commas, not semicolons.

tvanfosson
+1 Wish I had posted this answer first before double checking :).
GenericTypeTea
That worked perfecty, Thanks!
+4  A: 

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

If you're passing your email addresses into your function as a semi-colon separated list, then just do a String.Split() on them, and add them into the To property in an iteration loop.

Coding Gorilla
+1  A: 

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

It also fixes a bunch of bugs in the .NET mail classes.

Remy