In this application we will see how to send mail from the iPhone. User just simple click in the ToRecipient , and it will take address from the addressbook. So let see how it will be worked.
Step 1: Create a View base application using template. Give the application name “MailSend”.
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 MailSendViewController class for you. Expand Resources and notice the template generated a separate nib, MailSendViewController.xib, for the “MailSend”.
Step 4: We need 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: In the MailSendViewController.h file,we have to import MessageUI.h file and added UIButton for create a button and a method. S make the following changes in the file.
#import <MessageUI/MessageUI.h>
@interface MailSendViewController : UIViewController
<MFMailComposeViewControllerDelegate> {
IBOutlet UIButton *myButton;
}
- (IBAction)buttonPressed;
Step 6: Double click the MailSendViewController.xib file and open it to the Interface Builder. Open the view icon and drag the Round Rect from the library and place it to the view window. Double click the button and give the name “PressButton”. Now connect File’s Owner icon to the View icon and select view. Drag File’s Owner icon to the PressButton and select myButton. Select “PressButton” and bring up connection inspector and drag to the TouchUpInside to the File’s Owner icon and select buttonPressed: action. Now save it, close it and go back to the Xcode.
Step 7: Open the MailSendViewController.m file and make the following changes:
if ([MFMailComposeViewController canSendMail])
myButton.enabled = YES;
}
- (IBAction)buttonPressed {
MFMailComposeViewController *mailController= [[MFMailComposeViewController alloc] init];
mailController.mailComposeDelegate = self;
[mailController setSubject:@"Hello iPhone"];
[mailController setMessageBody:@"This is the MailSend Application…." isHTML:NO];
[self presentModalViewController:mailController animated:YES];
[mailController release];
}
- (void)mailComposeController:(MFMailComposeViewController*)mailCon troller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self becomeFirstResponder];
[self dismissModalViewControllerAnimated:YES];
}
- (void)dealloc {
[myButton release];
[super dealloc];
}
Step 8: Now compile and run the application in the Simulator.
You can downloaded SourceCode from here MailSend 2












Thank you ! your article is very helpful for me