【问题标题】:Unable to send messages via SMTP mailkit and Gmail API using Service Account无法使用服务帐户通过 SMTP mailkit 和 Gmail API 发送消息
【发布时间】:2020-05-18 04:43:00
【问题描述】:

我已经工作了一段时间,试图让这个系统与 gmail api 系统和一个服务帐户一起工作(以避免创建一个空的用户帐户),但我不确定我哪里出错了.

        public static async Task<ServiceResponse> SendMail(ContactUsModel ContactInfo)
        {
            ServiceResponse response = new ServiceResponse();

            try
            {
                var certificate = new X509Certificate2(@"cert.p12", "notasecret", X509KeyStorageFlags.Exportable);
                var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer("service account")
                {
                    // Note: other scopes can be found here: https://developers.google.com/gmail/api/auth/scopes
                    Scopes = new[] { "https://mail.google.com" },
                    User = "service account"
                }.FromCertificate(certificate));

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

                // create an OAuth2 SASL context
                var oauth2 = new SaslMechanismOAuth2("service account", credential.Token.AccessToken);

                MimeMessage msg = new MimeMessage();
                msg.From.Add(new MailboxAddress(ContactInfo.Name, ContactInfo.EmailAddress));
                msg.To.Add(new MailboxAddress("Me", "business email account"));
                msg.Subject = ContactInfo.Subject;
                msg.Body = new TextPart("plain")
                {
                    Text = $"From: {ContactInfo.Name}\nEmail: {ContactInfo.EmailAddress}\nDate:{ContactInfo.AppointmentDate.ToString()}\nMessage: {ContactInfo.Message}"
                };

                using(SmtpClient client = new SmtpClient())
                {
                    client.ServerCertificateValidationCallback = (s, c, h, e) => true;
                    client.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);
                    client.Authenticate(oauth2);
                    client.Send(msg);
                    client.Disconnect(true);
                }

                response.Success = true;
                response.Message = "Message Sent";
            }
            catch (Exception e)
            {
                response.Success = false;
                response.Message = "An error occurred. Please call us.";
            }

            return response;
        }

我启用了 gmail api,并将服务帐户设置为云函数调用程序。

这是我目前遇到的错误,我不知道为什么。

{"status":"400","schemes":"Bearer","scope":"https://mail.google.com/"}

编辑: 更新代码并更新错误信息。

【问题讨论】:

    标签: c# asp.net-core gmail mailkit


    【解决方案1】:

    您使用了错误的范围。 “https://www.googleapis.com/auth/gmail.send”范围用于使用基于 HTTP 的协议库来发送消息。

    您需要“https://mail.google.com/”范围才能与 IMAP、POP3 和/或 SMTP 协议一起使用。

    【讨论】:

    • 更改范围后,我仍然收到相同的错误消息。
    • 我遇到的问题是服务帐户的电子邮件不匹配。我现在更新了上面的代码,带有一个我不太理解的新错误消息。另一个 SO 回答说这是他们的范围,但我使用的是相同的范围。
    猜你喜欢
    • 1970-01-01
    • 2023-02-19
    • 2011-03-17
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    • 2011-10-25
    • 2020-04-05
    • 2017-04-16
    相关资源
    最近更新 更多