Sometimes we need to send the email from iOS application for that we have to integrate MFMailComposeViewController in our application so that we can send email.
This program will help you to send email through your app using swift.
Steps Required:
Step1: Need to Add the MessageUI.framework and import it like: import MessageUI
import UIKit
import MessageUI
class ViewController: UIViewController,MFMailComposeViewControllerDelegate {
//outlet for Button
@IBOutlet weak var sendMailButton: UIButton!
//actionforButtonSendMail
@IBAction func sendMailButtonClicked(sender:UIButton){
let mailviewController=MFMailComposeViewController()
var messageBody="My First mail uisng Swift"
var toRecipient=["anup227kumar@gmail.com","anoop@gmail.com"]
var messageSubject="Testing my mail in swift"
mailviewController.setToRecipients(toRecipient)
mailviewController.setSubject(messageSubject)
mailviewController.setMessageBody(messageBody, isHTML:false)
if MFMailComposeViewController.canSendMail()
{
self.presentViewController(mailviewController, animated: true, completion: nil)
}
else
{
(UIAlertView(title: "Mail Error", message: "Can not send mail", delegate:self, cancelButtonTitle:"Ok")).show()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//Delegate Method for MailComposeController
func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!){
switch result.value
{
case MFMailComposeResultCancelled.value:
println("Mail sending Cancelled")
break
case MFMailComposeResultFailed.value:
println("Mail Sending Failed")
break
case MFMailComposeResultSaved.value:
println("Message saved")
break
case MFMailComposeResultSent.value:
println("Message sent")
default:
println("Default action")
break
}
controller.dismissViewControllerAnimated(true, completion: nil)
}
0 Comment(s)