You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用MailKit+OAuth2通过Gmail SMTP发送邮件时持续出现5.7.8 BadCredentials错误

MailKit+OAuth2通过Gmail SMTP发送邮件时持续出现5.7.8 BadCredentials错误

我尝试用MailKit通过Gmail账户发送邮件,但一直遇到5.7.8 BadCredentials错误。

我的Google OAuth2凭据里指定了桌面应用项目名称为"EmailTesting",但代码里没有任何地方用到这个字符串,这会是问题吗?代码里用的credentials.json是直接从OAuth2凭据页面下载的。

更新:现在改用SaslMechanismOAuthBearer方法(因为GMAIL说明要用OAuthBearer),同时附上smtp.log日志。

我的代码如下:

public async Task Test4()
{
    UserCredential creds;
    using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
    {
        creds = await GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.FromStream(stream).Secrets,
            new[] { GmailService.Scope.GmailSend },
            "donotreply@INSERT_DOMAIN_HERE",
            CancellationToken.None);

        if (creds.Token.IsExpired(SystemClock.Default))
        {
            await creds.RefreshTokenAsync(CancellationToken.None);
        }
    }

    MailKit.Net.Smtp.SmtpClient client;
    client = new MailKit.Net.Smtp.SmtpClient();
    client.Connect("smtp.gmail.com", 465, MailKit.Security.SecureSocketOptions.SslOnConnect);
    client.Authenticate(new SaslMechanismOAuthBearer(creds.UserId, creds.Token.AccessToken));

    var builder = new MimeKit.BodyBuilder();
    builder.TextBody = "This is the body of the email";

    MimeMessage mail = new MimeKit.MimeMessage();
    mail.From.Add(MailboxAddress.Parse("donotreply@INSERT_DOMAIN_HERE"));
    mail.To.Add(MailboxAddress.Parse("erict@INSERT_DOMAIN_HERE"));
    mail.Subject = "this is the subject";
    mail.Body = builder.ToMessageBody();

    await client.SendAsync(mail);
    client.Disconnect(true);
    client.Dispose();
    mail.Dispose();
}

第一天运行时,页面跳转到网页让donotreply@INSERT_DOMAIN_HERE账户授权应用发送邮件,授权成功了。但从那之后,每次运行都会抛出5.7.8 Username and Password not accepted错误。

昨天一整天,Google的错误提示里的"Learn more"链接建议我添加恢复手机号——但我24小时前就已经添加并验证过了。今天早上,Google不再显示这个建议,但还是抛出同样的5.7.8错误。

SMTP日志输出如下:

S: 220 smtp.gmail.com ESMTP r12-20020a05621410cc00b0066d132b1c8bsm4912733qvs.102 - gsmtp
C: EHLO New-Laptop
S: 250-smtp.gmail.com at your service, [<IP-ADDRESS OF MY MACHINE>]
S: 250-SIZE 35882577
S: 250-8BITMIME
S: 250-STARTTLS
S: 250-ENHANCEDSTATUSCODES
S: 250-PIPELINING
S: 250-CHUNKING
S: 250 SMTPUTF8
C: STARTTLS
S: 220 2.0.0 Ready to start TLS
C: EHLO New-Laptop
S: 250-smtp.gmail.com at your service, [<IP-ADDRESS OF MY MACHINE>]
S: 250-SIZE 35882577
S: 250-8BITMIME
S: 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
S: 250-ENHANCEDSTATUSCODES
S: 250-PIPELINING
S: 250-CHUNKING
S: 250 SMTPUTF8
C: AUTH OAUTHBEARER ********
S: 334 eyJzdGF0dXMiOiJpbnZhbGlkX3JlcXVlc3QiLCJzY29wZSI6Imh0dHBzOi8vbWFpbC5nb29nbGUuY29tLyJ9
C: ********
S: 535-5.7.8 Username and Password not accepted. Learn more at...

备注:内容来源于stack exchange,提问作者erict

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 07:59:29