【发布时间】:2019-11-18 06:37:20
【问题描述】:
我的应用使用 Mailcore2 通过 Gmail 发送电子邮件。我也为此使用 OAuth2 身份验证。如果选择 FULL 访问范围,则效果很好:https://mail.google.com
但是,Google 建议使用其他范围(发送)。我正在尝试https://www.googleapis.com/auth/gmail.send 和/或https://www.googleapis.com/auth/gmail.compose
Mailcore2 不发送这些范围返回:
MCOSMTPResponseCodeKey=535, NSLocalizedDescription=Unable to authenticate with the current session's credentials., MCOSMTPResponseKey=5.7.8 Username and Password not accepted.
MailCore2 是否需要完全访问范围? 有什么解决方法吗?
这是我使用 MailCore2 代码发送的,仅适用于 FULL 范围:
MCOSMTPSession *smtpSession = [[MCOSMTPSession alloc] init];
smtpSession.hostname = @"smtp.gmail.com";
smtpSession.port = 465;
[smtpSession setAuthType:MCOAuthTypeXOAuth2];
[smtpSession setOAuth2Token:delAuthAccessToken];//from oauth2
[smtpSession setUsername:delAuthUserEmail];
smtpSession.authType =MCOAuthTypeXOAuth2;
smtpSession.connectionType=MCOConnectionTypeTLS;
MCOMessageBuilder *builder = [[MCOMessageBuilder alloc] init];
NSMutableArray *to = [[NSMutableArray alloc] init];
[to addObject:[MCOAddress addressWithDisplayName:delSendName mailbox:delSendEmail]];
MCOAddress *from = [MCOAddress addressWithDisplayName:delAuthUserEmail mailbox:delAuthUserEmail];
[[builder header] setFrom:from];
[[builder header] setTo:to];
[[builder header] setSubject:@"Sent By MyApp"];
[builder setTextBody:delMessage];
NSData * rfc822Data = [builder data];
MCOSMTPSendOperation *sendOperation =[smtpSession sendOperationWithData:rfc822Data];
[sendOperation start:^(NSError *error) {
if(error) {
NSLog(@"Error sending email: %@", error);
} else {
NSLog(@"Message sent sucessfully.");
}
}];
【问题讨论】:
标签: objective-c gmail-api mailcore2