MFMailCompose-附件似乎可以工作..没有任何东西到达另一端



这是我的代码:

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启动时,它在"查看附件"下有一个小的txt文件图像,下面的名称是SurveyResults,然而,当我实际发送电子邮件时,txt文件没有附件,有人能解释吗?

试试下面的方法,对我来说很有效。问题是,填充数据的路径必须是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

相关内容

最新更新