我有以下代码,导致“未指定 SMTP 主机”。 有什么想法为什么会发生这种情况吗? 非常感谢
var mailMessage = new System.Net.Mail.MailMessage();
mailMessage.To.Add(new MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3c514559515d55507c545348515d5550125f53124957" rel="noreferrer noopener nofollow">[email protected]</a>"));
mailMessage.From = new MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b2a3f2e383f0b3f2e383f65282426" rel="noreferrer noopener nofollow">[email protected]</a>");
mailMessage.Subject = "my test subject";
mailMessage.Body = "my test body";
mailMessage.IsBodyHtml = true;
var smtpClient = new SmtpClient { EnableSsl = true };
object userState = mailMessage;
smtpClient.Send(mailMessage);
我已经尝试了以下方法,但仍然失败
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bcd1d9fcdbd1ddd5d092dfd3d1" rel="noreferrer noopener nofollow">[email protected]</a>", "password"),
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false
};
var mail = new MailMessage("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3a4e5f494e7a5f425b574a565f14595557" rel="noreferrer noopener nofollow">[email protected]</a>", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4a9a184a3a9a5ada8eaa7aba9" rel="noreferrer noopener nofollow">[email protected]</a>", "hello", "there");
mail.Body = "Hello";
mail.Subject = "hi";
client.Send(mail);
最佳答案
因为您没有指定 smtpClient.Host属性。
关于c# - 发送本地邮件到hotmail问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9361550/