'参数'地址'不能是带有2条消息的空字符串

时间:2014-08-06 13:24:52

标签: c# asp.net email smtpclient


我尝试从相同的代码隐藏发送2封电子邮件,但它给了我这个错误:

  

参数'地址'不能是一个空字符串。

如果我删除了此消息中的1条,它会毫无问题地发送它......为什么? 这是我的代码:

MailMessage message;
string body;
SmtpClient sc;
message = new MailMessage();
message.From = new MailAddress("address@address.ext");
message.IsBodyHtml = true;
sc = new SmtpClient("smtp.smtp.smtp");
sc.Port = 25;
sc.Credentials = new NetworkCredential("address@address.ext", "password");
sc.EnableSsl = true;
System.Net.ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
message.To.Add("address@address.ext");
message.Subject = "This is the subject";

MailMessage message2;
string body2;
message2 = new MailMessage();
message2.From = new MailAddress("address@address.ext");
message2.IsBodyHtml = true;
message2.To.Add("address@address.ext");
message2.Subject = "This is the subject2";
body2 = HttpUtility.HtmlDecode("This is the body2");
message2.Body = body2;
sc.Send(message2);

body = HttpUtility.HtmlDecode("This is the body");
message.Body = body;
sc.Send(message);

我也尝试使用2 sc,称为" sc2"与第一个相同的参数,但我收到相同的错误。 我希望你能帮助我。 谢谢! 丹尼尔

1 个答案:

答案 0 :(得分:0)

当您使用message2.To.Add()时,您需要创建一个新的MailAddress,因此您的代码应该更像message.To.Add(new MailAddress(address@address.ext));,而message2则相同。

我认为这是问题所在。让我知道

修改

MailMessage message;
string body;
SmtpClient sc;
message = new MailMessage();
message.From = new MailAddress("address@address.ext");
message.IsBodyHtml = true;
sc = new SmtpClient("smtp.smtp.smtp");
sc.Port = 25;
sc.Credentials = new NetworkCredential("address@address.ext", "password");
sc.EnableSsl = true;
System.Net.ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
message.To.Add("address@address.ext");
message.Subject = "This is the subject";

MailMessage message2;
string body2;
message2 = new MailMessage();
message2.From = new MailAddress("address@address.ext");
message2.IsBodyHtml = true;
message2.To.Add("address@address.ext");
message2.Subject = "This is the subject2";
body2 = "This is the body2"
message2.Body = body2;
sc.Send(message2);

body = "This is the body";
message.Body = body;
sc.Send(message);

我把身体改成了一个普通的字符串。