Sending an email from swift 3

For sending email from your application you 

first Add UIMessage Package from for Application then Write this Code and connect them at your Storyboard :


import UIKit
import MessageUI

class SendDropMailVC: UIViewController , MFMailComposeViewControllerDelegate {
    
    @IBOutlet weak var subject: UITextField!
    @IBOutlet weak var Body: UITextView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

    } 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    @IBAction func SendEmail(_ sender: AnyObject) {
        
        var SubjectText = "Your Subject:"
        SubjectText += subject.text!
        
        let messageBody = Body
        
        let toRecients = ["123@email.com"]
        
        let mc: MFMailComposeViewController = MFMailComposeViewController()
        mc.mailComposeDelegate = self
        mc.setSubject(SubjectText)
        mc.setMessageBody((messageBody?.text)!, isHTML: false)
        mc.setToRecipients(toRecients)
        
        self.present(mc, animated: true, completion: nil)
        
    }
    
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        switch result.rawValue{
            
        case MFMailComposeResult.cancelled.rawValue:
            NSLog("Mail Cancelled")
        case MFMailComposeResult.saved.rawValue:
            NSLog("Mail Saved")
        case MFMailComposeResult.sent.rawValue:
            NSLog("Mail Sent")
            
        case MFMailComposeResult.failed.rawValue:
            NSLog("Mail Sent Failure: %@" , [error!.localizedDescription])
            
        default:
            break
        }
        
        self.dismiss(animated: true, completion: nil)
    
}


Add a UITextField! & UITextView! to your view with a button as you see in the picture



if its not clear send me your message



Comments

  1. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…!!..Swift Online Training

    ReplyDelete

Post a Comment