2

Is there any SKPSMTPMessage 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.

4

2 回答 2

2

只需通过添加以下内容将 skpsmtpmessage 与可可豆荚集成:

pod 'skpsmtpmessage'

到您的 Podfile 然后这是您从代码发送电子邮件的方式:

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!")
    }
}
于 2018-02-01T14:04:45.307 回答
1

我不知道 Swift 中有任何电子邮件发送库,但你绝对可以在你的 Swift 应用程序中使用 Objective-C 教程中描述的那个。所有 Objective-C 库都与 Swift 代码交叉兼容:只需使用bridging header.

于 2015-01-31T09:34:48.803 回答