Email Account For MailCore2

喜你入骨 提交于 2019-12-11 10:16:33

问题


I have created a Swift universal app that incorporates the MailCore2 API. It works perfectly on both debug and release mode. When I asked a friend in California to test it out, an alert view popped up with an error message. I found out that the reason for this was because I was using Google as the account I was emailing from.

Here is my code:

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 prevented the email from being sent because it thought a hijacker was trying to access my account (being that I am in a different state when I normally log in.)

My question is: Is there a way of preventing Google from blocking these emails from being sent? I would like people to be able to send emails, no matter where they are located. If this isn't possible, them is there an emailing service that doesn't prevent emails from being sent just because you (the users) are logging in from a different location?

Thanks in advance to all who reply.


回答1:


this one works for me, hope it works for you too, try this,

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


来源:https://stackoverflow.com/questions/31909821/email-account-for-mailcore2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!