我遇到通过MFMailComposeViewController发送csv附件的问题。 有时候它们会很好地通过,但对于其他用户来说,它们并不是作为附件而来的,而是作为电子邮件中的文本内联(使用< br />而不是行返回)。这很奇怪。谁知道我做错了什么? 以下是我的代码片段:
MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
mailComposeViewController.mailComposeDelegate = self;
NSString *csv = @"foo,bar,blah,hello";
NSData *csvData = [csv dataUsingEncoding:NSUTF8StringEncoding];
[mailComposeViewController addAttachmentData:csvData mimeType:@"text/csv" fileName:@"testing.csv"];
[mailComposeViewController setSubject:@"testing sending csv attachment"];
[mailComposeViewController setMessageBody:@"csv file should be attached" isHTML:NO];
[self presentModalViewController:mailComposeViewController animated:YES];
答案 0 :(得分:10)
-(IBAction)btnPressed:(id)sender {
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDir = [arrayPaths objectAtIndex:0];
NSString *Path = [docDir stringByAppendingString:@"/CSVFile.csv"];
NSData *csvData = [NSData dataWithContentsOfFile:Path];
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:@"For csv file..."];
[controller setMessageBody:@"...csv file is hear.." isHTML:NO];
[controller addAttachmentData:csvData mimeType:@"text/csv" fileName:@"CSVFile.csv"];
[self presentModalViewController:controller animated:YES];
[controller release];
}
答案 1 :(得分:2)
您好我为创建CSV文件添加了示例代码并将其附加到邮件中,但请确保您必须添加MessageUI.Framework并导入其相关标题“MessageUI / MessageUI.h” “MessageUI / MFMailComposeViewController.h”和deligate“MFMailComposeViewControllerDelegate”...我希望这对其他人有用
- (void)viewDidLoad {
arrCsv=[[NSArray alloc]initWithObjects:@"Hello",@"Hi",@"traun",@"fine",nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"%@/try.csv", documentsDirectory];
[[arrCsv componentsJoinedByString:@","] writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:NULL];
}
-(ibAction)btnMail {
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDir = [arrayPaths objectAtIndex:0];
NSString *Path = [docDir stringByAppendingString:@"/CSVFile.csv"];
NSData *csvData = [NSData dataWithContentsOfFile:Path];
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:@"For csv file..."];
[controller setMessageBody:@"...csv file is hear.." isHTML:NO];
[controller addAttachmentData:csvData mimeType:@"text/csv" fileName:@"CSVFile.csv"];
[self presentModalViewController:controller animated:YES];
[controller release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{ message.hidden = NO;
switch (result)
{
case MFMailComposeResultCancelled:
message.text = @"Result: canceled";
break;
case MFMailComposeResultSaved:
message.text = @"Result: saved";
break;
case MFMailComposeResultSent:
message.text = @"Result: sent";
break;
case MFMailComposeResultFailed:
message.text = @"Result: failed";
break;
default:
message.text = @"Result: not sent";
break;
}
[self dismissModalViewControllerAnimated:YES];
}
答案 2 :(得分:1)
将mime类型设置为“application / octet-stream”,这应该可以删除内联附件(我仍然将文件的扩展命名为pdf)
答案 3 :(得分:0)
我认为setMessageBody:isHTML:
的第二个参数必须为YES
,以便附件不显示内联。
答案 4 :(得分:0)
即使您将isHTML参数设置为YES,如果可以像这样表示消息正文,也可以将您的消息正文作为普通/文本发送。某些电子邮件客户端(Outlook)无法始终正确识别普通邮件中的附件。
在我的案例中,在邮件正文中添加了一个链接。使用HTML标记将文本格式设置为粗体也可以。棘手!
在iPod 1G 3.1.3上测试过。
答案 5 :(得分:0)
这可能不是这种情况,但需要注意的一点是:
[NSString dataUsingEncoding:]
如果无法转换为指定的编码,则返回有效但空的NSData对象。最好使用完整版:
[NSString dataUsingEncoding: s allowLossyConversion: YES]
或检查返回数据的长度。看来零邮件数据附件在邮件进程的某处被修剪。