在.h
文件中:
#import <MessageUI/MessageUI.h>
并使用MFMailComposeViewControllerDelegate
委托
在.m
文件中:
// Method :
-(void)sendMailto:(NSString*)to WithSubject:(NSString*)subject withBody:(NSString*)body
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:subject];
NSArray *toRecipients = [NSArray arrayWithObjects:to, nil];
[mailer setToRecipients:toRecipients];
NSString *emailBody = body;
[mailer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailer animated:YES];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Fail !" message:@"Please configure your Mail application of device with your email id" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled");
activityView.hidden = YES;
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
activityView.hidden = YES;
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
activityView.hidden = YES;
UIStoryboard * myStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
CustomerAccountViewController *custAcnt = [myStoryboard instantiateViewControllerWithIdentifier:@"customerAcnt"];
custAcnt.btnID = 1;
[self.navigationController pushViewController:custAcnt animated:YES];
break;
case MFMailComposeResultFailed:
NSLog(@"Mail failed");
activityView.hidden = YES;
break;
default:
NSLog(@"Mail not sent");
activityView.hidden = YES;
break;
}
[self dismissModalViewControllerAnimated:YES];
}
// Call On Button Click
- (IBAction)btnProceedClicked:(id)sender
{
[self sendMailto:@"abc@gmail.com" WithSubject:@"Email Subject" withBody:@"Your Mail Body"];
}