可能重复:
Unable to send an email to multiple addresses/recipients using 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”,即一个电子邮件地址
时工作正常但当我在列表中有多个地址时,这不会发送任何邮件(也不会失败)
ex:“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);
答案 0 :(得分:4)
您尚未显示添加收件人的确切方式。但是,要添加多个收件人,请添加到“收件人”集合:
MailMessage message = new MailMessage();
message.To.Add("sillyjoe@stackoverflow.com");
“To”是MailAddresses的集合。确保将其添加到该集合中,而不是尝试将电子邮件地址全部连接到一个MailAddress对象中。
答案 1 :(得分:2)
根据MSDN: MailMessage Class,“To”属性是Mail Addresses的集合
所以你只需要做一些像
这样的事情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 ));
}
答案 2 :(得分:0)
要指定多个地址,您需要使用To属性MailAddressCollection
message.To.Add("one@example.com, one@example.com"));
message.To.Add("two@example.com, two@example.com"));