当前位置: 移动技术网 > 移动技术>移动开发>IOS > IOS 开发中发送e-mail的几种方法总结

IOS 开发中发送e-mail的几种方法总结

2019年07月24日  | 移动技术网移动技术  | 我要评论

ios系统框架提供的两种发送email的方法      

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; 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网