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.
Asked
Active
Viewed 1,998 times
2 Answers
3
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!")
}
}

Leszek Szary
- 9,763
- 4
- 55
- 62
-
Its crashing the application, please let me know if you have also faced crash issue? – Mohsin Qureshi Sep 19 '19 at 10:24
-
No, I did not had any crashes last time I used this (in Feb 2018). – Leszek Szary Sep 20 '19 at 08:21
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
.

Nicolas B.
- 1,318
- 1
- 11
- 20