Email sending code for iOS development

Source: Internet
Author: User

IOS developmentOfEmail sendingThe Code is the content to be introduced in this article,Email sendingThe function is provided by MessageUI Framework, which is the simplest box in the iPhone SDK. It consists of a class, A View Controller, and a protocol.

1. Create a View Controller:

 
 
  1. MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];     
  2. mc.mailComposeDelegate = self;  

Ii. Set Email Subject:

 
 
  1. [mc setSubject:@"Hello, World!"]; 

3. Set recipients. There are three types of recipients:

1. Set the primary recipient

 
 
  1. [mc setToRecipients:[NSArray arrayWithObjects:@"zhuqi0@126.com",     
  2.         "@dave@iphonedevbook.com", nil]; 

2. Set cc

 
 
  1. [mc setCcRecipients:[NSArray arrayWithObject:@"zhuqil@163.com"]]; 

3. Set bcc

 
 
  1. [mc setBccRecipients:[NSArray arrayWithObject:@"secret@gmail.com"]];  

4. Set the email subject in two formats.

One is plain text

 
 
  1. [mc setMessageBody:@"Watson!!!\n\nCome here, I need you!" isHTML:NO];  

One is in html format.

 
 
  1. [mc setMessageBody:@"<HTML><B>Hello, Joe!</B><BR/>What do you know?</HTML>"    
  2.        isHTML:YES];  

5. Add attachments

Adding an attachment requires three parameters: an attachment of the NSData type, a mime type, and an attachment name.

 
 
  1.   NSString *path = [[NSBundle mainBundle] pathForResource:@"blood_orange"            ofType:@"png"];    
  2.  NSData *data = [NSData dataWithContentsOfFile:path];    
  3. [mc addAttachmentData:data mimeType:@"image/png" fileName:@"blood_orange"];    

Vi. View presentation

 
 
  1. [self presentModalViewController:mc animated:YES];   
  2. [mc release]; 

VII. View Controller delegation Method

The delegate method of the mail View Controller is included in MFMailComposeViewControllerDelegate. Whether or not the user sends or cancels the message, whether the system can implementEmail sending,

Method mailComposeController: didFinishWithResult: error: gets called will be called.

 
 
  1. - (void)mailComposeController:(MFMailComposeViewController*)controller            
  2. didFinishWithResult:(MFMailComposeResult)result            
  3.                      error:(NSError*)error   
  4.  {        
  5.  switch (result){           
  6.  case MFMailComposeResultCancelled:  NSLog(@"Mail send canceled...");               
  7.          break;          
  8.   case MFMailComposeResultSaved:    NSLog(@"Mail saved...");              
  9.         break;           
  10.         case MFMailComposeResultSent:             NSLog(@"Mail sent...");               
  11.         break;           
  12.         case MFMailComposeResultFailed:  NSLog(@"Mail send errored: %@...", [error localizedDescription]);     
  13.        break;           
  14.      default:             break;  
  15.  }   
  16. [self dismissModalViewControllerAnimated:YES];  
  17.  } 

Summary: AnalysisIOS developmentOfEmail sendingI hope this article will help you with the introduction of the Code!

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.