Migrating away from gmail smtp server or is there another option?

Viewed 220

Since Google discontinued the Less secure apps feature and made is more difficult to send e-mail using their smtp server, I've migrated over to another provider.

Now I'm getting The STMP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication required.

This code was working well with Gmail's outgoing SMTP but my new server is complaining. I've tried several code variations and the result is the same.

private void SendEmail()
{
  MailMessage message = new MailMessage();
  SmtpClient smtpClient = new SmtpClient("smtp-relay.sendinblue.com");
  message.From = new MailAddress("winrest_mail_service@winrest32.com");
  message.To.Add("sales@abspos.com");
  message.Subject = "NEW LICENCE REQUEST FROM " + ((User) this.Session["User"]).Name;
  message.Body = "LICENCE DEATIL" + Environment.NewLine + "SELLER = " + ((User) this.Session["User"]).Name + Environment.NewLine + "ID = " + this.TextBox__id.Text + Environment.NewLine + "NAME = " + this.TextBox__register_nam.Text + Environment.NewLine + "ADDRESS = " + this.TextBox__address.Text + Environment.NewLine + "LIC = " + this.TextBox__licence.Text + Environment.NewLine + "PDA = " + this.TextBox__pda.Text + Environment.NewLine + "CONTACT = " + this.TextBox__contact.Text + Environment.NewLine + "PHONE = " + this.TextBox__phone.Text + Environment.NewLine + "EMAIL = " + this.TextBox__email.Text + Environment.NewLine + "EXP = " + this.Calendar.SelectedDate.ToShortDateString() + Environment.NewLine + "AD SCREEN = " + this.CheckBox__AdScreen.Checked.ToString() + Environment.NewLine + "Biometrics = " + this.CheckBox__Biometrics.Checked.ToString() + Environment.NewLine + "Debit = " + this.CheckBox__Debit.Checked.ToString() + Environment.NewLine + "Draft = " + this.CheckBox__DraftControl.Checked.ToString() + Environment.NewLine + "KitchenScreen = " + this.CheckBox__KitchenScreen.Checked.ToString() + Environment.NewLine + "LiquorControl = " + this.CheckBox__LiquorControl.Checked.ToString() + Environment.NewLine + "WinAuthorize = " + this.CheckBox__WinAuthorize.Checked.ToString() + Environment.NewLine + "Lite = " + this.CheckBox__Lite.Checked.ToString() + Environment.NewLine + "CashNoBill = " + this.CheckBox__DisableCashNoBill.Checked.ToString();
  smtpClient.Port = 587;
  smtpClient.Credentials = (ICredentialsByHost) new NetworkCredential("mike@notrealcredentials.com", "XSnfc213213216");
  smtpClient.EnableSsl = true;
  smtpClient.Send(message);
}

Credentials have been altered for privacy reasons.

2 Answers

If you would like to stay with gmail smtp you have a few options. Either way your error

5.7.0 Authentication required.

Means that you need to be authentication in order to use that smtp server. Which means you would need to configure Oauth2. Something simlar to the xoauth version below that works with googles smtp server. I would check the docs for this new server it should tell you how to configure authorizaiton.

Google SMTP options below

The following are two ways to use googles smpt server after the removal of less secure apps feature.

xoauth2

The following code shows how to use XOauth2 with Googles smtp server. It requires that you create installed app credentials on Google cloud console.

using Google.Apis.Auth.OAuth2;
using Google.Apis.Util.Store;
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;


var path = @"C:\YouTube\dev\credentials.json";
var scopes = new[] { "https://mail.google.com/" };
var userName = "test2";

var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.FromFile(path).Secrets,
    scopes,
    userName,
    CancellationToken.None,
    new FileDataStore(Directory.GetCurrentDirectory(), true)).Result;

credential.GetAccessTokenForRequestAsync();

var message = new EmailMessage()
{
    From = "xxx@gmail.com",
    To = "xxx@gmail.com",
    MessageText = "test",
    Subject = "test"
};

try
{
    using (var client = new SmtpClient())
    {
        client.Connect("smtp.gmail.com", 465, true);
        
        var oauth2 = new SaslMechanismOAuth2 ("xxx@gmail.com", credential.Token.AccessToken);
        await client.AuthenticateAsync (oauth2, CancellationToken.None);
        
        client.Send(message.GetMessage());
        client.Disconnect(true);
    }

   
}
catch (Exception ex)
{
    int i = 1;  // throws MailKit.Security.AuthenticationException here
}


public class EmailMessage
{
    public string To { get; set; }
    public string From { get; set; }
    public string Subject { get; set; }
    public string MessageText { get; set; }

    public MimeMessage GetMessage()
    {
        var body = MessageText;
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("test", From));
        message.To.Add(new MailboxAddress("test", To));
        message.Subject = Subject;
        message.Body = new TextPart("plain") { Text = body };
        return message;
    }
}

Apps password

Another option is to create an apps password and use that in place of the password you were using previously.

using (var client = new SmtpClient())
    {
        client.Connect("smtp.gmail.com", 465, true);
        client.Authenticate("xxxx@gmail.com", "appspassword");
        client.Send(message.GetMessage());
        client.Disconnect(true);
    }

Quick fix for SMTP username and password not accepted error

Looks like apps password worked. You need to activate 2 factor authentication and then google gives you a 16 digit password. Thanks.

Related