IOS 開發(fā)中發(fā)送e-mail的幾種方法總結(jié)
iOS系統(tǒng)框架提供的兩種發(fā)送Email的方法
1、使用openURL來實現(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]];
缺點很明顯,這樣的過程會導(dǎo)致程序暫時退出,即使在iOS 4.x支持多任務(wù)的情況下,這樣的過程還是會讓人覺得不是很便。
2、使用MFMailComposeViewController來實現(xiàn)發(fā)郵件的功能,它在MessageUI.framework中,你需要在項目中加入該框架,并在使用的文件中導(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];
//使用該方法實現(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,//用戶點擊發(fā)送,將郵件放到隊列中
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來實現(xiàn)發(fā)送Email的功能。
- (void)addAttachmentData:(NSData *)attachment mimeType:(NSString *)mimeType fileName:(NSString *)filename;
//NSData類型的attachment自然不必多說,關(guān)于mimeType需要一點說明,官方文檔里給出了一個鏈接http://www.iana.org/assignments/media-types/ ,這里列出的所有的類型都應(yīng)該支持。關(guān)于mimeType的用處,更多需要依靠搜索引擎了 =]
第二種方法的劣勢也很明顯,iOS系統(tǒng)替我們提供了一個mail中的UI,而我們卻完全無法對齊進行訂制,這會讓那些定制化成自己風(fēng)格的App望而卻步,因為這樣使用的話無疑太突兀了。
3、我們可以根據(jù)自己的UI設(shè)計需求來定制相應(yīng)的視圖以適應(yīng)整體的設(shè)計??梢允褂帽容^有名的開源SMTP協(xié)議來實現(xiàn)。
在SKPSMTPMessage類中,并沒有對視圖進行任何的要求,它提供的都是數(shù)據(jù)層級的處理,你之需要定義好相應(yīng)的發(fā)送要求就可以實現(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使用NSURLConnection實現(xiàn)斷點續(xù)傳下載
這篇文章主要為大家詳細介紹了iOS使用NSURLConnection實現(xiàn)斷點續(xù)傳下載,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
iOS中利用CoreAnimation實現(xiàn)一個時間的進度條效果
在iOS中實現(xiàn)進度條通常都是通過不停的設(shè)置progress來完成的,這樣的進度條適用于網(wǎng)絡(luò)加載(上傳下載文件、圖片等)。下面通過本文給大家介紹iOS中利用CoreAnimation實現(xiàn)一個時間的進度條,需要的的朋友參考下吧2017-09-09
iOS中關(guān)于音樂鎖屏控制音樂(鎖屏信息設(shè)置)的實例代碼
這篇文章主要介紹了 iOS中關(guān)于音樂鎖屏控制音樂(鎖屏信息設(shè)置)的實例代碼,需要的朋友可以參考下2017-01-01
iOS開發(fā)之topLayoutGuide和bottomLayoutGuide的使用小技巧分享
這篇文章主要給大家介紹了關(guān)于iOS開發(fā)之topLayoutGuide和bottomLayoutGuide使用的一些小技巧,需要的朋友可以參考下2017-11-11
iOS開發(fā)技能weak和strong修飾符的規(guī)范使用詳解
這篇文章主要為大家介紹了iOS開發(fā)技能weak和strong修飾符的規(guī)范使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07

