iPhone开发中发送e-mail的3种方式
来源:互联网 发布:二次元站源码 编辑:程序博客网 时间:2024/04/27 13:21
1、使用openURL来实现发邮件的功能:
NSString *url = [NSString stringWithString: @"mailto:foo@example.com?cc=bar@example.com& subject=Greetings%20from%20Cupertino!& body=Wish%20you%20were%20here!"];[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
缺点很明显,这样的过程会导致程序暂时退出,即使在iOS 4.x支持多任务的情况下,这样的过程还是会让人觉得不是很方便。
2、使用MFMailComposeViewController来实现发邮件的功能,它在MessageUI.framework中,你需要在项目中加入该框架,并在使用的文件中导入MFMailComposeViewController.h头文件。
#import <MessageUI/MFMailComposeViewController.h>; MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];controller.mailComposeDelegate = self;[controller setSubject:@"My Subject"];[controller setMessageBody:@"Hello there." isHTML:NO];[self presentModalViewController:controller animated:YES];[controller release];
使用该方法实现发送Email是最常规的方法,该方法有相应的MFMailComposeViewControllerDelegate事件:
- (void)mailComposeController: (MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error;{ if (result == MFMailComposeResultSent) { NSLog(@"It's away!"); } [self dismissModalViewControllerAnimated:YES];}
有一些相关的数据结构的定义在头文件中都有具体的描述:
enum MFMailComposeResult { MFMailComposeResultCancelled,//用户取消编辑邮件 MFMailComposeResultSaved,//用户成功保存邮件 MFMailComposeResultSent,//用户点击发送,将邮件放到队列中 MFMailComposeResultFailed//用户试图保存或者发送邮件失败 };typedef enum MFMailComposeResult MFMailComposeResult; // iOS3.0以上有效
在头文件中MFMailComposeViewController的部分方法顺便提及:
+ (BOOL)canSendMail __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);//如果用户没有设置邮件账户,则会返回NO,你可以用根据返回值来决定是 使用MFMailComposeViewController 还是 mailto://的传统方法, 也或者, 你可以选择上文中提到的skpsmtpmessage来实现发送Email的功能。
- (void)addAttachmentData:(NSData *)attachment mimeType:(NSString *)mimeType fileName:(NSString *)filename;//NSData类型的attachment自然不必多说,关于mimeType需要一点说明, 官方文档里给出了一个链接http://www.iana.org/assignments/media-types/ , 这里列出的所有的类型都应该支持。关于mimeType的用处,更多需要依靠搜索引擎了 =]
第二种方法的劣势也很明显,iOS系统替我们提供了一个mail中的UI,而我们却完全无法对齐进行订制,这会让那些定制化成自己风格的App望而却步,因为这样使用的话无疑太突兀了。
3、我们可以根据自己的UI设计需求来定制相应的视图以适应整体的设计。可以使用比较有名的开源SMTP协议来实现。
在SKPSMTPMessage类中,并没有对视图进行任何的要求,它提供的都是数据层级的处理,你之需要定义好相应的发送要求就可以实现邮件发送了。至于是以什么样的方式获取这些信息,就可以根据软件的需求来确定交互方式和视图样式了。
SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];testMsg.fromEmail = @"test@gmail.com";testMsg.toEmail =@"to@gmail.com";testMsg.relayHost = @"smtp.gmail.com";testMsg.requiresAuth = YES;testMsg.login = @"test@gmail.com";testMsg.pass = @"test";testMsg.subject = [NSString stringWithCString:"测试" encoding:NSUTF8StringEncoding];testMsg.bccEmail = @"bcc@gmail.com";testMsg.wantsSecure = YES; // smtp.gmail.com doesn't work without TLS! // Only do this for self-signed certs! // testMsg.validateSSLChain = NO; testMsg.delegate = self; NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,[NSString stringWithCString:"测试正文" encoding:NSUTF8StringEncoding],kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil]; NSString *vcfPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"vcf"];NSData *vcfData = [NSData dataWithContentsOfFile:vcfPath]; NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys: @"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"test.vcf\"",kSKPSMTPPartContentTypeKey,@"attachment;\r\n\tfilename=\"test.vcf\"",kSKPSMTPPartContentDispositionKey,[vcfData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil]; testMsg.parts = [NSArray arrayWithObjects:plainPart,vcfPart,nil]; [testMsg send];
该类也提供了相应的Delegate方法来让你更好的获知发送的状态.
-(void)messageSent:(SKPSMTPMessage *)message;-(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error;
- iPhone开发中发送e-mail的3种方式
- iPhone开发中发送e-mail的3种方式
- iPhone开发中发送e-mail的3种方式
- iphone开发中发送e-mail的3种方式
- iPhone开发中发送e-mail的3种方式
- IOS iPhone开发中发送e-mail的3种方式
- IOS iPhone开发中发送e-mail的3种方式
- IOS iPhone开发中发送e-mail的3种方式
- 发送e-mail的3种方式
- PHP中关于邮件E-mail的发送案例
- iOS开发-发送邮件(E-mail)方法整理合集(共3种)
- SEND E-MAIL ASP.NET 发送E-Mail的代码
- 从Java应用程序中发送E-mail
- 在Director中发送E-mail
- C#中使用MAPI发送E-Mail
- 从Java应用程序中发送E-mail
- SAP 中发送e-Mail的方法,如何发送附件?如何在附件中显示中文?
- Lion的发送E-mail的类
- iOS小技巧:用小图片重复显示来设置大背景图片
- iOS 6 IAP入门
- Objective-C解析HTML
- 复杂XML的解析及组装
- 使用CATiledLayer优化PDF展示
- iPhone开发中发送e-mail的3种方式
- 在 iOS 中使用 OpenSSL 库
- [ASP.NET MVC 小牛之路]04 - 依赖注入(DI)和Ninject
- warning: Signal <> missing in the sensitivity list is added for synthesis purposes
- ios获得通讯录中联系人的所有属性
- OpenSSL在苹果手机推送中的使用
- 黑马程序员_java注解
- iOS学习笔记–10 IOS之云端应用
- HDU 4278