Pages

Subscribe:

Monday, March 14, 2011

iPhone SDK - How to send mail from iPhone ?

Following example illustrates the method to access the mail interface of iPhone.

Import the message api.


#import <MessageUI/MessageUI.h>

Now, create a button and add the action for the button. Call the following sendMail method 

- (void) sendMail {
   
            NSMutableString *mailMessage = [[NSMutableString alloc] init];
            [mailMessage appendString:@"Hello\n\n"];
            [mailMessage appendString:@"I am willing to participate in the career\n"];
            [mailMessage appendString:@"Enrichment Program. I like to know more details\n\n"];
            [mailMessage appendString:@"My contact details. \n\n"];
            [mailMessage appendString:@"Name: Raja T S Sekhar\n"];
            [mailMessage appendString:@"Email:rajatssekhar@gmail.com\n"];
            [mailMessage appendString:@"Mobile No: [Add your No here]\n\n"];
            [mailMessage appendString:@"With Regards. \n"];
            [mailMessage appendString:@"EMail Team"];
  
            NSString *mailSubject = [[NSString alloc] initWithString:@"New Mail"];
           
            MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
            controller.mailComposeDelegate = self;
            NSArray *toRecipients = [[NSArray alloc] initWithObjects:@"sekhar@enrichware.com",nil];
            [controller setToRecipients:toRecipients];
            [controller setSubject:mailSubject];
            [controller setMessageBody:mailMessage isHTML:NO];
            if (controller) [self presentModalViewController:controller animated:YES];

}

Following delegate will be called when user taps cancel and send button.

- (void)mailComposeController:(MFMailComposeViewController*)controller 
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError*)error {
            if (result == MFMailComposeResultSent) {
                        NSString *success = [[NSString alloc] initWithString:@"Success"];
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Email Sample"
                                                                                                                                                                        message:success
                                                                                                                                                               delegate:self
                                                                                                                                      cancelButtonTitle:nil
                                                                                                                                      otherButtonTitles:@"OK",nil];
                        [alert show];
                        [alert release];
            }
            [self dismissModalViewControllerAnimated:YES];
}

Now, run the code. Click the 'Send Mail' button. It will open the email program of iphone and from there you can send the email. The subject, recipient and body will be displayed as pre-configured in the program.

The program will display success message if the user clicks the send button.

0 comments:

Post a Comment