jstedfast / MailKit

A cross-platform .NET library for IMAP, POP3, and SMTP.

Home Page:http://www.mimekit.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot sign into GMail using SMTP

SetTrend opened this issue · comments

Describe the bug

Following this tutorial I am trying to send e-mails via GMail SMTP:

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

public void SendMailMessage(System.Net.Mail.MailMessage message, MySmtpConfiguration smtpConfig)
{
  using MimeMessage mail = (MimeMessage)message;
  using SmtpClient smtp = new SmtpClient();

  smtp.Connect(smtpConfig.HostName, smtpConfig.Port, SecureSocketOptions.StartTls);
  smtp.Authenticate(smtpConfig.UserName.ToLowerInvariant(), smtpConfig.Password);
  string _ = smtp.Send(new FormatOptions() { International = true }, mail);
  smtp.Disconnect(true);
}

I'm using exact the same credentials that I'm using for the GMail website (copy/paste). Yet, I when running above code, I always receive:

535: 5.7.8 Username and Password not accepted. Learn more at
5.7.8  https://support.google.com/mail/?p=BadCredentials b21-20020aa7d495000000b005231e3d89efsm6099603edr.31 - gsmtp

… when the following line is executed:

smtp.Authenticate(smtpConfig.UserName.ToLowerInvariant(), smtpConfig.Password);

Why doesn't this work?

Platform (please complete the following information):

  • OS: Windows
  • .NET Runtime: .NET 7
  • .NET Framework: .Net Core
  • MailKit Version: 4.1.0

Exception

535: 5.7.8 Username and Password not accepted. Learn more at
5.7.8  https://support.google.com/mail/?p=BadCredentials b21-20020aa7d495000000b005231e3d89efsm6099603edr.31 - gsmtp

   bei MailKit.Net.Smtp.SmtpClient.Authenticate(Encoding encoding, ICredentials credentials, CancellationToken cancellationToken)
   bei MailKit.MailService.Authenticate(Encoding encoding, String userName, String password, CancellationToken cancellationToken)
   bei MailKit.MailService.Authenticate(String userName, String password, CancellationToken cancellationToken)
   bei WebApi.EMailService.MailKitEMailService.SendMailMessage(MailMessage message, SmtpConfiguration smtpConfig) in
   ...

Configuration used

"SMTP":
{
  "Name": "Web Mailer",
  "UserName": "Caffee****ToGo@gmail.com",
  "Password": "*******",
  "HostName": "smtp.gmail.com",
  "Port": "587",
  "Security": "STARTTLS"
},

(Data partially masked using asterisks.)

Expected behavior

I should be able to send e-mail via GMail SMTP.

I'm using exact the same credentials that I'm using for the GMail website (copy/paste).

This is the problem :-)

GMail and Office365 are locking down access to IMAP, POP3 and SMTP and now require OAuth2 authentication. I believe that GMail still allows access via passwords, but only via what they call an "app-specific password".

I wrote up a FAQ answer for this problem here: https://github.com/jstedfast/MailKit/blob/master/FAQ.md#gmail-access

That answer includes instructions for creating an app-password as well.

@sdtedfast: Please pardon me for bothering you with this issue again, yet after I was following your FAQ, it still doesn' work.

I'm getting this "invalid redirection URL" error message, presented in a browser window – something that isn't available in a docker machine hosting my web service.

I don't think I quite understand how to send e-mails from my web service using a public, free e-mail provider.