溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

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

發(fā)布時(shí)間:2020-10-05 02:44:37 來(lái)源:腳本之家 閱讀:145 作者:墨魚(yú)精 欄目:移動(dòng)開(kāi)發(fā)

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

1、使用openURL來(lái)實(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)很明顯,這樣的過(guò)程會(huì)導(dǎo)致程序暫時(shí)退出,即使在iOS 4.x支持多任務(wù)的情況下,這樣的過(guò)程還是會(huì)讓人覺(jué)得不是很便。   

2、使用MFMailComposeViewController來(lái)實(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,//用戶(hù)取消編輯郵件 
 MFMailComposeResultSaved,//用戶(hù)成功保存郵件 
 MFMailComposeResultSent,//用戶(hù)點(diǎn)擊發(fā)送,將郵件放到隊(duì)列中 
 MFMailComposeResultFailed//用戶(hù)試圖保存或者發(fā)送郵件失敗 
}; 
typedef enum MFMailComposeResult MFMailComposeResult; // iOS3.0以上有效 
//在頭文件中MFMailComposeViewController的部分方法順便提及: 
 
+ (BOOL)canSendMail __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); 
//如果用戶(hù)沒(méi)有設(shè)置郵件賬戶(hù),則會(huì)返回NO,你可以用根據(jù)返回值來(lái)決定是使用MFMailComposeViewController 還是 mailto://的傳統(tǒng)方法,也或者,你可以選擇上文中提到的skpsmtpmessage來(lái)實(shí)現(xiàn)發(fā)送Email的功能。 
- (void)addAttachmentData:(NSData *)attachment mimeType:(NSString *)mimeType fileName:(NSString *)filename; 
//NSData類(lèi)型的attachment自然不必多說(shuō),關(guān)于mimeType需要一點(diǎn)說(shuō)明,官方文檔里給出了一個(gè)鏈接http://www.iana.org/assignments/media-types/ ,這里列出的所有的類(lèi)型都應(yīng)該支持。關(guān)于mimeType的用處,更多需要依靠搜索引擎了 =] 

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

3、我們可以根據(jù)自己的UI設(shè)計(jì)需求來(lái)定制相應(yīng)的視圖以適應(yīng)整體的設(shè)計(jì)??梢允褂帽容^有名的開(kāi)源SMTP協(xié)議來(lái)實(shí)現(xiàn)。   

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

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

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

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI