【问题标题】:How can I make SMTP authenticated in C#如何使 SMTP 在 C# 中进行身份验证
【发布时间】:2010-09-22 20:07:39
【问题描述】:

我创建了使用 SMTP 发送消息的新 ASP.NET Web 应用程序。问题是 smtp 未从发送消息的人那里进行身份验证。

如何使 SMTP 在我的程序中通过身份验证? C# 是否有一个具有输入用户名和密码属性的类?

【问题讨论】:

    标签: c# authentication smtp


    【解决方案1】:
    using System.Net;
    using System.Net.Mail;
    
    using(SmtpClient smtpClient = new SmtpClient())
    {
        var basicCredential = new NetworkCredential("username", "password"); 
        using(MailMessage message = new MailMessage())
        {
            MailAddress fromAddress = new MailAddress("from@yourdomain.com"); 
    
            smtpClient.Host = "mail.mydomain.com";
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = basicCredential;
    
            message.From = fromAddress;
            message.Subject = "your subject";
            // Set IsBodyHtml to true means you can send HTML email.
            message.IsBodyHtml = true;
            message.Body = "<h1>your message body</h1>";
            message.To.Add("to@anydomain.com"); 
    
            try
            {
                smtpClient.Send(message);
            }
            catch(Exception ex)
            {
                //Error, could not send the message
                Response.Write(ex.Message);
            }
        }
    }
    

    你可以使用上面的代码。

    【讨论】:

    • 用户名和密码从哪里来?什么是 mail.mydomain.com ?那是他的 DNS 名称吗?
    • 它们是您的电子邮件地址和密码,mail.mydomain.com 是您的 SMTP 服务器(例如 smtp.gmail.com)。
    • 您应该将 MailMessage 对象包装在 using 语句中(或在完成后对其调用 Dispose),对吗?
    • 通常你还需要配置一个端口。这可以通过使用Port Property 来完成,例如smtpClient.Port(123)
    • @Arief:您好,您能检查一下我的问题吗? stackoverflow.com/questions/56604850/…关于如何使我们的发送邮件功能不作为外部发件人的任何建议?
    【解决方案2】:

    你如何发送消息?

    System.Net.Mail 命名空间中的类(这可能是您应该使用的)完全支持身份验证,可以在 Web.config 中指定,也可以使用 SmtpClient.Credentials 属性。

    【讨论】:

      【解决方案3】:

      在发送消息之前设置Credentials 属性。

      【讨论】:

        【解决方案4】:

        确保在调用SmtpClient.UseDefaultCredentials = false之后设置SmtpClient.Credentials

        顺序很重要,因为设置 SmtpClient.UseDefaultCredentials = false 会将 SmtpClient.Credentials 重置为 null。

        【讨论】:

        • 当使用 Sendgrid 并且顺序错误时 - 它会返回错误:“邮箱不可用。服务器响应是:不允许未经身份验证的发件人”。在 UseDefaultCredentials 标志修复此问题后设置凭据
        【解决方案5】:

        要通过 TLS/SSL 发送消息,需要将 SmtpClient 类的 Ssl 设置为 true。

        string to = "jane@contoso.com";
        string from = "ben@contoso.com";
        MailMessage message = new MailMessage(from, to);
        message.Subject = "Using the new SMTP client.";
        message.Body = @"Using this new feature, you can send an e-mail message from an application very easily.";
        SmtpClient client = new SmtpClient(server);
        // Credentials are necessary if the server requires the client 
        // to authenticate before it will send e-mail on the client's behalf.
        client.UseDefaultCredentials = true;
        client.EnableSsl = true;
        client.Send(message);
        

        【讨论】:

        • 写一些关于 SSL 与 SMTP 客户端的代码示例以获得更好的答案
        【解决方案6】:

        在我的情况下,即使遵循上述所有内容。我必须将我的项目从 .net 3.5 升级到 .net 4 才能授权我们的内部 Exchange 2010 邮件服务器。

        【讨论】:

          猜你喜欢
          • 2010-12-19
          • 2013-02-25
          • 1970-01-01
          • 2013-04-29
          • 1970-01-01
          • 1970-01-01
          • 2010-10-27
          • 2015-04-29
          • 2012-12-23
          相关资源
          最近更新 更多