標籤:cti use dma smis object 關閉 www. suse sage
第一種方式:openURL
#pragma mark - 使用系統郵件用戶端發送郵件 -(void)launchMailApp { NSMutableString *mailUrl = [[[NSMutableString alloc]init]autorelease]; //添加收件者 NSArray *toRecipients = [NSArray arrayWithObject: @"[email protected]"]; [mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]]; //添加抄送 NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]; [mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]]; //添加密送 NSArray *bccRecipients = [NSArray arrayWithObjects:@"[email protected]", nil]; [mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]]; //添加主題 [mailUrl appendString:@"&subject=my email"]; //添加郵件內容 [mailUrl appendString:@"&body=<b>email</b> body!"]; NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]]; }
第二種方式:MFMailComposeViewController
讓控制器遵守<MFMailComposeViewControllerDelegate>協議
#pragma mark - 在應用內發送郵件//啟用郵件功能- (void)sendMailInApp{ Class mailClass = (NSClassFromString(@"MFMailComposeViewController")); if (!mailClass) { [WSYFunction alertViewWithTitle:@"提示" message:@"當前系統版本不支援應用內發送郵件功能,您可以使用mailto方法代替" buttonTitle:@"確定"]; return; } if (![mailClass canSendMail]) { [WSYFunction alertViewWithTitle:@"提示" message:@"使用者沒有設定郵件賬戶" buttonTitle:@"確定"]; return; } [self displayMailPicker];}//調出郵件發送視窗- (void)displayMailPicker{ MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init]; mailPicker.mailComposeDelegate = self; //設定主題 [mailPicker setSubject: [NSString stringWithFormat:@"%@",self.bookNameArray[ChooseBook]]]; //添加收件者 NSArray *toRecipients = [NSArray arrayWithObject: @""]; [mailPicker setToRecipients: toRecipients]; //添加抄送// NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];// [mailPicker setCcRecipients:ccRecipients]; //添加密送// NSArray *bccRecipients = [NSArray arrayWithObjects:@"[email protected]", nil];// [mailPicker setBccRecipients:bccRecipients]; // // 添加一張圖片 UIImage *addPic = [UIImage imageNamed: @"lanch.jpeg"]; NSData *imageData = UIImagePNGRepresentation(addPic); // png //關於mimeType:http://www.iana.org/assignments/media-types/index.html [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"lanch.jpeg"]; //構造字串檔案的儲存路徑 // 擷取Document目錄 NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *strPath = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",self.bookNameArray[ChooseBook]]]; //構造字串對象 NSString *book_str = @""; for (int i = 0; i < self.array.count; i++) { book_str = [book_str stringByAppendingString:[NSString stringWithFormat:@"\n\n%@",self.array[i]]]; } //通過將writeToFile:atomically:encoding:error:方法發送給字串對象完成字串儲存到檔案內的功能 [book_str writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; NSData *bookData = [NSData dataWithContentsOfFile:strPath]; //發送txt文本附件 [mailPicker addAttachmentData:bookData mimeType:@"text/txt" fileName:[NSString stringWithFormat:@"%@.txt",self.bookNameArray[ChooseBook]]]; NSString *emailBody = @"<font color=‘red‘>eMail</font> 本文"; [mailPicker setMessageBody:emailBody isHTML:YES]; [self presentModalViewController: mailPicker animated:YES];}#pragma mark - 實現 MFMailComposeViewControllerDelegate- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{ //關閉郵件發送視窗 [self dismissModalViewControllerAnimated:YES]; NSString *msg; switch (result) { case MFMailComposeResultCancelled: msg = @"您已取消編輯郵件"; break; case MFMailComposeResultSaved: msg = @"您已成功儲存郵件"; break; case MFMailComposeResultSent: msg = @"您點擊發送,將郵件放到隊列中,還沒發送"; break; case MFMailComposeResultFailed: msg = @"您試圖儲存或者發送郵件失敗"; break; default: msg = @""; break; } [WSYFunction alertViewWithTitle:@"" message:msg buttonTitle:@"確定"];}
最後使用直接調用方法:
[self sendMailInApp];
ios 發送郵件,簡訊