7

我在通过 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];
4

6 回答 6

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];
}
于 2011-02-22T11:14:28.710 回答
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];
}
于 2011-05-17T05:28:27.147 回答
1

将 mime 类型设置为“application/octet-stream”,这样就可以删除内联附件(我仍然将文件的扩展名命名为 pdf)

于 2013-10-03T16:39:33.373 回答
0

这里可能不是这种情况,但需要注意的一件事是:

[NSString dataUsingEncoding:] 

如果无法转换为指定的编码,则返回一个有效但为空的 NSData 对象。最好使用完整版:

[NSString dataUsingEncoding: s allowLossyConversion: YES]

或者检查返回数据的长度。似乎零长度的数据附件在邮件过程中的某处被修剪。

于 2011-05-07T15:54:43.647 回答
0

即使您将 isHTML 参数设置为 YES,如果邮件正文可以这样表示,您的邮件正文也可以作为纯文本/文本发送。某些电子邮件客户端 (Outlook) 并不总是能正确识别纯文本/文本消息中的附件。

在我的情况下,在消息正文中添加链接会有所帮助。使用 HTML 标签将文本格式化为粗体也可以。棘手!

在 iPod 1G 3.1.3 上测试。

于 2010-03-01T22:59:13.863 回答
0

我相信第二个参数setMessageBody:isHTML:必须是YES附件不能内联显示。

于 2009-12-21T19:16:02.500 回答