简体   繁体   English

如何使用DotNetOpenAuth对任何邮件提供商进行身份验证?

[英]How to authenticate to any mail provider using DotNetOpenAuth?

I developed an ASP.net application that can send an email to any domain. 我开发了一个ASP.net应用程序,可以向任何域发送电子邮件。 I'm using a simple .Net smtp client: 我正在使用一个简单的.Net smtp客户端:

var fromAddress = new MailAddress("mymailid@gmail.com");
var fromPassword = "xxxxxx";
var toAddress = new MailAddress("yourmailid@yourdoamain.com");

var smtpClient = new System.Net.Mail.SmtpClient
    {
        Host = "smtp.gmail.com",// or any others
        Port = 587, // correspond to host
        EnableSsl = true,
        DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
        UseDefaultCredentials = false,
        Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
    };

smtpClient.Send(message) . smtpClient.Send(message) But I cannot authenticate to any modern mail providers(including gmail, yahoo, yandex) by using .Net.Mail because I have got the following exception The server response was: 5.5.1 Authentication Required 但我无法使用.Net.Mail对任何现代邮件提供商(包括gmail,yahoo,yandex)进行身份验证,因为我遇到以下异常The server response was: 5.5.1 Authentication Required

All of the smtp configuration fill-out correctly. 所有smtp配置都正确填写。

How can I use DotNetOpenAuth for authentication and .Net.Smtp for sending emails? 如何使用DotNetOpenAuth进行身份验证,使用.Net.Smtp进行发送电子邮件? Please give me an example. 请举个例子。

I'd consider your SMTP server. 我会考虑你的SMTP服务器。 Is it allowing you to send email? 是否允许您发送电子邮件? Is your request making it to the server and being validated? 您的请求是否已发送到服务器并进行验证? Have you looked at the traffic using Fiddler ? 你用Fiddler看过流量了吗?

You can sign up for a free account with SendGrid , who will provide you with a SMTP server to send the email for you. 您可以使用SendGrid注册一个免费帐户,该帐户将为您提供SMTP服务器以便为您发送电子邮件。 You really are better of using a company like SendGrid instead of the gmail SMTP server. 你真的更喜欢使用像SendGrid这样的公司而不是gmail SMTP服务器。

Other than that, Mail4Net might help you in your code. 除此之外, Mail4Net可能会帮助您完成代码。

To use GMail as a SMTP Server, you have to set some values: 要将GMail用作SMTP服务器,您必须设置一些值:

Outgoing Mail (SMTP) Server: smtp.gmail.com
Use Authentication: Yes
Use Secure Connection: Yes (this can be TLS or SSL depending on your mail client)
Username: your GMail account, i.e. user@gmail.com
Password: your GMail password
Port: 465 or 587

You can also move your connection to the web or app config file: 您还可以将连接移动到Web或app配置文件:

<configuration>

  <system.net>
    <mailSettings>
      <smtp deliveryMethod="network" from="pam@gmail.com">
        <network
          host="smtp.gmail.com"
          port="465"
          enableSsl="true"
          userName="pam@gmail.com"
          password="password"
        />
      </smtp>
    </mailSettings>
  </system.net>

</configuration>

This code works for me, once the email and credentials are updated: 一旦电子邮件和凭证更新,此代码适用于我:

  try
  {

    var message = new MailMessage();

    message.Subject = "Test email";
    message.Body = "This is a test message";
    message.From = new MailAddress("from@email.com");
    message.To.Add(new MailAddress("to@email.com"));

    var client = new SmtpClient
    {
      Host = "smtp.sendgrid.com",
      Port = 587,
      Credentials = new NetworkCredential("User_Name", "My_Password")
    };

    client.Send(message);

  }
  catch (Exception ex)
  {
    var msg = ex.Message;
  }

Make sure your SendGrid credentials are correct. 确保您的SendGrid凭据正确无误。

From the comments, I can see the image , showing what you are trying to do: 从评论中,我可以看到图像 ,显示您要做的事情:

However, you cannot know what the configurations are going to be and users will select configurations that are not correct. 但是,您无法知道配置将是什么,用户将选择不正确的配置。 What will you do if the user selects TLS, but there is no TLS support from the mail server? 如果用户选择TLS,您会怎么做,但邮件服务器没有TLS支持?

I think you need to add a Send Test Email button, to send a test email to check the configuration. 我认为您需要添加“发送测试电子邮件”按钮,以发送测试电子邮件以检查配置。 You can then show any errors that are encountered. 然后,您可以显示遇到的任何错误。

OK, I know I am adding a lot of answers, but here are my findings after looking at this for several hours: 好的,我知道我正在添加很多答案,但是在看了几个小时之后,这是我的发现:

The error is the client is not authenticated (5.5.1)

I got the same error, then I noticed an email in my inbox (the gmail account is seldom used) saying Someone tried to sign in to your Google Account xxxxx@gmail.com from an app that doesn't meet modern securty standards. 我收到了同样的错误,然后我注意到收件箱中有一封电子邮件(很少使用gmail帐户), Someone tried to sign in to your Google Account xxxxx@gmail.com from an app that doesn't meet modern securty standards.

Is you app blocked by google: http://security.google.com/settings/security/activity might show your app, but are you getting an email? 您的应用是否被谷歌屏蔽: http//security.google.com/settings/security/activity可能会显示您的应用,但是您收到了电子邮件吗?

Also, if the account uses 2 step authentication, the email will not be sent without setting an application specific password: https://support.google.com/accounts/answer/185833 此外,如果该帐户使用两步验证,则不会在未设置应用专用密码的情况下发送电子邮件: https//support.google.com/accounts/answer/185833

I think this is account configuration instead of a problem with your code. 我认为这是帐户配置而不是代码问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM