Failed on using Swift to implement in-app email

前端 未结 3 2131
走了就别回头了
走了就别回头了 2021-02-14 08:52

I want to use swift to implement in-app email. When I click the button, the email window pops up. However, I am unable to send my email. Moreover, after I click cancel-delete dr

3条回答
  •  故里飘歌
    2021-02-14 09:27

    In case anyone is looking for a non MFMailCompose option, here's what I did to send using Gmail's SMTP servers.

    1. Download a zip of this repo: https://github.com/jetseven/skpsmtpmessage
    2. Drag and drop the files under SMTPLibrary into your XCode project
    3. Create a new header file - MyApp-Briding-Header.h
    4. Replace new header file with this:
    #import "Base64Transcoder.h"
    #import "HSK_CFUtilities.h"
    #import "NSData+Base64Additions.h"
    #import "NSStream+SKPSMTPExtensions.h"
    #import "SKPSMTPMessage.h"
    
    1. Go to Project(Targets > MyApp on the left)/Build Settings/Swift Compiler - Code Generation
    2. Add path to header file under Objective-C Briding Header -> Debug (i.e. MyApp/MyApp-Bridging-Header.h
    3. Go to Project/Build Phases/Compile Sources
    4. Select all .m files and click enter. Type -fno-objc-arc and hit enter.

    5. Use this code to send email:

    var mail = SKPSMTPMessage()       
    mail.fromEmail = "fromemail@gmail.com"
    mail.toEmail = "tomail@gmail.com"
    mail.requiresAuth = true
    mail.login = "fromemail@gmail.com"
    mail.pass = "password"
    mail.subject = "test subject"
    mail.wantsSecure = true
    mail.relayHost = "smtp.gmail.com"
    
    mail.relayPorts = [587]
    
    var parts: NSDictionary = [
                "kSKPSMTPPartContentTypeKey": "text/plain; charset=UTF-8",
                "kSKPSMTPPartMessageKey": "test message",
    ]
    
    mail.parts = [parts]
    
    mail.send()
    

    Hope it helps someone. I didn't want to use the MFMailCompose option because I didn't want to have to prompt the user.

提交回复
热议问题