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

ASP.NET

Avatar of undefined
Last Comment
Dirk Haest

8/22/2022 - Mon
Dirk Haest

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

ASKER
ASkEPA

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)
Dirk Haest

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")
 
 ...
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER
ASkEPA

Can't do this because msg dosn't have add method.
Dirk Haest

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
Dirk Haest

.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"
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
ASkEPA

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
Dirk Haest

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question