问题
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