IOS development-Email sending

Source: Internet
Author: User

IOS development-Email sending
During Web development, email registration and logon are essential, and mobile phone numbers can be changed. However, email addresses are used only when they are important, such as the required email address when looking for a job, email verification for registered website Members. The current mobile phone and Web are actually the same. In most cases, emails are sent. There are two ways to send emails on iOS. One is to use the built-in MFMailComposeViewController to send emails, the second method is to use the third-party class library SKPSMTPMessage to send an email MFMailComposeViewController. after sending an iOS3.0 message, you can use the built-in MFMailComposeViewController to form an email sending interface, which is not supported by the simulator, in addition, the default Interface does not conform to the mail sending habits of Chinese people, but since iOS is ready, you can simply look at the effect: first import MessageUI (# import <MessageUI/MessageUI. h>) to implement the MFMailComposeViewControllerDelegate protocol. The mail sending method is-(void) displayComposerSheet {MFMailComposeViewContro Roller * picker = [[MFMailComposeViewController alloc] init]; picker. mailComposeDelegate = self; // set the topic [picker setSubject: @ "-FlyElephant"]; // set the recipient NSArray * toRecipients = [NSArray arrayWithObjects: @ "FlyElephant@163.com ", nil]; NSArray * ccRecipients = [NSArray inputs: @ "10000@qq.com", @ "10000@sina.com", nil]; NSArray * bccRecipients = [NSArray arrayWithObjects: @ "keso@sina.com", nil]; [pic Ker setToRecipients: toRecipients]; [picker setCcRecipients: ccRecipients]; [picker parameters: bccRecipients]; // set the attachment to the image NSString * path = [[NSBundle mainBundle] pathForResource: @ "man" ofType: @ "jpg"]; NSData * myData = [NSData dataWithContentsOfFile: path]; [picker addAttachmentData: myData mimeType: @ "image/png" fileName: @ "man"]; // set the mail content NSString * emailBody = @ "personal blog address in IOS: http://www.cnblogs.c Om/xiaofeixiang "; [picker setMessageBody: emailBody isHTML: NO]; // modal window for mail sending [self presentModalViewController: picker animated: YES];} Method for sending a call to mail completion: -(void) mailComposeController :( MFMailComposeViewController *) controller didFinishWithResult :( MFMailComposeResult) result error :( NSError *) error {switch (result) {case MFMailComposeResultCancelled: // cancel NSLog (@ "MFMailComposeResultCancelled-cancel"); break; case Secure: // save NSLog (@ "secure-save mail"); break; case MFMailComposeResultSent: // send NSLog (@ "MFMailComposeResultSent-send mail"); break; case MFMailComposeResultFailed: // NSLog (@ "MFMailComposeResultFailed: % @... ", [error localizedDescription]); break;} // close the mail sending view [self dismissViewControllerAnimated: YES completion: nil];} SKPSMTPMessage sends this third-party class library early, there are also many references It is very easy to encapsulate common emails and call them directly. After downloading, you will see a Demo and a SMTPLibrary, which will directly copy SMTPLibray to the project, reference: ① import the SKPSMTPMessage and NSData + Base64Additions header files on the Controller to be sent # import "SKPSMTPMessage. h "# import" NSData + Base64Additions. h "② implement the SKPSMTPMessageDelegate protocol; ③ specific sending method: SKPSMTPMessage * myMessage = [[SKPSMTPMessage alloc] init]; myMessage. fromEmail = @ "keso@163.com"; myMessage. toEmail = @ "FlyElephant@qq.com"; myMessage. bccEmail = @ "Goo Dman@qq.com "; myMessage. relayHost = @ "smtp.163.com"; myMessage. requiresAuth = YES; if (myMessage. requiresAuth) {myMessage. login = @ "keso@163.com"; myMessage. pass = @ "keso";} myMessage. wantsSecure = YES; // set smtp.gmail.com myMessage for the gmail mailbox. subject = @ "-FlyElephant"; myMessage. delegate = self; // set the mail content NSDictionary * plainPart = [NSDictionary dictionaryWithObjectsAndKeys: @ "text/plain; charset = UTF-8", kS KPSMTPPartContentTypeKey, @ "make a simple and happy programmer ~ Keso ", kSKPSMTPPartMessageKey, @" 8bit ", kSKPSMTPPartContentTransferEncodingKey, nil]; myMessage. parts = [NSArray arrayWithObjects: plainPart, nil]; dispatch_async (dispatch_get_global_queue (queue, 0), ^ {[myMessage send] ;}); ④ set processing after sending: -(void) messageSent :( SKPSMTPMessage *) message {NSLog (@ "congratulations, the email is successfully sent");}-(void) messageFailed :( SKPSMTPMessage *) message error :( NSError *) error {NSLo G (@ "sorry, email sending failed");} I sent the email to QQ via email 163. I have not tried sending emails between other mailboxes. The above has implemented simple email sending, for specific mailbox settings, refer to the previous mail article: C # mail sending (the most boring mailbox-QQ mailbox ). Email sending is relatively simple. If you have any questions, please feel free to contact me ~

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.