Swift MailCore2的电子邮件帐户

Swift MailCore2的电子邮件帐户,swift,gmail,mailcore2,Swift,Gmail,Mailcore2,我已经创建了一个包含MailCore2API的Swift通用应用程序。它在调试和发布模式下都能完美工作。当我请加利福尼亚的一位朋友测试它时,一个警告视图弹出了一条错误消息。我发现这样做的原因是因为我使用谷歌作为我发送电子邮件的账户 这是我的密码: var smtpSession = MCOSMTPSession() smtpSession.hostname = "smtp.gmail.com" smtpSession.username = "matt@gmail.com" smtpSession

我已经创建了一个包含MailCore2API的Swift通用应用程序。它在调试和发布模式下都能完美工作。当我请加利福尼亚的一位朋友测试它时,一个警告视图弹出了一条错误消息。我发现这样做的原因是因为我使用谷歌作为我发送电子邮件的账户

这是我的密码:

var smtpSession = MCOSMTPSession()
smtpSession.hostname = "smtp.gmail.com"
smtpSession.username = "matt@gmail.com"
smtpSession.password = "xxxxxxxxxxxxxxxx"
smtpSession.port = 465
smtpSession.authType = MCOAuthType.SASLPlain
smtpSession.connectionType = MCOConnectionType.TLS
smtpSession.connectionLogger = {(connectionID, type, data) in
    if data != nil {
        if let string = NSString(data: data, encoding: NSUTF8StringEncoding){
            NSLog("Connectionlogger: \(string)")
        }
    }
}

var builder = MCOMessageBuilder()
builder.header.to = [MCOAddress(displayName: "Rool", mailbox: "itsrool@gmail.com")]
builder.header.from = MCOAddress(displayName: "Matt R", mailbox: "matt@gmail.com")
builder.header.subject = "My message"
builder.htmlBody = "Yo Rool, this is a test message!"

let rfc822Data = builder.data()
let sendOperation = smtpSession.sendOperationWithData(rfc822Data)
sendOperation.start { (error) -> Void in
    if (error != nil) {
        NSLog("Error sending email: \(error)")
    } else {
        NSLog("Successfully sent email!")
    }
} 
谷歌阻止了这封电子邮件的发送,因为它认为有劫机者试图访问我的帐户(因为我在正常登录时处于不同的状态)

我的问题是:有没有办法阻止谷歌阻止这些电子邮件的发送?我希望人们能够发送电子邮件,无论他们在哪里。如果这是不可能的,他们是否有一个电子邮件服务,不阻止电子邮件发送仅仅因为你(用户)是从一个不同的位置登录


提前感谢所有回复的人

这个适合我,希望也适合你,试试这个

    let smtpSession:MCOSMTPSession = MCOSMTPSession()
    smtpSession.hostname = "smtp.gmail.com"
    smtpSession.port = 465
    smtpSession.username = "abc@gmail.com"
    smtpSession.password = "xxxxx"
    smtpSession.authType = MCOAuthType.SASLPlain
    smtpSession.connectionType = MCOConnectionType.TLS

    let builder:MCOMessageBuilder = MCOMessageBuilder()
    let from:MCOAddress = MCOAddress(displayName: "abc", mailbox: "abc@gmail.com")
    let to:MCOAddress = MCOAddress(displayName: nil, mailbox: "abc@gmail.com")
    builder.header.from = from
    builder.header.to = [to]
    builder.header.subject = "My Messgae"
    builder.htmlBody = "This is test message"
    let rcf822Data:NSData = builder.data()
    let sendOperation:MCOSMTPSendOperation = smtpSession.sendOperationWithData(rcf822Data)
    sendOperation.start { (error:NSError?) in
        if (error != nil) {
            print("Error Sending Mail : \(error)")
        }else{
            print("Mail Sent Successfully")
        }
    }

你能提供slog错误吗?如果你第一次使用SMTP,我想谷歌可能会显示这种消息。您应该向用户显示一条正确的错误消息和说明,让用户知道如何解除阻止。@iProgramIt您收到的错误消息是什么?这里的修复方法是什么?与上述实现有什么区别?