本文介绍了将.txt附加到MFMailComposeViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个存储在Documents Folder中的.txt文件,我希望通过MFMailComposeViewController将其发送给 -sendEmail
方法体中的下一个代码:
I have a .txt file stored in Documents Folder and I want to send it by MFMailComposeViewController with next code in the body of -sendEmail
method:
NSData *txtData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dataBase" ofType:@"txt"]];
[mail addAttachmentData:txtData mimeType:@"text/plain" fileName:[NSString stringWithFormat:@"dataBase.txt"]];
当邮件编辑器出现时,我可以在邮件正文中看到附件,但是我收到这封邮件而没有附件。也许它是.txt附件的MIME类型错误或此代码有问题?
When Mail composer appears I can see attachment in the mail body but I receive this mail without attachment. Maybe it is wrong MIME-type for .txt attachment or something wrong with this code?
谢谢
推荐答案
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *txtFilePath = [documentsDirectory stringByAppendingPathComponent:@"abc.txt"];
NSData *noteData = [NSData dataWithContentsOfFile:txtFilePath];
MFMailComposeViewController *_mailController = [[MFMailComposeViewController alloc] init];
[_mailController setSubject:[NSString stringWithFormat:@"ABC"]];
[_mailController setMessageBody:_messageBody
isHTML:NO];
[_mailController setMailComposeDelegate:self];
[_mailController addAttachmentData:noteData mimeType:@"text/plain" fileName:@"abc.txt"];
希望有所帮助。
这篇关于将.txt附加到MFMailComposeViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!