Link to home
Start Free TrialLog in
Avatar of ASkEPA
ASkEPAFlag for Poland

asked on

How to send one mail to many recipients asp.net ?

Hello Experts.

How to send one mail to many recipients from my vb.net code ?
Included function works only for one recipient.


Function WyslijEmail_html_LISTA_OSOB(ByVal _pobierzdo As String, ByVal _temat As String, ByVal _tresc As String) As String
 
        Dim email_od As New MailAddress("obieg@epa", "Obieg Dokumentów")
        Dim email_do As New MailAddress(_pobierzdo, _pobierzdo)
        Dim msg As New MailMessage(email_od, email_do)
 
 
        msg.Body = _tresc
        msg.Subject = _temat
        msg.IsBodyHtml = True
        Dim mailSender As New System.Net.Mail.SmtpClient()
        mailSender.Host = "192.168.2.10"
 
        Try
            mailSender.Send(msg)
        Catch ex As Exception
            WyslijEmail_html_LISTA_OSOB = ex.ToString
        End Try
 
        Return WyslijEmail_html_LISTA_OSOB
    End Function

Open in new window

Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

To specify multiple recipients for a MailMessage, simply separate each recipient with a semicolon. For example:
mail.To = "me@mycompany.com;him@hiscompany.com;her@hercompany.com";

Source: http://www.systemwebmail.com/faq/2.6.aspx
Dim mail As New MailMessage()
mail.To = "me@mycompany.com;him@hiscompany.com;her@hercompany.com"
mail.From = "you@yourcompany.com"
mail.Subject = "this is a test email."
mail.Body = "this is my test email body."
SmtpMail.SmtpServer = "localhost" 'your real server goes here
SmtpMail.Send(mail)

Open in new window

Avatar of ASkEPA

ASKER

i got errors :
property to is read only in line :   mail.To = "me@mycompany.com;him@hiscompany.com;her@hercompany.com"

and value of type string cannot be converted to system.net.mail.mailaddres in line : mail.From = "you@yourcompany.com"

and : SmtpMail is not declarated in lines :

 SmtpMail.SmtpServer = "localhost" 'your real server goes here
 SmtpMail.Send(mail)
You can also add them separatly ...


Function WyslijEmail_html_LISTA_OSOB(ByVal _pobierzdo As String, ByVal _temat As String, ByVal _tresc As String) As String
 
        Dim email_od As New MailAddress("obieg@epa", "Obieg Dokumentów")
        Dim email_do As New MailAddress(_pobierzdo, _pobierzdo)
        Dim msg As New MailMessage(email_od, email_do)


        msg.Add("test@test.com")
        msg.Add("test2@test.Com")
 
 ...
Avatar of ASkEPA

ASKER

Can't do this because msg dosn't have add method.
What version of framework are you using ? I have just tested it here on my machine !

Sending Email from an ASP.NET 1.x Web Page
http://www.4guysfromrolla.com/webtech/080801-1.shtml

Sending Email in ASP.NET 2.0
http://aspnet.4guysfromrolla.com/articles/072606-1.aspx
.NET 2.0
        Dim mail As New System.Net.Mail.MailMessage
        mail.To.Add("test@test.com")
        mail.To.Add("test2@test.com")

.NET 1.1
        Dim mail2 As New System.Web.Mail.MailMessage
        mail2.To = "test@test.com; test2@test.com"
Avatar of ASkEPA

ASKER

I use .NET 2.0
Ok now this code works but i have to do this in one line "mail1@email.pl;mail2@email.pl" .... any idea how ?

        Dim ToAddress1 As String = "mail1@mail.pl"
        Dim toaddress2 As String = "mail2@mail.pl"
        Dim email_od As New MailAddress("obieg@epa", "Obieg Dokumentów")
        Dim mm As New System.Net.Mail.MailMessage
        mm.To.Add(ToAddress1)
        mm.To.Add(toaddress2)
        mm.From = email_od
        mm.Subject = "temat"
        mm.Body = "tre["
        mm.IsBodyHtml = True
        Dim mailSender As New System.Net.Mail.SmtpClient()
        mailSender.Host = "192.168.2.10"
        mailSender.Send(mm)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial