Detailed explanation of two examples of IOS's own Email
The IOS system framework provides two methods for sending emails: openURL and MFMailComposeViewController. With these two methods, we can easily add functions such as user feedback to the application that require sending emails.
Using openURL to call the system mailbox client is our main method to implement the mail sending function below IOS3.0. We can specify the content of the email by setting the relevant parameters in the url, but its disadvantages are obvious, such a process will cause the program to temporarily exit. Here is a small example of using openURL to send emails:
#pragma mark - Send emails using the system mail client
-(void)launchMailApp { NSMutableString *mailUrl = [[[NSMutableString alloc]init]autorelease]; //Add recipient NSArray *toRecipients = [NSArray arrayWithObject: @"first@"]; [mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]]; //Add cc NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@", @"third@", nil]; [mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]]; //Add a secret send NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@", nil]; [mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]]; //Add topic [mailUrl appendString:@"&subject=my email"]; //Add email content [mailUrl appendString:@"&body=<b>email</b> body!"]; NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]]; }
MFMailComposeViewController is an interface added in IOS 3.0, which is in. By calling
MFMailComposeViewController can integrate the email sending window into our application, so you don’t need to exit the program when sending emails.
How to use MFMailComposeViewController:
1. Introduced in the project;
2. Import header files into the file used;
3. Implement MFMailComposeViewControllerDelegate to handle email sending events;
4. Before calling up the email sending window, use the "+ (BOOL)canSendMail" method in the MFMailComposeViewController to check whether the user has set up an email account;
5. Initialize the MFMailComposeViewController and construct the email body
// // // MailDemo // // Created by LUOYL on 12-4-4. // Copyright (c) 2012. All rights reserved.// #import <UIKit/> #import <MessageUI/> @interface ViewController : UIViewController<MFMailComposeViewControllerDelegate> @end
#pragma mark - Send emails in the app//Activate the email function- (void)sendMailInApp { Class mailClass = (NSClassFromString(@"MFMailComposeViewController")); if (!mailClass) { [self alertWithMessage:@"The current system version does not support the in-app sending mail function, you can use the mailto method instead"]; return; } if (![mailClass canSendMail]) { [self alertWithMessage:@"The user has not set up an email account"]; return; } [self displayMailPicker]; } //Click out the email sending window- (void)displayMailPicker { MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init]; = self; //Set the theme [mailPicker setSubject: @"eMail Theme"]; //Add recipient NSArray *toRecipients = [NSArray arrayWithObject: @"first@"]; [mailPicker setToRecipients: toRecipients]; //Add cc NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@", @"third@", nil]; [mailPicker setCcRecipients:ccRecipients]; //Add a secret send NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@", nil]; [mailPicker setBccRecipients:bccRecipients]; // Add a picture UIImage *addPic = [UIImage imageNamed: @"Icon@"]; NSData *imageData = UIImagePNGRepresentation(addPic); // png //About mimeType: /assignments/media-types/ [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @""]; //Add a pdf attachment NSString *file = [self fullBundlePathFromRelativePath:@"High Quality C++ Programming Guide.pdf"]; NSData *pdf = [NSData dataWithContentsOfFile:file]; [mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"High Quality C++ Programming Guide.pdf"]; NSString *emailBody = @"<font color='red'>eMail</font> text"; [mailPicker setMessageBody:emailBody isHTML:YES]; [self presentModalViewController: mailPicker animated:YES]; [mailPicker release]; } #pragma mark - Implement MFMailComposeViewControllerDelegate- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { //Close the email sending window [self dismissModalViewControllerAnimated:YES]; NSString *msg; switch (result) { case MFMailComposeResultCancelled: msg = @"User Cancel Editing Email"; break; case MFMailComposeResultSaved: msg = @"User saves email successfully"; break; case MFMailComposeResultSent: msg = @"The user clicks to send and puts the email in the queue, but has not been sent yet"; break; case MFMailComposeResultFailed: msg = @"User attempting to save or send an email failed"; break; default: msg = @""; break; } [self alertWithMessage:msg]; }