Mail Send from iPad

This is the very simple application. In this application we will see how to mail send from the iPad.

Step 1: Create a View base application using template. Give the application name “MailComposer”.

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: Expand classes and notice Interface Builder created the MailComposerViewController class for you. Expand Resources and notice the template generated a separate nib, MailComposerViewController.xib, for the “MailComposer”.

Step 4: We need to add MessageUI Framework. Select Frameworks folder ->Add -> Existing Framework -> Add MessageUI Framework.

Step 5: In the MailComposerViewController.h file , we have import MessageUI framework. Create an instance of UIButton class and add one buttonPressed method. So make the following changes in the file.

 

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

@interface MailComposerViewController : UIViewController

{
IBOutlet UIButton *button;
}

- (IBAction)buttonPressed;

 

 

Step 6: Double click MailComposerViewController.xib file and open it to the Interface Builder. First drag the Round Rect button from the library and place it to the view window. Connect File’s Owner icon to the View icon and select view. Drag File’s Owner icon to the round Rect button and select button and select the Round Rect button and bring up Connection Inspector next drag Touch Up Inside to the File’s Owner icon and select buttonPressed: action. Now save the MailComposerViewController.xib file, close it and go back to the Xcode.

Step 7: Open the MailComposerViewController.m file and make the following changes in the file.

 

- (void)viewDidLoad {
if ([MFMailComposeViewController canSendMail])
button.enabled = YES;
}

- (IBAction)buttonPressed {
MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
mailController.mailComposeDelegate = self;
[mailController setSubject:@"Hello World"];
[mailController setMessageBody:@"This is the MailSend Application...." isHTML:NO];
[self presentModalViewController:mailController animated:YES];
[mailController release];
}

- (void)mailComposeController:(MFMailComposeViewController*)mailController didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self becomeFirstResponder];
[self dismissModalViewControllerAnimated:YES];
}

 

 

Step 8: Now Compile and run the application in the Simulator.

You can Download SourceCode from here MailComposer

Leave a Comment:

5 comments
Dominik says August 26, 2010

I imported this project, but I am getting an error running the application on the iPad device. It works perfectly fine on the iPad simulator.

The line of code it is crashing:

[self presentModalViewController:mailController animated:YES];

with this error:

Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘Application tried to present a nil modal view controller on target

Dose anyone know what could cause this error?

Reply
Kacper86 says September 22, 2010

@Dominik:
I found out, that unless you have mail configured you got this crash. My solution:

MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setToRecipients:recipients];
[controller setSubject:@””];
[controller setMessageBody:body isHTML:NO];
if (controller != nil) {
[self presentModalViewController:controller animated:YES];
[controller release];
}

This “if” helped 🙂 I hope there won’t be a memory leak.

Reply
    James York says April 5, 2011

    Sorry to necro this post, but I have the same problem as Dominik above, and the if {} didn’t help… do you have any idea what could be calling this problem?

    thanks in advance,

    James

    Reply
victorvj says November 2, 2010

It should send and email right? I don’t get any email…
is it working well in your cases?

Thanks!

Reply
Charlie Sheen says February 3, 2011

I very glad to find this site on bing, just what I was searching for : D also bookmarked .

Reply
Add Your Reply