How to send email with multiple addresses in C#

I'm trying to send emails using gmail's username and password in a Windows application. However, the following code is sending the mail to only the first email address when I collect multiple email address in my StringBuilder instance.

var fromAddress = new MailAddress(username, DefaultSender);
var toAddress = new MailAddress(builder.ToString());//builder reference having multiple email address

string subject = txtSubject.Text;
string body = txtBody.Text; ;
var smtp = new SmtpClient
{
    Host = HostName,
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(username, password),
    //Timeout = 1000000

};
var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body,
    IsBodyHtml = chkHtmlBody.Checked

};

if (System.IO.File.Exists(txtAttechments.Text))
{
    System.Net.Mail.Attachment attechment = new  Attachment(txtAttechments.Text);
    message.Attachments.Add(attechment);
}

if(this.Enabled)
    this.Enabled = false;

smtp.Send(message);

What am I doing wrong, and how can I sort out my problem?


Best bet is to message.To.Add() each of your MailAddress es individually. I think early versions of .Net were happier to parse apart comma or semicolon separated email addresses than the more recent runtime versions.


I was having the same problem.

The code is actually

message.To.Add("xxx@gmail.com, yyy@gmail.com");

this one can work in .net 3.5

if you use

message.To.Add( new MailAddress("xxx@gmail.com, yyy@gmail.com"));

this won't work in .net 3.5

链接地址: http://www.djcxy.com/p/51184.html

上一篇: Mathematica .Net / Link在Asp.Net应用程序中

下一篇: 如何在C#中使用多个地址发送电子邮件