STMP 服务器的 MailKit /Google OAUTH 抛出异常“5.5.2 语法错误,再见”认证时
我正在尝试使用 oAuth2 通过 Google Smtp 服务器进行身份验证。
当我尝试与服务器进行身份验证时出现此错误: MailKit.Security.AuthenticationException: '555: 5.5.2 语法错误,再见。 g2-20020a056e021e0200b002c82a3f69cesm5461647ila.8 - gsmtp'
我看到其他人在尝试发送邮件时遇到此错误,但我还没有走到这一步。以下示例应用程序可以重现该错误。
在 Google 开发者控制台上,我创建了一个项目。我创建了一个服务帐户和一个密钥。我已启用 GMAIL API。
基本身份验证有效,但我正在准备让谷歌禁用基本身份验证。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using MailKit;
using MailKit.Net.Smtp;
using MailKit.Security;
namespace ConsoleAppToTestMail
{
internal class Program
{
private static void Main(string[] args)
{
SendMail();
}
private static void SendMail()
{
try
{
// service account email address
const string serviceAccount = "[email protected]";
// import service account key p12 certificate.
var certificate = new X509Certificate2("C:\\Certs\\smtp-support-60485-58c02993074a.p12",
"notasecret", X509KeyStorageFlags.Exportable);
// G Suite user email address
var gsuiteUser = "[email protected]";
var serviceAccountCredentialInitializer = new ServiceAccountCredential.Initializer(serviceAccount)
{
User = gsuiteUser,
Scopes = new[]
{
"../auth/gmail.send",
"https://mail.google.com/"
}
}.FromCertificate(certificate);
var credential = new ServiceAccountCredential(serviceAccountCredentialInitializer);
// request access token
var task = credential.RequestAccessTokenAsync(CancellationToken.None);
var success = task.Result;
if (!success)
throw new InvalidOperationException("Access token failed.");
var oauth2 = new SaslMechanismOAuth2(credential.User, credential.Token.AccessToken);
//var oauthBearer = new SaslMechanismOAuthBearer(credential.User, credential.Token.AccessToken);
var smtpClient = new SmtpClient(new ProtocolLogger("google.smtp.log"));
smtpClient.Connect("smtp.gmail.com", 587, SecureSocketOptions.Auto);
smtpClient.Authenticate(oauth2); // this is where the error occurs
// send mail goes here
}
catch (Exception ep)
{
Console.WriteLine(ep.ToString());
}
}
}
}
我取回的身份验证令牌是 1024 个字符。但以一长串句点结尾
ya29.c.b0AXv0zTNEf9xphDdsW_a-NtVGzk63Gj4vf4zaWyG3f-GLGOk82BREueuG41EmcjNZTo-RZxU6UWHFra2kDqv9estaLRuTDnf8yBHPYiLH2-lFYlEQcw2sxrx 0BMjL3cB01IzTII5XJR6mcQxeoYLAteB_IWovLBWnYreGIiCH3PTDos4gBFSACb1KNQ4wJqomlivoBefLulE1OHyaOesCqWonkaBcQw................................. ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... ...... ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... …………
我也尝试使用 OAuthBearer 得到相同的结果。
var oauthBearer = new SaslMechanismOAuthBearer(credential.User, credential.Token.AccessToken);
I am attempting to authenticate with a Google Smtp server using oAuth2.
I get this error when I attempt to Authenticate with the server:
MailKit.Security.AuthenticationException: '555: 5.5.2 Syntax error, goodbye. g2-20020a056e021e0200b002c82a3f69cesm5461647ila.8 - gsmtp'
I have seen others get this error when attempting to send the mail, but I'm not getting that far. The following sample app can reproduce the error.
On the Google Developer Console, I have Created a project. I created a service account and a Key. I have Enable the GMAIL API.
Basic Auth works, but I am preparing for google to disable Basic Auth.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using MailKit;
using MailKit.Net.Smtp;
using MailKit.Security;
namespace ConsoleAppToTestMail
{
internal class Program
{
private static void Main(string[] args)
{
SendMail();
}
private static void SendMail()
{
try
{
// service account email address
const string serviceAccount = "[email protected]";
// import service account key p12 certificate.
var certificate = new X509Certificate2("C:\\Certs\\smtp-support-60485-58c02993074a.p12",
"notasecret", X509KeyStorageFlags.Exportable);
// G Suite user email address
var gsuiteUser = "[email protected]";
var serviceAccountCredentialInitializer = new ServiceAccountCredential.Initializer(serviceAccount)
{
User = gsuiteUser,
Scopes = new[]
{
"../auth/gmail.send",
"https://mail.google.com/"
}
}.FromCertificate(certificate);
var credential = new ServiceAccountCredential(serviceAccountCredentialInitializer);
// request access token
var task = credential.RequestAccessTokenAsync(CancellationToken.None);
var success = task.Result;
if (!success)
throw new InvalidOperationException("Access token failed.");
var oauth2 = new SaslMechanismOAuth2(credential.User, credential.Token.AccessToken);
//var oauthBearer = new SaslMechanismOAuthBearer(credential.User, credential.Token.AccessToken);
var smtpClient = new SmtpClient(new ProtocolLogger("google.smtp.log"));
smtpClient.Connect("smtp.gmail.com", 587, SecureSocketOptions.Auto);
smtpClient.Authenticate(oauth2); // this is where the error occurs
// send mail goes here
}
catch (Exception ep)
{
Console.WriteLine(ep.ToString());
}
}
}
}
The Authentication token I get back is 1024 Characters. but ends with a long string of periods
ya29.c.b0AXv0zTNEf9xphDdsW_a-NtVGzk63Gj4vf4zaWyG3f-GLGOk82BREueuG41EmcjNZTo-RZxU6UWHFra2kDqv9estaLRuTDnf8yBHPYiLH2-lFYlEQcw2sxrx0BMjL3cB01IzTII5XJR6mcQxeoYLAteB_IWovLBWnYreGIiCH3PTDos4gBFSACb1KNQ4wJqomlivoBefLulE1OHyaOesCqWOnkaBcQw.........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
I have also tried using OAuthBearer with the same result.
var oauthBearer = new SaslMechanismOAuthBearer(credential.User, credential.Token.AccessToken);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论