这是我的代码:
NSArray *recipient = [NSArray arrayWithObjects:@"testemail@office.co.uk",nil];
NSString *fileName = @"SurveyResults.txt";
NSString *homeDir = NSHomeDirectory();
NSString *fullPath = [homeDir stringByAppendingPathComponent:fileName];
[super viewDidLoad];
if([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:[NSString stringWithFormat:@"Place Holder Subject"]];
[mailViewController setMessageBody:@"See Attached" isHTML:NO];
[mailViewController setToRecipients:recipient];
[mailViewController addAttachmentData:[NSData dataWithContentsOfFile:fullPath] mimeType:@"text/txt" fileName:fileName];
[self presentModalViewController:mailViewController animated:YES];
} else
NSLog(@"Device cannot send email at this time. Please check you are connected to the internet and try again");
问题是,当MailComposer启动时,它在“See attached”下有一个小的txt文件图像,下面有名称SurveyResults,但是,当我实际发送电子邮件时,txt文件没有附加,任何人都可以脱掉一盏灯?
答案 0 :(得分:0)
尝试以下方式,为我工作。问题是,填充数据的路径需要是NSURL
类型,而不仅仅是字符串路径。
NSURL *fileURL = [[NSURL alloc]initFileURLWithPath:string_path_of_the_file];
NSData *data = [[NSData alloc]initWithContentsOfURL:fileURL];
[mailViewController addAttachmentData:data mimeType:@"text/plain" fileName:fileNameString];
//mimeType needs to be changed accordingly