c# - 发送本地邮件到hotmail问题

标签 c# asp.net .net frameworks hotmail

我有以下代码,导致“未指定 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/

相关文章:

c# - 紧凑框架: Fill Rectangle with solidbrush problem

asp.net - 如何捕获或拦截ODataException以返回自定义错误有效载荷

.net - 如何在 WPF 中的 TextBlock 上应用 CharacterCasing?

c# - .Net 开发与 SQLServer 的配对在 ORM 方面有什么优势?

c# - 数据库连接不工作 - System.Data.SqlClient.SqlException : No process is on the other end of the pipe

c# - 我无法访问数组的 Count 属性,而是通过转换为 ICollection!

c# - 串行端口 : Send in Bytes or Characters?

c# - 我怎样才能获得对当前事件模态形式的引用?

asp.net - 在 asp.net mvc 中读取 html 页面

c# - 在上传发生之前确保上传文件的文件大小小于 maxRequestLength?