I want to send an email from my iPhone application. I have heard that the iOS SDK doesn\'t have an email API. I don\'t want to use the following code because it will exit my
On iOS 3.0 and later you should use the MFMailComposeViewController
class, and the MFMailComposeViewControllerDelegate
protocol, that is tucked away in the MessageUI framework.
First add the framework and import:
#import <MessageUI/MFMailComposeViewController.h>
Then, to send a message:
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:@"My Subject"];
[controller setMessageBody:@"Hello there." isHTML:NO];
if (controller) [self presentModalViewController:controller animated:YES];
[controller release];
Then the user does the work and you get the delegate callback in time:
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error;
{
if (result == MFMailComposeResultSent) {
NSLog(@"It's away!");
}
[self dismissModalViewControllerAnimated:YES];
}
Remember to check if the device is configured for sending email:
if ([MFMailComposeViewController canSendMail]) {
// Show the composer
} else {
// Handle the error
}
Swift 2.2. Adapted from Esq's answer
import Foundation
import MessageUI
class MailSender: NSObject, MFMailComposeViewControllerDelegate {
let parentVC: UIViewController
init(parentVC: UIViewController) {
self.parentVC = parentVC
super.init()
}
func send(title: String, messageBody: String, toRecipients: [String]) {
if MFMailComposeViewController.canSendMail() {
let mc: MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(title)
mc.setMessageBody(messageBody, isHTML: false)
mc.setToRecipients(toRecipients)
parentVC.presentViewController(mc, animated: true, completion: nil)
} else {
print("No email account found.")
}
}
func mailComposeController(controller: MFMailComposeViewController,
didFinishWithResult result: MFMailComposeResult, error: NSError?) {
switch result.rawValue {
case MFMailComposeResultCancelled.rawValue: print("Mail Cancelled")
case MFMailComposeResultSaved.rawValue: print("Mail Saved")
case MFMailComposeResultSent.rawValue: print("Mail Sent")
case MFMailComposeResultFailed.rawValue: print("Mail Failed")
default: break
}
parentVC.dismissViewControllerAnimated(false, completion: nil)
}
}
Client code :
var ms: MailSender?
@IBAction func onSendPressed(sender: AnyObject) {
ms = MailSender(parentVC: self)
let title = "Title"
let messageBody = "https://stackoverflow.com/questions/310946/how-can-i-send-mail-from-an-iphone-application this question."
let toRecipents = ["foo@bar.com"]
ms?.send(title, messageBody: messageBody, toRecipents: toRecipents)
}
To send an email from iPhone application you need to do below list of task.
Step 1: Import #import <MessageUI/MessageUI.h>
In your controller class where you want to send an email.
Step 2: Add the delegate to your controller like shown below
@interface <yourControllerName> : UIViewController <MFMessageComposeViewControllerDelegate, MFMailComposeViewControllerDelegate>
Step 3: Add below method for send email.
- (void) sendEmail {
// Check if your app support the email.
if ([MFMailComposeViewController canSendMail]) {
// Create an object of mail composer.
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
// Add delegate to your self.
mailComposer.mailComposeDelegate = self;
// Add recipients to mail if you do not want to add default recipient then remove below line.
[mailComposer setToRecipients:@[<add here your recipient objects>]];
// Write email subject.
[mailComposer setSubject:@“<Your Subject Here>”];
// Set your email body and if body contains HTML then Pass “YES” in isHTML.
[mailComposer setMessageBody:@“<Your Message Body>” isHTML:NO];
// Show your mail composer.
[self presentViewController:mailComposer animated:YES completion:NULL];
}
else {
// Here you can show toast to user about not support to sending email.
}
}
Step 4: Implement MFMailComposeViewController Delegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(nullable NSError *)error {
[controller dismissViewControllerAnimated:TRUE completion:nil];
switch (result) {
case MFMailComposeResultSaved: {
// Add code on save mail to draft.
break;
}
case MFMailComposeResultSent: {
// Add code on sent a mail.
break;
}
case MFMailComposeResultCancelled: {
// Add code on cancel a mail.
break;
}
case MFMailComposeResultFailed: {
// Add code on failed to send a mail.
break;
}
default:
break;
}
}
MFMailComposeViewController is the way to go after the release of iPhone OS 3.0 software. You can look at the sample code or the tutorial I wrote.
A few things I'd like to add here:
Using the mailto URL won't work in the simulator as mail.app isn't installed on the simulator. It does work on device though.
There is a limit to the length of the mailto URL. If the URL is larger than 4096 characters, mail.app won't launch.
There is a new class in OS 3.0 that lets you send an e-mail without leaving your app. See the class MFMailComposeViewController.
Swift 2.0
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?){
if let error = error{
print("Error: \(error)")
}else{
//NO Error
//------------------------------------------------
var feedbackMsg = ""
switch result.rawValue {
case MFMailComposeResultCancelled.rawValue:
feedbackMsg = "Mail Cancelled"
case MFMailComposeResultSaved.rawValue:
feedbackMsg = "Mail Saved"
case MFMailComposeResultSent.rawValue:
feedbackMsg = "Mail Sent"
case MFMailComposeResultFailed.rawValue:
feedbackMsg = "Mail Failed"
default:
feedbackMsg = ""
}
print("Mail: \(feedbackMsg)")
//------------------------------------------------
}
}