C# 如何使用System.Net.Mail向多个地址发送电子邮件

C# 如何使用System.Net.Mail向多个地址发送电子邮件,c#,.net,smtp,C#,.net,Smtp,我有smtp电子邮件功能。它适用于单个地址,但在多个地址中存在问题 我使用下面的代码行传递多个地址 MailAddress to = new MailAddress("abc@gmail.com,xyz@gmail.com"); 请让我知道这个问题,因为我没有得到任何错误 MailMessage msg = new MailMessage(); msg.Body = ....; msg.To.Add(...); msg.To.Add(...); SmtpClient smtp = new S

我有smtp电子邮件功能。它适用于单个地址,但在多个地址中存在问题

我使用下面的代码行传递多个地址

MailAddress to = new MailAddress("abc@gmail.com,xyz@gmail.com");
请让我知道这个问题,因为我没有得到任何错误

MailMessage msg = new MailMessage();
msg.Body = ....;
msg.To.Add(...);
msg.To.Add(...);

SmtpClient smtp = new SmtpClient();
smtp.Send(msg);
To
是一个
MailAddressCollection
,因此您可以添加所需的地址数

如果需要显示名称,请尝试以下操作:

MailAddress to = new MailAddress(
    String.Format("{0} <{1}>",display_name, address));
maildaddress to=新的邮寄地址(
格式(“{0}”,显示名称,地址));
试试这个

using System;
using System.Net.Mail;

public class Test
{
    public static void Main()
    {
        SmtpClient client = new SmtpClient("smtphost", 25);
        MailMessage msg = new MailMessage("x@y.com", "a@b.com,c@d.com");
        msg.Subject = "sdfdsf";
        msg.Body = "sdfsdfdsfd";
        client.UseDefaultCredentials = true;
        client.Send(msg);
    }
}

Studiefg建议是有效的,但如果您想添加收件人姓名,请使用Marco在上面发布的内容,但首先是电子邮件地址,其次是显示姓名:

msg.To.Add(new MailAddress("your@email1.com","Your name 1"));
msg.To.Add(new MailAddress("your@email2.com","Your name 2"));

我认为您可以使用此代码来创建具有显示名称(也不同)的传出地址列表:

我已经习惯了接线员

try
{
    string s = textBox2.Text;
    string[] f = s.Split(',');

    for (int i = 0; i < f.Length; i++)
    {
        MailMessage message = new MailMessage(); // Create instance of message
        message.To.Add(f[i]); // Add receiver
        message.From = new System.Net.Mail.MailAddress(c);// Set sender .In this case the same as the username
        message.Subject = label3.Text; // Set subject
        message.Body = richTextBox1.Text; // Set body of message
        client.Send(message); // Send the message
        message = null; // Clean up
    }

}

