-1

Possible Duplicate:
Unable to send an email to multiple addresses/recipients using C#

I have used below code to send mail in script task

 string MailFromName = "Admin";
            System.Net.Mail.SmtpClient mailobj = new System.Net.Mail.SmtpClient();
            System.Net.Mail.MailAddress MailFrom = new System.Net.Mail.MailAddress(MailFromEmail, MailFromName);
            System.Net.Mail.MailAddress MailTo = new System.Net.Mail.MailAddress(MailToEmail, MailToEmail);
            System.Net.Mail.MailMessage mailmsg = new System.Net.Mail.MailMessage(MailFrom, MailTo);
            mailmsg.IsBodyHtml = true;
            mailmsg.Subject = strMessageSubject;
            mailmsg.Body = strMessageBody;
            mailobj.Host = strSMTPServerName;
            mailobj.Send(mailmsg);

It is working fine when I am using MailToEmail as "myaddress@myMail.com" i.e. for one email address

but this doesn't send any mail(also it dosen't fail) when I have multiple adress in to list

ex: "MyAdress@MyMail.com; MySecondAddress@MyMail.com"

How to resolve this?

EDIT New Code

 string MailFromName = "Admin";
            System.Net.Mail.SmtpClient mailobj = new System.Net.Mail.SmtpClient();
            System.Net.Mail.MailAddress MailFrom = new System.Net.Mail.MailAddress(MailFromEmail, MailFromName);
            System.Net.Mail.MailAddress MailTo = new System.Net.Mail.MailAddress(MailToEmail, MailToEmail);
            System.Net.Mail.MailMessage mailmsg = new System.Net.Mail.MailMessage(MailFrom, MailTo);
            mailmsg.IsBodyHtml = true;
            mailmsg.Subject = strMessageSubject;
            mailmsg.Body = strMessageBody;
            foreach (string str in multipleToMsg)
            {
                mailmsg.To.Add(str);
            }


            mailobj.Host = strSMTPServerName;
            mailobj.Send(mailmsg);
Cœur
  • 37,241
  • 25
  • 195
  • 267
Zerotoinfinity
  • 6,290
  • 32
  • 130
  • 206

3 Answers3

4

You've not shown how exactly you are adding the recipients. However to add multiple recipients you add to the "To" collection:

MailMessage message = new MailMessage();
message.To.Add("sillyjoe@stackoverflow.com");

"To" is a collection of MailAddresses. Make sure you are adding it to that collection and not attempting to concatenate email addresses all into one MailAddress object.

Arran
  • 24,648
  • 6
  • 68
  • 78
2

Accoring to MSDN: MailMessage Class the "To" property is a collection of MailAddresses

so you just need to do something like

mailmsg.To.Add(new System.Net.Mail.MailAddress(MailToEmail, MailToEmail)); 
mailmsg.To.Add(new System.Net.Mail.MailAddress(MailToEmail2, MailToEmail2))

or in a foreach loop

//get email addresses into a collection called emailAdds
foreach (var emailAdd in emailAdds)
{
    mailmsg.To.Add(new System.Net.Mail.MailAddress(emailAdd, emailAdd )); 
}
Mauro
  • 4,531
  • 3
  • 30
  • 56
0

To specify multiple addresses you need to use the To property which is a MailAddressCollection

message.To.Add("one@example.com, one@example.com"));
message.To.Add("two@example.com, two@example.com"));
Michael G
  • 6,695
  • 2
  • 41
  • 59