MailCore2的电子邮件帐户

时间:2015-08-09 23:29:36

标签: swift gmail mailcore2

我创建了一个包含MailCore2 API的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!")
    }
} 

Google阻止了该电子邮件的发送,因为它认为劫持者试图访问我的帐户(因为我正常登录时处于不同的状态。)

我的问题是:有没有办法阻止Google阻止这些电子邮件被发送?我希望人们能够发送电子邮件,无论他们身在何处。如果这是不可能的,那么他们是否有一个电子邮件服务,它不会因为您(用户)从其他位置登录而阻止发送电子邮件?

提前感谢所有回复的人。

1 个答案:

答案 0 :(得分:2)

这个对我有用,希望它也适合你,试试这个,

    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")
        }
    }