swiftemailswiftuimail-senderskpsmtpmessage

skpsmtpmessage sending email function is not working correctly?


So I downloaded the skpsmtpmessage pod and installed it to my project. I have a file called MailSender in the project that looks like:

import Foundation
import skpsmtpmessage

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

    func sendEmail() {
        let message = SKPSMTPMessage()
        message.relayHost = "smtp.gmail.com"
        message.login = "myemail@gmail.com"
        message.pass = "password"
        message.requiresAuth = true
        message.wantsSecure = true
        message.relayPorts = [587]
        message.fromEmail = "myemail@gmail.com"
        message.toEmail = "recipientemail@gmail.com"
        message.subject = "subject"
        let messagePart = [kSKPSMTPPartContentTypeKey: "text/plain; charset=UTF-8", kSKPSMTPPartMessageKey: "body of email"]
        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!")
    }
}

In ContentView, I have a button that looks like:

Button("click me") {
    MailSender.shared.sendEmail()
}

When I run the simulator and click the button in ContentView, I get a bunch of "*** stopping watchdog ***" messages in the output and then some additional messages that say "S: 250-smtp.gmail.com at your service, [My IP Address]", and then there's a message that says "S: 535-5.7.8 Username and Password not accepted. Learn more at", but I know that the from email and password that I'm providing are correct (it's my personal email information in my version of the project). Any help would be greatly appreciated.


Solution

  • By default support for external clients is off on Gmail, so login in your account and make sure you turned on some, IMAP is preferred.

    demo1

    Then make sure you configure properly email client

    demo2