catch (Exception ex)
{

    MessageBox.Show(ex.Message);
}
试试看
{
字符串s=textBox2.Text;
字符串[]f=s.Split(',');
对于(int i=0;i
解决此问题的我的代码:

private void sendMail()
{   
    //This list can be a parameter of metothd
    List<MailAddress> lst = new List<MailAddress>();

    lst.Add(new MailAddress("mouse@xxxx.com"));
    lst.Add(new MailAddress("duck@xxxx.com"));
    lst.Add(new MailAddress("goose@xxxx.com"));
    lst.Add(new MailAddress("wolf@xxxx.com"));


    try
    {


        MailMessage objeto_mail = new MailMessage();
        SmtpClient client = new SmtpClient();
        client.Port = 25;
        client.Host = "10.15.130.28"; //or SMTP name
        client.Timeout = 10000;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential("from@email.com", "password");
        objeto_mail.From = new MailAddress("from@email.com");

        //add each email adress
        foreach (MailAddress m in lst)
        {
            objeto_mail.To.Add(m);
        }


        objeto_mail.Subject = "Sending mail test";
        objeto_mail.Body = "Functional test for automatic mail :-)";
        client.Send(objeto_mail);


    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
private void sendMail()
{   
//此列表可以是metothd的一个参数
List lst=新列表();
添加(新邮件地址(“mouse@xxxx.com"));
添加(新邮件地址(“duck@xxxx.com"));
添加(新邮件地址(“goose@xxxx.com"));
添加(新邮件地址(“wolf@xxxx.com"));
尝试
{
MailMessage objeto_mail=新邮件();
SmtpClient=新的SmtpClient();
客户端端口=25;
client.Host=“10.15.130.28”//或SMTP名称
client.Timeout=10000;
client.DeliveryMethod=SmtpDeliveryMethod.Network;
client.UseDefaultCredentials=false;
client.Credentials=新系统.Net.NetworkCredential(“from@email.com“,”密码“);
objeto_mail.From=新邮件地址(“from@email.com");
//添加每个电子邮件地址
foreach(lst中的邮件地址m)
{
objeto_mail.To.Add(m);
}
objeto_mail.Subject=“发送邮件测试”;
objeto_mail.Body=“自动邮件功能测试:-”);
客户端发送(objeto_邮件);
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}

@Denish:use
msg.From
property。。。有什么问题吗?;)事实上,我有逗号分隔的地址列表。所以这个问题的另一个答案是合适的,但我没有找到任何方法来分配发件人地址的显示名称。您不需要单独添加每个收件人<代码>消息到添加(“abc@gmail.com,xyz@gmail.com");可以正常工作。有人知道
CC
可以包含多少地址的限制吗?我找不到有关MSDN的任何信息…请更好地格式化您的代码,并在发布之前尝试编译它。此
新SmtpClient(){enabHost=“smtp.gmail.com”}
将不会编译。发布可编译的代码。确保在发送后处理objecto_邮件。或者使用Using块。
namespace WebForms.Code.Logging {

    public class ObserverLogToEmail: ILog {
        private string from;
        private string to;
        private string subject;
        private string body;
        private SmtpClient smtpClient;
        private MailMessage mailMessage;
        private MailPriority mailPriority;
        private MailAddressCollection mailAddressCollection;
        private MailAddress fromMailAddress, toMailAddress;
        public MailAddressCollection toMailAddressCollection {
            get;
            set;
        }
        public MailAddressCollection ccMailAddressCollection {
            get;
            set;
        }
        public MailAddressCollection bccMailAddressCollection {
            get;
            set;
        }

        public ObserverLogToEmail(string from, string to, string subject, string body, SmtpClient smtpClient) {
            this.from = from;
            this.to = to;
            this.subject = subject;
            this.body = body;

            this.smtpClient = smtpClient;
        }

        public ObserverLogToEmail(MailAddress fromMailAddress, MailAddress toMailAddress,
        string subject, string content, SmtpClient smtpClient) {
            try {
                this.fromMailAddress = fromMailAddress;
                this.toMailAddress = toMailAddress;
                this.subject = subject;
                this.body = content;

                this.smtpClient = smtpClient;

                mailAddressCollection = new MailAddressCollection();

            } catch {
                throw new SmtpException(SmtpStatusCode.CommandNotImplemented);
            }
        }

        public ObserverLogToEmail(MailAddressCollection fromMailAddressCollection,
        MailAddressCollection toMailAddressCollection,
        string subject, string content, SmtpClient smtpClient) {
            try {
                this.toMailAddressCollection = toMailAddressCollection;
                this.ccMailAddressCollection = ccMailAddressCollection;
                this.subject = subject;
                this.body = content;

                this.smtpClient = smtpClient;

            } catch {
                throw new SmtpException(SmtpStatusCode.CommandNotImplemented);
            }
        }

        public ObserverLogToEmail(MailAddressCollection toMailAddressCollection,
        MailAddressCollection ccMailAddressCollection,
        MailAddressCollection bccMailAddressCollection,
        string subject, string content, SmtpClient smtpClient) {
            try {
                this.toMailAddressCollection = toMailAddressCollection;
                this.ccMailAddressCollection = ccMailAddressCollection;
                this.bccMailAddressCollection = bccMailAddressCollection;

                this.subject = subject;
                this.body = content;

                this.smtpClient = smtpClient;

            } catch {
                throw new SmtpException(SmtpStatusCode.CommandNotImplemented);
            }
        }#region ILog Members

        // sends a log request via email.
        // actual email 'Send' calls are commented out.
        // uncomment if you have the proper email privileges.
        public void Log(object sender, LogEventArgs e) {
            string message = "[" + e.Date.ToString() + "] " + e.SeverityString + ": " + e.Message;
            fromMailAddress = new MailAddress("", "HaNN", System.Text.Encoding.UTF8);
            toMailAddress = new MailAddress("", "XXX", System.Text.Encoding.UTF8);

            mailMessage = new MailMessage(fromMailAddress, toMailAddress);
            mailMessage.Subject = subject;
            mailMessage.Body = body;

            // commented out for now. you need privileges to send email.
            // _smtpClient.Send(from, to, subject, body);
            smtpClient.Send(mailMessage);
        }

        public void LogAllEmails(object sender, LogEventArgs e) {
            try {
                string message = "[" + e.Date.ToString() + "] " + e.SeverityString + ": " + e.Message;

                mailMessage = new MailMessage();
                mailMessage.Subject = subject;
                mailMessage.Body = body;

                foreach(MailAddress toMailAddress in toMailAddressCollection) {
                    mailMessage.To.Add(toMailAddress);

                }
                foreach(MailAddress ccMailAddress in ccMailAddressCollection) {
                    mailMessage.CC.Add(ccMailAddress);
                }
                foreach(MailAddress bccMailAddress in bccMailAddressCollection) {
                    mailMessage.Bcc.Add(bccMailAddress);
                }
                if (smtpClient == null) {
                    var smtp = new SmtpClient {
                        Host = "smtp.gmail.com",
                        Port = 587,
                        EnableSsl = true,
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        Credentials = new NetworkCredential("yourEmailAddress", "yourPassword"),
                        Timeout = 30000
                    };
                } else smtpClient.SendAsync(mailMessage, null);
            } catch (Exception) {

                throw;
            }
        }
    }
try
{
    string s = textBox2.Text;
    string[] f = s.Split(',');

    for (int i = 0; i < f.Length; i++)
    {
        MailMessage message = new MailMessage(); // Create instance of message
        message.To.Add(f[i]); // Add receiver
        message.From = new System.Net.Mail.MailAddress(c);// Set sender .In this case the same as the username
        message.Subject = label3.Text; // Set subject
        message.Body = richTextBox1.Text; // Set body of message
        client.Send(message); // Send the message
        message = null; // Clean up
    }

}

catch (Exception ex)
{

    MessageBox.Show(ex.Message);
}
private void sendMail()
{   
    //This list can be a parameter of metothd
    List<MailAddress> lst = new List<MailAddress>();

    lst.Add(new MailAddress("mouse@xxxx.com"));
    lst.Add(new MailAddress("duck@xxxx.com"));
    lst.Add(new MailAddress("goose@xxxx.com"));
    lst.Add(new MailAddress("wolf@xxxx.com"));


    try
    {


        MailMessage objeto_mail = new MailMessage();
        SmtpClient client = new SmtpClient();
        client.Port = 25;
        client.Host = "10.15.130.28"; //or SMTP name
        client.Timeout = 10000;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential("from@email.com", "password");
        objeto_mail.From = new MailAddress("from@email.com");

        //add each email adress
        foreach (MailAddress m in lst)
        {
            objeto_mail.To.Add(m);
        }


        objeto_mail.Subject = "Sending mail test";
        objeto_mail.Body = "Functional test for automatic mail :-)";
        client.Send(objeto_mail);


    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
 string[] MultiEmails = email.Split(',');
 foreach (string ToEmail in MultiEmails)
 {
    message.To.Add(new MailAddress(ToEmail)); //adding multiple email addresses
 }