MessageUI Framework邮件发送功能

邮件发送功能是由MessageUI Framework提供的,这个框架是iPhone sdk中最简单的框。由一个类、一个视图控制器,一个protocol组成。

一、创建视图控制器:

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

二、设置邮件主题:

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

三、设置收件人,收件人有三种:

1、设置主收件人

[mc setToRecipients:[NSArray arrayWithObjects:@"[email protected]",
        "@dave @iphonedevbook.com", nil];

2、设置cc

 [mc setCcRecipients:[NSArray arrayWithObject:@"[email protected]"]];

3、设置bcc

[mc setBccRecipients:[NSArray arrayWithObject:@"[email protected]"]]; 

四、设置邮件主体,有两种格式。

一种是纯文本

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

一个是html格式

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

五、添加附件

添加附件需要三个参数,一个是NSData类型的附件,一个是mime type,一个附件的名称。

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

六、视图呈现

 [self presentModalViewController:mc animated:YES]; 
    [mc release];

七、视图控制器的委托方法

邮件视图控制器的委托方法包含在MFMailComposeViewControllerDelegate中,无论用户是否发送或取消发送,不论系统是否能够发送邮件,

方法 mailComposeController:didFinishWithResult:error: gets called都会被调用。

- (void)mailComposeController:(MFMailComposeViewController*)controller  
        didFinishWithResult:(MFMailComposeResult)result  
        error:(NSError*)error { switch (result) 
    { case MFMailComposeResultCancelled: 
            NSLog(@"Mail send canceled..."); break; case MFMailComposeResultSaved: 
            NSLog(@"Mail saved..."); break; case MFMailComposeResultSent: 
            NSLog(@"Mail sent..."); break; case MFMailComposeResultFailed: 
            NSLog(@"Mail send errored: %@...", [error localizedDescription]); break; default: break; 
    } 
    [self dismissModalViewControllerAnimated:YES]; 
} 

你可能感兴趣的:(MessageUI Framework邮件发送功能)