Compose mail application in iPhone?

The iPhone by Apple allows users to send email from wherever they are at least from any location that receives a wireless Internet signal. You no longer need to be at your computer in order to communicate with your friends and family via email. Using this application we can easily send email from the iPhone.

Step 1: Create a View base application using the template . Give the project name .

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Open your ViewController.h file and add UILabel and three methods.Make the label IBOutlet and method and IBAction. So make the changes in the header file.

#import <uikit/UIKit.h>
#import <messageUI/MessageUI.h>
#import <messageUI/MFMailComposeViewController.h>

@interface MailComposerViewController : UIViewController <mfmailComposeViewControllerDelegate>
{
	IBOutlet UILabel *message;
}

@property (nonatomic, retain) IBOutlet UILabel *message;

-(IBAction)showPicker:(id)sender;
-(void)displayComposerSheet;
-(void)launchMailAppOnDevice;


Step 4: We have to add MessageUI Framework. After hitting the Add -> Existing Framework .. menu item an open panel will popup. Navigate to this directory.Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk/System/Library/Frameworks

Step 5: Open the .xib file in the Interface Builder. We are going to create a Round Rect button from the library and place it to the view window. Double click the Round Rect button and give the name Compose Mail. Single click it and bring up connection inspector, now connect Touch UP inside to the File’s Owner icon and select the showPicker: action. Now drag the label from library and place it below of the Round Rect. Connect  File’s Owner icon to the label and select message . Now save your .xib file and go back to the Xcode.

Step 6: Now open your .m file and make the following changes.


-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker setSubject:@"Hello iPhone!"];

// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"];
NSString *emailBody = @"Nice  to See you!";
[picker setMessageBody:emailBody isHTML:NO];

[self presentModalViewController:picker animated:YES];
[picker release];
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
message.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
message.text = @"Result: canceled";
break;
case MFMailComposeResultSaved:
message.text = @"Result: saved";
break;
case MFMailComposeResultSent:
message.text = @"Result: sent";
break;
case MFMailComposeResultFailed:
message.text = @"Result: failed";
break;
default:
message.text = @"Result: not sent";
break;
}
[self dismissModalViewControllerAnimated:YES];
}

Step 7 : Compile and run your application on the simulator.

  • Rick
  • February 24, 2010
  • iOS

About the Author

Leave a Reply 7 comments

Rahul K - April 6, 2010 Reply

I was going through Send email tutorial using MFMailComposerViewController you posted on your site and with help of it I was able to implement mail composer in my app. I had one question to it.
Is it possible I can auto populate To Recipient email address when I select person from the address book. Presently I am displaying all of my contacts in table view cell pulling them from address book and I have placed a button on each cell. When user presses the button, I want mail composer to open with the receivers email address populated in it. I am able to open the mail composer with touch of button but don’t know how to populate the email address.
Thanks alot!

Sushant - April 6, 2010 Reply

See the below link. I thought this link will help you.

Link: http://www.edumobile.org/iphone/iphone-programming-tutorials/mailsend-in-the-iphone/

tom - July 27, 2010 Reply

Thanks for sharing your work.

tom - July 27, 2010 Reply

hi Sushant, thanks for sharing your work.

Rosendo - August 18, 2010 Reply

How can I read a email in my own application

which library (or framework) do I need to make it

ducnguyen - September 24, 2011 Reply

I saw your code, missing set to recipients.
[picker setToRecipients: toRecipients];
Thank alot !

Sean - March 24, 2012 Reply

You might want to add

[picker setToRecipients:toRecipients];

as it is it’s just unreferenced.

Leave a Reply: