-1

可能重复:
无法使用 C# 向多个地址/收件人发送电子邮件

我使用下面的代码在脚本任务中发送邮件

 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);

当我将 MailToEmail 用作“myaddress@myMail.com”即一个电子邮件地址时,它工作正常

但是当我有多个地址要列出时,这不会发送任何邮件(也不会失败)

例如:“MyAdress@MyMail.com;MySecondAddress@MyMail.com”

如何解决这个问题?

编辑新代码

 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);
4

3 回答 3

4

您尚未显示添加收件人的准确程度。但是,要添加多个收件人,请添加到“收件人”集合:

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

“收件人”是邮件地址的集合。确保将其添加到该集合中,而不是尝试将电子邮件地址全部连接到一个 MailAddress 对象中。

于 2012-09-11T13:51:04.430 回答
2

根据MSDN:MailMessage 类,“To”属性是 MailAddresses 的集合

所以你只需要做类似的事情

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

或在 foreach 循环中

//get email addresses into a collection called emailAdds
foreach (var emailAdd in emailAdds)
{
    mailmsg.To.Add(new System.Net.Mail.MailAddress(emailAdd, emailAdd )); 
}
于 2012-09-11T13:52:00.150 回答
0

要指定多个地址,您需要使用作为MailAddressCollection的 To 属性

message.To.Add("one@example.com, one@example.com"));
message.To.Add("two@example.com, two@example.com"));
于 2012-09-11T13:51:30.500 回答