中国领先的工业平台

返回贤集网 返回微头条
贤集网技术微头条APP获取

iOS开发之邮件发送代码

 山东大明消毒科技有限公司

下载贤集网APP入驻自媒体

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

一、创建视图控制器:

?

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

二、设置邮件主题:

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

2、设置cc

[code][mc setMessageBody:@"Hello, Joe!

What do you know?" 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];

}


最新回复

还没有人回复哦,抢沙发吧~

发布回复

为您推荐

热门交流