IOS development-e-mail (E-mail) method collection (3 in total), ios collection

Source: Internet
Author: User
Tags email account

IOS development-e-mail (E-mail) method collection (3 in total), ios collection

In IOS development, sometimes we need to use the email sending function. For example, receive user feedback and program crash notifications. In fact, this function is very commonly used, because I currently have the development requirements for sending emails, so by the way, sort out the IOS mail sending method.

 

IOS native has two methods to send emails, and the other is to use a third-party Library:

1) openURL (native)

-- If the user experience is poor, the program will go to the background and jump to the mail sending interface.

2) MFMailComposeViewController (native)

-- Does not go to the background. The mail sending view is displayed in the modal mode.

3) SKPSMTPMessage (https://github.com/jetseven/skpsmtpmessage)

-- You don't need to tell the user about the email to be sent. I'm wondering if this does not conform to Apple's theory. Of course, you can also get a pop-up box to inform the user before sending the message, and ask the user to choose whether to send or not.

 

The following code is available on a real machine (IOS8.0.

 

1. Use openURL to send an email:

Create a variable address String object:

NSMutableString *mailUrl = [[NSMutableString alloc] init];

 

Add recipient:

NSArray * toRecipients = @ [@ "write your own email test @ qq.com"]; // Note: if there are multiple recipients, you can use the componentsJoinedByString method to connect. The connector is @", "[mailUrl appendFormat: @" mailto: % @ ", toRecipients [0];

 

Add CC recipients:

NSArray * ccRecipients = @ [@ "1229436624@qq.com"]; [mailUrl appendFormat :@"? Cc = % @ ", ccRecipients [0];View Code

 

Add BCC:

NSArray * bccRecipients = @ [@ "shana_happy@126.com"]; [mailUrl appendFormat: @ "& bcc = % @", bccRecipients [0];View Code

 

Add the subject and content of the email:

[mailUrl appendString:@"&subject=my email"];[mailUrl appendString:@"&body=<b>Hello</b> World!"];

 

Open the address, which will jump to the mail sending interface:

NSString *emailPath = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];[[UIApplication sharedApplication] openURL:[NSURL URLWithString:emailPath]];

 

:

Ii. Use MFMailComposeViewController to send emails

Note before use:

1) the project needs to import the framework: MessageUI. framework

2) import the header file in Controlelr: # import <MessageUI/MessageUI. h>

 

Obtain whether the email account is set by the user:

If ([MFMailComposeViewController canSendMail]) {// The user has set the email account [self sendEmailAction]; // call the email sending code}

 

SendEmailAction method code:

-(Void) sendEmailAction {// The email server MFMailComposeViewController * mailCompose = [[MFMailComposeViewController alloc] init]; // set the mail proxy [mailCompose setMailComposeDelegate: self]; // set the mail subject [mailCompose setSubject: @ ""]; // set the recipient [mailCompose setToRecipients: @ [@ "1147626297@qq.com"]; // set the CC to [mailCompose setCcRecipients: @ [@ "1229436624@qq.com"]; // set the bcc to [mailCompose setBccRecipients: @ [@ "shana_happ Y@126.com "];/*** set the body content of the mail */NSString * emailContent = @" I am the body of the mail "; // whether it is in HTML format [mailCompose setMessageBody: emailContent isHTML: NO]; // If the HTML format is used, the following code // [mailCompose setMessageBody: @ "

 

The proxy method of MFMailComposeViewControllerDelegate:

-(Void) mailComposeController :( MFMailComposeViewController *) controller didFinishWithResult :( MFMailComposeResult) result error :( NSError *) error {switch (result) {case MFMailComposeResultCancelled: // cancel NSLog editing (@ "Mail send canceled... "); break; case MFMailComposeResultSaved: // The NSLog (@" Mail saved... "); break; case MFMailComposeResultSent: // The NSLog (@" Mail sent... "); break; case MFMailComposeResultFailed: // NSLog (@" Mail send errored: % @... ", [error localizedDescription]); break;} // close the mail sending view [self dismissViewControllerAnimated: YES completion: nil];}

 

Program running:

In IOS mail, attachments are directly displayed below the body, but do not mistakenly think that the image is placed in the body. The two are different!

 

3. Use the third-party library SKPSMTPMessage to send emails 

Note before use:

1) download a third-party library (beginning with the article)

2) import class # import "SKPSMTPMessage. h", # import "NSData + Base64Additions. h"

 

Set Basic parameters:

SKPSMTPMessage * mail = [[SKPSMTPMessage alloc] init]; [mail setSubject: @ "I Am a topic"]; // set the mail subject [mail setToEmail: @ "fill in your own @ qq.com"]; // target Email Address [mail setFromEmail: @ "fill in your own @ qq.com"]; // the sender's email address [mail setRelayHost: @ "smtp.qq.com"]; // send the mail proxy server [mail setRequiresAuth: YES]; [mail setLogin: @ "fill in your own @ qq.com"]; // The sender's email account [mail setPass: @ "fill in your own"]; // the sender's email password [mail setWantsSecure: YES]; // You Need to encrypt [mail setDelegate: self];

 

Set the body of the email:

NSString * content = [NSString stringWithCString: "Test content" encoding: NSUTF8StringEncoding]; NSDictionary * plainPart =@{ plaintext: @ "text/plain", kSKPSMTPPartMessageKey: content, expires: @ "8bit "};

 

Add attachments (the following code can be found in dmeo of the SKPSMTPMessage Library ):

NSString *vcfPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"vcf"];NSData *vcfData = [NSData dataWithContentsOfFile:vcfPath];NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"test.vcf\"",kSKPSMTPPartContentTypeKey,                         @"attachment;\r\n\tfilename=\"test.vcf\"",kSKPSMTPPartContentDispositionKey,[vcfData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];

 

Run the email sending code:

[Mail setParts: @ [plainPart, vcfPart]; // mail header field, mail content format, and transmission code [mail send];

 

SKPSMTPMessage proxy, which can be known as success/failure for subsequent steps:

- (void)messageSent:(SKPSMTPMessage *)message{    NSLog(@"%@", message);}- (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error{    NSLog(@"message - %@\nerror - %@", message, error);}

 

:

What? No, because of willfulness.

Here, the user is not notified to send an email, so no.

 

Summary: I didn't want to write the third-party library because it always feels insecure. Don't ask me why, man's intuition? In fact, the code is too ugly to understand. It may be that my technology is too good.

 

This article draws on:

Http://blog.sina.com.cn/s/blog_7d280d7c0101da7d.html

Http://www.cnblogs.com/zhuqil/archive/2011/07/21/2112816.html

Http://blog.csdn.net/zhibudefeng/article/details/7677421

 

 

 

Blog Author: GarveyCalvin

Blog Source: http://www.cnblogs.com/GarveyCalvin/

The copyright of this article is shared by the author and the blog. You are welcome to repost it, but you must keep this statement and provide the original article link. Thank you for your cooperation!

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.