Two ways to send email to iOS

Source: Internet
Author: User
Tags mail account

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:
  1. #pragma mark-Send mail using System mail client
  2. -(void) Launchmailapp
  3. {
  4. nsmutablestring *mailurl = [[[Nsmutablestring alloc]init]autorelease];
  5. //Add Recipients
  6. Nsarray *torecipients = [Nsarray arraywithobject: @"[email protected]"];
  7. [Mailurl appendformat:@"mailto:%@", [torecipients componentsjoinedbystring:@","]];
  8. //Add cc
  9. Nsarray *ccrecipients = [Nsarray arraywithobjects:@"[email protected]", @"[email protected]", nil];
  10. [Mailurl appendformat:@"? cc=%@", [ccrecipients componentsjoinedbystring:@","]];
  11. //Add encryption to send
  12. Nsarray *bccrecipients = [Nsarray arraywithobjects:@"[email protected]", nil];
  13. [Mailurl appendformat:@"&bcc=%@", [bccrecipients componentsjoinedbystring:@","]];
  14. //Add Theme
  15. [Mailurl appendstring:@"&subject=my Email"];
  16. //Add message content
  17. [Mailurl appendstring:@"&body=<b>email</b> body!"];
  18. nsstring* email = [Mailurl stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
  19. [[UIApplication Sharedapplication] OpenURL: [Nsurl Urlwithstring:email]];
  20. }

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

  1. //
  2. ViewController.h
  3. Maildemo
  4. //
  5. Created by luoyl on 12-4-4.
  6. Copyright (c) 2012 Http://luoyl.info. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #import <MessageUI/MFMailComposeViewController.h>
  10. @interface viewcontroller:uiviewcontroller<mfmailcomposeviewcontrollerdelegate>
  11. @end
  1. #pragma mark-Send messages within the app
  2. Activate Mail feature
  3. -(void) Sendmailinapp
  4. {
  5. Class Mailclass = (nsclassfromstring (@"Mfmailcomposeviewcontroller"));
  6. if (!mailclass) {
  7. [Self alertwithmessage:@] The current system version does not support in-app send mail functionality, you can use the Mailto method instead of "];
  8. return;
  9. }
  10. if (![ Mailclass Cansendmail]) {
  11. [Self alertwithmessage:@"user does not have a mail account set up"];
  12. return;
  13. }
  14. [Self displaymailpicker];
  15. }
  16. Bring up the mail send window
  17. -(void) Displaymailpicker
  18. {
  19. Mfmailcomposeviewcontroller *mailpicker = [[Mfmailcomposeviewcontroller alloc] init];
  20. Mailpicker.mailcomposedelegate = self;
  21. //Set Theme
  22. [Mailpicker setsubject: @"email theme"];
  23. //Add Recipients
  24. Nsarray *torecipients = [Nsarray arraywithobject: @"[email protected]"];
  25. [Mailpicker settorecipients:torecipients];
  26. //Add cc
  27. Nsarray *ccrecipients = [Nsarray arraywithobjects:@"[email protected]", @"[email protected]", nil];
  28. [Mailpicker setccrecipients:ccrecipients];
  29. //Add encryption to send
  30. Nsarray *bccrecipients = [Nsarray arraywithobjects:@"[email protected]", nil];
  31. [Mailpicker setbccrecipients:bccrecipients];
  32. //Add a picture
  33. UIImage *addpic = [UIImage imagenamed: @"[email protected]"];
  34. NSData *imagedata = uiimagepngrepresentation (addpic); //PNG
  35. //About mimetype:http://www.iana.org/assignments/media-types/index.html
  36. [Mailpicker addattachmentdata:imagedata MimeType: @ "" FileName: @"Icon.png"];
  37. //Add a PDF attachment
  38. NSString *file = [self fullbundlepathfromrelativepath:@"high quality C + + Programming Guide. pdf"];
  39. NSData *pdf = [NSData datawithcontentsoffile:file];
  40. [Mailpicker addattachmentdata:pdf MimeType: @ "" FileName: @"High quality C + + Programming Guide. pdf"];
  41. NSString *emailbody = @"<font color= ' red ' >eMail</font> text";
  42. [Mailpicker setmessagebody:emailbody Ishtml:yes];
  43. [Self presentmodalviewcontroller:mailpicker animated:yes];
  44. [Mailpicker release];
  45. }
  46. #pragma mark-Achieve mfmailcomposeviewcontrollerdelegate
  47. -(void) Mailcomposecontroller: (Mfmailcomposeviewcontroller *) controller didfinishwithresult: ( Mfmailcomposeresult) Result Error: (NSERROR *) Error
  48. {
  49. //close mail sending window
  50. [Self dismissmodalviewcontrolleranimated:yes];
  51. NSString *msg;
  52. switch (result) {
  53. Case mfmailcomposeresultcancelled:
  54. msg = @"User cancels edit mail";
  55. Break ;
  56. Case mfmailcomposeresultsaved:
  57. msg = @"User successfully saved mail";
  58. Break ;
  59. Case Mfmailcomposeresultsent:
  60. msg = @"The user clicks Send, puts the message in the queue, has not been sent";
  61. Break ;
  62. Case mfmailcomposeresultfailed:
  63. msg = @"user attempted to save or send message failed";
  64. Break ;
  65. Default:
  66. msg = @"";
  67. Break ;
  68. }
  69. [Self alertwithmessage:msg];
  70. }

Two ways to send email to iOS

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.