Swift有一个库“SKPSMTPMessage”吗?

时间:2015-01-31 09:27:20

标签: ios swift skpsmtpmessage

是否有在Swift中使用的SMPSKMPMessage库? earlier i have used this tutorial which is in objective - c。我想要做的是使用Swift在后台发送邮件。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

通过添加:

,简单地将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!")
    }
}