Top of the page
Close

This is a red error

This is a red error

This is a red error

Sending email message from the app using MFMailComposeViewController in MessageUI on iPhone

Ondrej Rafaj on 2010.04.16 13:53:08

Just a short version of our tutorial "How to send mail from an iPhone app"

// Header file

// importing the MessageUI framework
#import <MessageUI/MessageUI.h>

// adding the delegate functionality to the class (<MFMailComposeViewControllerDelegate>)
@interface TutorialProjectViewController : UIViewController <MFMailComposeViewControllerDelegate> {

}

- (IBAction)pressTheMailButtonDudeFunction:(id)sender;



// Implementation file

- (IBAction)pressTheMailButtonDudeFunction:(id)sender {
	
	// allocatind new message composer window
	MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
	
	// setting a delegate method to "self"
	mc.mailComposeDelegate = self;
	
	// pre-populating the message subject
	[mc setSubject:@"Send me a message"];
	
	// adding content of the message as a plain text
	[mc setMessageBody:@"Send me a message is you like this tutorial :)" isHTML:NO];
	
	// adding content of the message as an HTML
	[mc setMessageBody:@"<p>Send me a message is you like this tutorial :)<p>" isHTML:YES];
	
	// adding recipients
	[mc setToRecipients:[NSArray arrayWithObjects:@"Fuerte <info@fuerte.cz>", @"info@xprogress.com", nil]];
	
	// adding recipients for a send copy to (arrayWithObject or arrayWithObjects)
	[mc setCcRecipients:[NSArray arrayWithObject:@"test@example.com"]];
	
	// adding hidden recipients
	[mc setBccRecipients:[NSArray arrayWithObject:@"test@example.com"]];
	
	// adding image attachment
	// getting path for the image we have in the tutorial project
	NSString *path = [[NSBundle mainBundle] pathForResource:@"Extra_Xcode_100x100" ofType:@"png"];
	
	// loading content of the image into NSData
	NSData *imageData = [NSData dataWithContentsOfFile:path];
	
	// adding the attachment to he message
	[mc addAttachmentData:imageData mimeType:@"image/png" fileName:@"My tutorial image"];
	
	// setting different than the default transition for the modal view controller
	[mc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
	
	/*
	 Modal view controllers transitions:
	 
	 UIModalTransitionStyleCoverVertical => pops up from the bottom, default transition
	 UIModalTransitionStyleCrossDissolve => fade on the screen
	 UIModalTransitionStyleFlipHorizontal => page flip
	 */
	
	// displaying our modal view controller on the screen (of course animated has to be set on YES if you want to see any transition)
	[self presentModalViewController:mc animated:YES];
	
	// releasing the controller
	[mc release];
}

// delegate function callback
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
	// switchng the result
	switch (result) {
		case MFMailComposeResultCancelled:
			NSLog(@"Mail send canceled.");
			/*
			 Execute your code for canceled event here ...
			 */
			break;
		case MFMailComposeResultSaved:
			NSLog(@"Mail saved.");
			/*
			 Execute your code for email saved event here ...
			 */
			break;
		case MFMailComposeResultSent:
			NSLog(@"Mail sent.");
			/*
			 Execute your code for email sent event here ...
			 */
			break;
		case MFMailComposeResultFailed:
			NSLog(@"Mail send error: %@.", [error localizedDescription]);
			/*
			 Execute your code for email send failed event here ...
			 */
			break;
		default:
			break;
	}
	// hide the modal view controller
	[self dismissModalViewControllerAnimated:YES];
}

Don't forget to import the MessageUI framework into your project :)

Ondrej Rafaj

Ondrej Rafaj

Independent iPhone developer @ ondrej-rafaj.co.uk

I am available to give you a free quote or start working on your project ... just give me a call or drop a line. Please find all my details on my portfolio site ondrej-rafaj.co.uk

Back to top Comment

Comments ... Why not to get involved!

We had to disable comments till the problem with spam will be solved, sorry for any inconvenience caused.

Back to top

IE SUCKS, DON'T USE IT :) Digg this page