【问题标题】:How to fix this error in sending emails by Outh2 using MailCore?如何在 Outh2 使用 MailCore 发送电子邮件时解决此错误?
【发布时间】:2016-07-01 22:41:04
【问题描述】:

我正在使用 mailcore 来使用访问令牌发送邮件。这在使用密码但使用访问令牌不起作用的情况下工作正常。我已经更改了端口和连接类型的每种组合,但仍然遇到同样的问题。我需要一些帮助。

 MCOSMTPSession * smtpSession = [[MCOSMTPSession alloc] init];
 smtpSession.hostname = @"smtp.gmail.com";
 smtpSession.port = 465;
 smtpSession.username = user; //saved value
 smtpSession.connectionType = MCOConnectionTypeTLS;
 smtpSession.OAuth2Token = token; //saved value(Validated)
 smtpSession.authType = MCOAuthTypeXOAuth2;
 smtpSession.checkCertificateEnabled=NO;

 MCOMessageBuilder * builder = [[MCOMessageBuilder alloc] init];
 MCOAddress *fromAdress=[[MCOAddress alloc]init];
 fromAdress = [MCOAddress addressWithMailbox:user];
 MCOAddress *toAdress=[[MCOAddress alloc]init];
 toAdress = [MCOAddress addressWithMailbox:self.to.text];
 [[builder header] setFrom:fromAdress];
 [[builder header] setTo:@[toAdress]];
 NSString *htmlbody1=@"Body";
 [[builder header] setSubject:@"Some_subject"];
 [builder setHTMLBody:htmlbody1];
 [smtpSession setConnectionLogger:^(void * connectionID,        MCOConnectionLogType type, NSData * data) {
 NSString *response = [[NSString alloc] initWithData:data    encoding:NSUTF8StringEncoding];
 NSLog(@"Response %@",response);
    }];
  NSData * rfc822Data = [builder data];

 MCOSMTPSendOperation *sendOperation = [smtpSession    sendOperationWithData:rfc822Data];
  [sendOperation start:^(NSError *error) {
if(error) {

    NSLog(@"Error sending email: %@", error);
} else {

}
  }];

我收到了这个回复 响应 535-5.7.8 用户名和密码不被接受。了解更多信息 535 5.7.8https://support.google.com/mail/answer/14257gg7sm1578291wjd.10 并得到这个错误 Error Domain=MCOErrorDomain Code=5 "无法使用当前会话的凭据进行身份验证。

【问题讨论】:

  • 您需要正确生成oauth2令牌。
  • 这是否意味着我们必须在打开会话之前使用 GTMOAuth2Authentication 类来获取最新的访问令牌 - 还是 mailcore 会为我们这样做?
  • 你有没有想过这个问题?我不完全了解 OAuth。使用我认为可以给我正确令牌但仍无法发送电子邮件的 GoogleSign。

标签: ios gmail-api smtpclient mailcore2 smtp-auth


【解决方案1】:

值得注意的是,如果您想通过 Gmail 发送电子邮件,那么您需要:

  1. 使用 Oauth(更多详细信息,请参见下文)
  2. 对于启用两步验证的用户,让他们设置一个App password,然后输入到应用中
  3. 让用户在其帐户中启用Less Secure Apps 选项

使用 OAuth

对于 OAuth,您可以使用 GTMAppAuth 之类的库,它允许您创建 OAuth 令牌以在您的应用程序中使用。但是,这样做时,您必须确保在请求访问时正确设置scope。这是取自the wiki的一些代码:

OIDAuthorizationRequest *request =
    [[OIDAuthorizationRequest alloc] initWithConfiguration:configuration
                                                  clientId:kClientID
                                              clientSecret:kClientSecret
                                                    scopes:@[OIDScopeOpenID, OIDScopeProfile]
                                               redirectURL:redirectURI
                                              responseType:OIDResponseTypeCode
                                      additionalParameters:nil];

通过 SMTP 发送

如果您想使用 SMTP 发送电子邮件,那么您必须添加 @"https://mail.google.com/" to the scopes` 参数。

请注意,在 2022 年,Google 似乎真的不希望您直接访问 SMTP。如果您在应用中使用@"https://mail.google.com/" 范围,则您的应用被视为Restricted,需要经过验证过程。

通过 Google API 发送

另一种方法是通过 Google API 发送。如果你想这样做,那么你需要@"https://www.googleapis.com/auth/gmail.send" 范围。这将允许您发送电子邮件,例如使用GTLR 库。

注意事项:

如果您尝试使用 @"https://www.googleapis.com/auth/gmail.send" 范围然后通过 SMTP 登录,您将收到错误 Username and Password not accepted。您不能将该范围用于 SMTP,只能用于 googleapis。

附:为什么谷歌不能更好地记录这一点,这超出了我的理解。我刚刚自愿向 Google 贡献了 20 个小时的时间来解决这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-29
    • 2022-06-14
    • 2023-04-05
    • 2013-04-17
    • 1970-01-01
    • 2015-04-18
    • 2011-04-28
    • 1970-01-01
    相关资源
    最近更新 更多