国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

IOS 開發(fā)中發(fā)送e-mail的幾種方法總結(jié)

2019-10-21 18:47:45
字體:
供稿:網(wǎng)友

iOS系統(tǒng)框架提供的兩種發(fā)送Email的方法      

1、使用openURL來實(shí)現(xiàn)發(fā)郵件的功能:   

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]]; 

缺點(diǎn)很明顯,這樣的過程會導(dǎo)致程序暫時退出,即使在iOS 4.x支持多任務(wù)的情況下,這樣的過程還是會讓人覺得不是很便。   

2、使用MFMailComposeViewController來實(shí)現(xiàn)發(fā)郵件的功能,它在MessageUI.framework中,你需要在項(xiàng)目中加入該框架,并在使用的文件中導(dǎo)入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]; //使用該方法實(shí)現(xiàn)發(fā)送Email是最常規(guī)的方法,該方法有相應(yīng)的MFMailComposeViewControllerDelegate事件:  - (void)mailComposeController:(MFMailComposeViewController*)controller    didFinishWithResult:(MFMailComposeResult)result       error:(NSError*)error; {  if (result == MFMailComposeResultSent) {  NSLog(@"It's away!");  }  [self dismissModalViewControllerAnimated:YES]; } //有一些相關(guān)的數(shù)據(jù)結(jié)構(gòu)的定義在頭文件中都有具體的描述:  enum MFMailComposeResult {  MFMailComposeResultCancelled,//用戶取消編輯郵件  MFMailComposeResultSaved,//用戶成功保存郵件  MFMailComposeResultSent,//用戶點(diǎn)擊發(fā)送,將郵件放到隊(duì)列中  MFMailComposeResultFailed//用戶試圖保存或者發(fā)送郵件失敗 }; typedef enum MFMailComposeResult MFMailComposeResult; // iOS3.0以上有效 //在頭文件中MFMailComposeViewController的部分方法順便提及:  + (BOOL)canSendMail __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); //如果用戶沒有設(shè)置郵件賬戶,則會返回NO,你可以用根據(jù)返回值來決定是使用MFMailComposeViewController 還是 mailto://的傳統(tǒng)方法,也或者,你可以選擇上文中提到的skpsmtpmessage來實(shí)現(xiàn)發(fā)送Email的功能。 - (void)addAttachmentData:(NSData *)attachment mimeType:(NSString *)mimeType fileName:(NSString *)filename; //NSData類型的attachment自然不必多說,關(guān)于mimeType需要一點(diǎn)說明,官方文檔里給出了一個鏈接http://www.iana.org/assignments/media-types/ ,這里列出的所有的類型都應(yīng)該支持。關(guān)于mimeType的用處,更多需要依靠搜索引擎了 =] 

第二種方法的劣勢也很明顯,iOS系統(tǒng)替我們提供了一個mail中的UI,而我們卻完全無法對齊進(jìn)行訂制,這會讓那些定制化成自己風(fēng)格的App望而卻步,因?yàn)檫@樣使用的話無疑太突兀了。  

3、我們可以根據(jù)自己的UI設(shè)計需求來定制相應(yīng)的視圖以適應(yīng)整體的設(shè)計。可以使用比較有名的開源SMTP協(xié)議來實(shí)現(xiàn)。   

在SKPSMTPMessage類中,并沒有對視圖進(jìn)行任何的要求,它提供的都是數(shù)據(jù)層級的處理,你之需要定義好相應(yīng)的發(fā)送要求就可以實(shí)現(xiàn)郵件發(fā)送了。至于是以什么樣的方式獲取這些信息,就可以根據(jù)軟件的需求來確定交互方式和視圖樣式了。  

    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]; //該類也提供了相應(yīng)的Delegate方法來讓你更好的獲知發(fā)送的狀態(tài).  -(void)messageSent:(SKPSMTPMessage *)message; -(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error; 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關(guān)教程知識閱讀請移步到IOS開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 昌平区| 新田县| 丰原市| 五常市| 永清县| 深水埗区| 保靖县| 武强县| 连江县| 泰和县| 徐水县| 富顺县| 霍邱县| 哈尔滨市| 阿城市| 迭部县| 宝山区| 永康市| 武冈市| 汶上县| 平武县| 平果县| 个旧市| 牡丹江市| 永川市| 惠安县| 化隆| 盘山县| 焉耆| 临夏市| 新沂市| 高阳县| 岳阳市| 玉树县| 增城市| 长汀县| 成都市| 随州市| 灌阳县| 滨海县| 乌海市|