Is there a library “SKPSMTPMessage” for Swift?

本秂侑毒 提交于 2019-12-11 02:19:41

问题


Is there any SMPSKMPMessage library to use in Swift? earlier i have used this tutorial which is in objective - c. What I want to do is send a mail in background using Swift.


回答1:


I don't know any email sending library in Swift, but you can definitely use the one described in the Objective-C tutorial in your Swift application. All Objective-C libraries are cross-compatible with Swift code: just use a bridging header.




回答2:


Simply integrate skpsmtpmessage with cocoa pods by adding:

pod 'skpsmtpmessage'

to your Podfile and then this is how you send an email from code:

import skpsmtpmessage

class MailSender: NSObject, SKPSMTPMessageDelegate {
    static let shared = MailSender()

    func sendEmail(subject: String, body: String) {
        let message = SKPSMTPMessage()
        message.relayHost = "smtp.gmail.com"
        message.login = "login@gmail.com"
        message.pass = "password"
        message.requiresAuth = true
        message.wantsSecure = true
        message.relayPorts = [587]
        message.fromEmail = "login@gmail.com"
        message.toEmail = "to@gmail.com"
        message.subject = subject
        let messagePart = [kSKPSMTPPartContentTypeKey: "text/plain; charset=UTF-8", kSKPSMTPPartMessageKey: body]
        message.parts = [messagePart]
        message.delegate = self
        message.send()
    }

    func messageSent(_ message: SKPSMTPMessage!) {
        print("Successfully sent email!")
    }

    func messageFailed(_ message: SKPSMTPMessage!, error: Error!) {
        print("Sending email failed!")
    }
}


来源:https://stackoverflow.com/questions/28250101/is-there-a-library-skpsmtpmessage-for-swift

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