2

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.

nickcoding
  • 305
  • 8
  • 35

1 Answers1

2

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

Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Hi Asperi, I tried changing the message.relayHost to "imap.gmail.com" and the relayPort to 993, and then I tried setting the relayHost to "smtp.gmail.com" and the relayPort to 465 but neither works. I don't entirely understand how to set this up correctly--could you by any chance also submit an answer with the corrected relayPort and relayHost? – nickcoding Jul 21 '20 at 03:33