IOS iPhone开发中发送e-mail的3种方式

34 篇文章 0 订阅
20 篇文章 0 订阅
转自:http://blog.csdn.net/zhibudefeng/article/details/12952203[objc] view plain  copy
  1. iOS系统框架提供的两种发送Email的方法  
  2.    
  3.   
  4. 1、使用openURL来实现发邮件的功能:  
  5. (

                /* 例如:

                    NSString *stringURL = @"mailto:test@example.com";

                    NSURL *url = [NSURL URLWithString:stringURL];

                    [[UIApplication sharedApplication] openURL:url];

                   //或者可以进行设置

                      NSString *subject = @"Message subject";

                    NSString *body = @"Message body";

                    NSString *address = @"test1@akosma.com";

                    NSString *cc = @"test2@akosma.com";

                    NSString *path = [NSString stringWithFormat:@"mailto:%@?cc=%@&subject=%@&body=%@", address, cc, subject, body];

                    NSURL *url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

                    [[UIApplication sharedApplication] openURL:url];

                    //或者你可以省略一些信息

                    NSString *subject = @"Message subject";

                    NSString *path = [NSString stringWithFormat:@"mailto:?subject=%@", subject];

                    NSURL *url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

                    [[UIApplication sharedApplication] openURL:url];

                    //对于更复杂的文本,您可能想要使用CFURLCreateStringByAddingPercentEscapes()函数,如前所述在这个页面。                                   */

    )
  6.   
  7. NSString *url = [NSString stringWithString@"mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];  
  8. [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];  
  9. 缺点很明显,这样的过程会导致程序暂时退出,即使在iOS 4.x支持多任务的情况下,这样的过程还是会让人觉得不是很方便。  
  10.   
  11. 2、使用MFMailComposeViewController来实现发邮件的功能,它在MessageUI.framework中,你需要在项目中加入该框架,并在使用的文件中导入MFMailComposeViewController.h头文件。  
  12. #import <MessageUI/MFMailComposeViewController.h>;  
  13.    
  14. MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];  
  15. controller.mailComposeDelegate = self;  
  16. [controller setSubject:@"My Subject"];  
  17. [controller setMessageBody:@"Hello there." isHTML:NO];  
  18. [self presentModalViewController:controller animated:YES];  
  19. [controller release];  
  20. 使用该方法实现发送Email是最常规的方法,该方法有相应的MFMailComposeViewControllerDelegate事件:  
  21.   
  22. - (void)mailComposeController:(MFMailComposeViewController*)controller  
  23.           didFinishWithResult:(MFMailComposeResult)result  
  24.                         error:(NSError*)error;  
  25. {  
  26.   if (result == MFMailComposeResultSent) {  
  27.     NSLog(@"It's away!");  
  28.   }  
  29.   [self dismissModalViewControllerAnimated:YES];  
  30. }  
  31. 有一些相关的数据结构的定义在头文件中都有具体的描述:  
  32.   
  33. enum MFMailComposeResult {  
  34.     MFMailComposeResultCancelled,//用户取消编辑邮件  
  35.     MFMailComposeResultSaved,//用户成功保存邮件  
  36.     MFMailComposeResultSent,//用户点击发送,将邮件放到队列中  
  37.     MFMailComposeResultFailed//用户试图保存或者发送邮件失败  
  38. };  
  39. typedef enum MFMailComposeResult MFMailComposeResult;   // iOS3.0以上有效  
  40. 在头文件中MFMailComposeViewController的部分方法顺便提及:  
  41.   
  42. + (BOOL)canSendMail __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);  
  43. //如果用户没有设置邮件账户,则会返回NO,你可以用根据返回值来决定是使用MFMailComposeViewController 还是 mailto://的传统方法,也或者,你可以选择上文中提到的skpsmtpmessage来实现发送Email的功能。  
  44. - (void)addAttachmentData:(NSData *)attachment mimeType:(NSString *)mimeType fileName:(NSString *)filename;  
  45. //NSData类型的attachment自然不必多说,关于mimeType需要一点说明,官方文档里给出了一个链接http://www.iana.org/assignments/media-types/ ,这里列出的所有的类型都应该支持。关于mimeType的用处,更多需要依靠搜索引擎了 =]  
  46. 第二种方法的劣势也很明显,iOS系统替我们提供了一个mail中的UI,而我们却完全无法对齐进行订制,这会让那些定制化成自己风格的App望而却步,因为这样使用的话无疑太突兀了。  
  47.   
  48. 3、我们可以根据自己的UI设计需求来定制相应的视图以适应整体的设计。可以使用比较有名的开源SMTP协议来实现。  
  49.   
  50. 在SKPSMTPMessage类中,并没有对视图进行任何的要求,它提供的都是数据层级的处理,你之需要定义好相应的发送要求就可以实现邮件发送了。至于是以什么样的方式获取这些信息,就可以根据软件的需求来确定交互方式和视图样式了。  
  51.   
  52.                 SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];  
  53.         testMsg.fromEmail = @"test@gmail.com";  
  54.         testMsg.toEmail =@"to@gmail.com";  
  55.         testMsg.relayHost = @"smtp.gmail.com";  
  56.         testMsg.requiresAuth = YES;  
  57.         testMsg.login = @"test@gmail.com";  
  58.         testMsg.pass = @"test";  
  59.         testMsg.subject = [NSString stringWithCString:"测试" encoding:NSUTF8StringEncoding];  
  60.         testMsg.bccEmail = @"bcc@gmail.com";  
  61.         testMsg.wantsSecure = YES// smtp.gmail.com doesn't work without TLS!  
  62.    
  63.         // Only do this for self-signed certs!  
  64.         // testMsg.validateSSLChain = NO;  
  65.         testMsg.delegate = self;  
  66.    
  67.         NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,  
  68.                                    [NSString stringWithCString:"测试正文" encoding:NSUTF8StringEncoding],kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];  
  69.    
  70.             NSString *vcfPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"vcf"];  
  71.             NSData *vcfData = [NSData dataWithContentsOfFile:vcfPath];  
  72.    
  73.             NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"test.vcf\"",kSKPSMTPPartContentTypeKey,  
  74.                                      @"attachment;\r\n\tfilename=\"test.vcf\"",kSKPSMTPPartContentDispositionKey,[vcfData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];  
  75.    
  76.         testMsg.parts = [NSArray arrayWithObjects:plainPart,vcfPart,nil];  
  77.    
  78.         [testMsg send];  
  79. 该类也提供了相应的Delegate方法来让你更好的获知发送的状态.  
  80.   
  81. -(void)messageSent:(SKPSMTPMessage *)message;  
  82. -(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error;  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值