How to send an email in .Net according to new security policies?

Solution 1:

The general solution is https://galleryserverpro.com/use-gmail-as-your-smtp-server-even-when-using-2-factor-authentication-2-step-verification/

1) Use a browser to log in to your Google account and go to your Sign-in & security settings. Look for the 2-step verification setting.

2) If 2-step verification is off and you want to keep it that way that's mean that you will need to implement many auth mechanism as you said.

Solution: Turn it on and then generate and use google app password. It should work! You don't need to use other libraries like mailkit.

Solution 2:

How to get usersOAuthToken?

The first thing you need to do is follow Google's instructions for obtaining OAuth 2.0 credentials for your application.

Once you've done that, the easiest way to obtain an access token is to use Google's Google.Apis.Auth library:

using System;
using System.Threading;
using System.Security.Cryptography.X509Certificates;

using Google.Apis.Auth.OAuth2;

using MimeKit;
using MailKit.Net.Smtp;
using MailKit.Security;

namespace Example {
    class Program
    {
        public static async void Main (string[] args)
        {
            var certificate = new X509Certificate2 (@"C:\path\to\certificate.p12", "password", X509KeyStorageFlags.Exportable);
            var credential = new ServiceAccountCredential (new ServiceAccountCredential
                .Initializer ("[email protected]") {
                    // Note: other scopes can be found here: https://developers.google.com/gmail/api/auth/scopes
                    Scopes = new[] { "https://mail.google.com/" },
                    User = "[email protected]"
                }.FromCertificate (certificate));

            // Note: result will be true if the access token was received successfully
            bool result = await credential.RequestAccessTokenAsync (CancellationToken.None);

            if (!result) {
                Console.WriteLine ("Error fetching access token!");
                return;
            }

            var message = new MimeMessage ();
            message.From.Add (new MailboxAddress ("Your Name", "[email protected]"));
            message.To.Add (new MailboxAddress ("Recipient's Name", "[email protected]"));
            message.Subject = "This is a test message";

            var builder = new BodyBuilder ();
            builder.TextBody = "This is the body of the message.";
            builder.Attachments.Add (@"C:\path\to\attachment");

            message.Body = builder.ToMessageBody ();

            using (var client = new SmtpClient ()) {
                client.Connect ("smtp.gmail.com", 587, SecureSocketOptions.StartTls);

                // use the access token as the password string
                client.Authenticate ("[email protected]", credential.Token.AccessToken);

                client.Send (message);

                client.Disconnect (true);
            }
        }
    }
}

Is it the best practice technique to use Google.Apis.Auth.OAuth2?

Why wouldn't you use their API for getting an authentication token? It seems like the best way to get it to me...

Will I can send an emails to other not gmail accounts?

Yes, any email you send through GMail can be sent to any other email address - it doesn't have to only be to other GMail addresses.

Solution 3:

Authentication errors happen when using the Gmail smtp server from applications not implementing Google’s specific security requirements. In the Gmail account settings, turn on: "Sign-in & security" > "Connected apps & sites" > “Allow less secure apps” > On