two ways to send email to iOSCategory: iOS send mail 2012-08-28 12:32 9575 people read reviews (1) favorite reports Emailios Encryption Programming File framework
Directory (?) [+]
The iOS system framework provides two ways to send email: OpenURL and Mfmailcomposeviewcontroller. With these two methods, we can easily add to the application such as user feedback, such as the need to send mail functions.
1.openURL using OpenURL to invoke the system mailbox client is our primary means of implementing the E-mail functionality below IOS3.0. We can specify the contents of the message by setting the relevant parameters in the URL, but it is obvious that the process will cause the program to quit temporarily. Here is a small example of using OpenURL to send a message:
- #pragma mark-Send mail using System mail client
- -(void) Launchmailapp
- {
- nsmutablestring *mailurl = [[[Nsmutablestring alloc]init]autorelease];
- //Add Recipients
- Nsarray *torecipients = [Nsarray arraywithobject: @"[email protected]"];
- [Mailurl appendformat:@"mailto:%@", [torecipients componentsjoinedbystring:@","]];
- //Add cc
- Nsarray *ccrecipients = [Nsarray arraywithobjects:@"[email protected]", @"[email protected]", nil];
- [Mailurl appendformat:@"? cc=%@", [ccrecipients componentsjoinedbystring:@","]];
- //Add encryption to send
- Nsarray *bccrecipients = [Nsarray arraywithobjects:@"[email protected]", nil];
- [Mailurl appendformat:@"&bcc=%@", [bccrecipients componentsjoinedbystring:@","]];
- //Add Theme
- [Mailurl appendstring:@"&subject=my Email"];
- //Add message content
- [Mailurl appendstring:@"&body=<b>email</b> body!"];
- nsstring* email = [Mailurl stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
- [[UIApplication Sharedapplication] OpenURL: [Nsurl Urlwithstring:email]];
- }
2.MFMailComposeViewControllerMFMailComposeViewController is a new interface in IOS3.0, which is in Messageui.framework. By calling Mfmailcomposeviewcontroller, you can integrate the mail-sending window into our app and send mail without exiting the program. How to use Mfmailcomposeviewcontroller:
- 1. Introduction of messageui.framework in the project;
- 2. Import the MFMailComposeViewController.h header file into the file used;
- 3. Implement Mfmailcomposeviewcontrollerdelegate, handle mail sending events;
- 4. Use the "+ (BOOL) Cansendmail" method in Mfmailcomposeviewcontroller to check if the user has set up a mail account before calling out the mail sending window;
- 5. Initialize the Mfmailcomposeviewcontroller, construct the mail body
- //
- ViewController.h
- Maildemo
- //
- Created by luoyl on 12-4-4.
- Copyright (c) 2012 Http://luoyl.info. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- #import <MessageUI/MFMailComposeViewController.h>
- @interface viewcontroller:uiviewcontroller<mfmailcomposeviewcontrollerdelegate>
- @end
- #pragma mark-Send messages within the app
- Activate Mail feature
- -(void) Sendmailinapp
- {
- Class Mailclass = (nsclassfromstring (@"Mfmailcomposeviewcontroller"));
- if (!mailclass) {
- [Self alertwithmessage:@] The current system version does not support in-app send mail functionality, you can use the Mailto method instead of "];
- return;
- }
- if (![ Mailclass Cansendmail]) {
- [Self alertwithmessage:@"user does not have a mail account set up"];
- return;
- }
- [Self displaymailpicker];
- }
- Bring up the mail send window
- -(void) Displaymailpicker
- {
- Mfmailcomposeviewcontroller *mailpicker = [[Mfmailcomposeviewcontroller alloc] init];
- Mailpicker.mailcomposedelegate = self;
- //Set Theme
- [Mailpicker setsubject: @"email theme"];
- //Add Recipients
- Nsarray *torecipients = [Nsarray arraywithobject: @"[email protected]"];
- [Mailpicker settorecipients:torecipients];
- //Add cc
- Nsarray *ccrecipients = [Nsarray arraywithobjects:@"[email protected]", @"[email protected]", nil];
- [Mailpicker setccrecipients:ccrecipients];
- //Add encryption to send
- Nsarray *bccrecipients = [Nsarray arraywithobjects:@"[email protected]", nil];
- [Mailpicker setbccrecipients:bccrecipients];
- //Add a picture
- UIImage *addpic = [UIImage imagenamed: @"[email protected]"];
- NSData *imagedata = uiimagepngrepresentation (addpic); //PNG
- //About mimetype:http://www.iana.org/assignments/media-types/index.html
- [Mailpicker addattachmentdata:imagedata MimeType: @ "" FileName: @"Icon.png"];
- //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-Achieve mfmailcomposeviewcontrollerdelegate
- -(void) Mailcomposecontroller: (Mfmailcomposeviewcontroller *) controller didfinishwithresult: ( Mfmailcomposeresult) Result Error: (NSERROR *) Error
- {
- //close mail sending window
- [Self dismissmodalviewcontrolleranimated:yes];
- NSString *msg;
- switch (result) {
- Case mfmailcomposeresultcancelled:
- msg = @"User cancels edit mail";
- Break ;
- Case mfmailcomposeresultsaved:
- msg = @"User successfully saved mail";
- Break ;
- Case Mfmailcomposeresultsent:
- msg = @"The user clicks Send, puts the message in the queue, has not been sent";
- Break ;
- Case mfmailcomposeresultfailed:
- msg = @"user attempted to save or send message failed";
- Break ;
- Default:
- msg = @"";
- Break ;
- }
- [Self alertwithmessage:msg];
- }
Send mail in APP