How to authenticate to any mail provider using DotNetOpenAuth?

I developed an ASP.net application that can send an email to any domain. I am using a simple .Net smtp client:

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). But I can not authenticate for any modern mail providers (including gmail, yahoo, yandex) using .Net.Mail, because I have the following exceptionThe server response was: 5.5.1 Authentication Required

All correctly populated smtp configuration.

How can I use DotNetOpenAuth for authentication and .Net.Smtp for sending emails? Please give me an example.

+4
source share
4 answers

OK, I know that I am adding a lot of answers, but here are my conclusions, after looking at this for several hours:

The error is the client is not authenticated (5.5.1)

, ( gmail), Someone tried to sign in to your Google Account xxxxx@gmail.com from an app that doesn't meet modern securty standards.

Google: http://security.google.com/settings/security/activity , ?

, , : https://support.google.com/accounts/answer/185833

, , .

0

SMTP-. ? ? , Fiddler?

SendGrid, SMTP- . , ​​ SendGrid, SMTP- gmail.

, Mail4Net .

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

:

<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>
+4

, :

  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;
  }

, SendGrid .

0

, , :

However, you cannot know what configurations will be, and users will choose the wrong configurations. What will you do if the user selects TLS, but there is no TLS support from the mail server?

I think you need to add the "Send Test Email" button to send a test email to verify the configuration. Then you can identify any errors that occur.

0
source

Source: https://habr.com/ru/post/1625462/


All Articles