5

我创建了一个 csv 文件,并将其附加到 MFMailComposer,它显示给我的邮件编辑器,但是当我将它发送到用户电子邮件时,它没有显示我在电子邮件中附加的 csv 文件。我已使用此代码创建 csv 文件并在其中添加数据。

        NSMutableString *mainString=[[NSMutableString alloc]initWithString:@""];
        //NSMutableArray *section = [[NSMutableArray alloc] init];
        for(int i = 0;i<[NameArray count];i++)
        {
            NSString *string=[indexArray objectAtIndex:i];
            string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""];
            [mainString appendFormat:@"\"%@\"",string];


            string=[NameArray objectAtIndex:i];  
            string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""];
            [mainString appendFormat:@",\"%@\"",string];

            string=[typearray objectAtIndex:i];
            string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""];
            [mainString appendFormat:@",\"%@\"",string];

            [mainString appendFormat:@",\"%@\"",string];
            [mainString appendFormat:@"\n"];

        }

        NSLog(@"getdatafor csv:%@",mainString);

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
        NSString *documentsDirectoryPath = [paths objectAtIndex:0];
        NSString *filePath = [documentsDirectoryPath  stringByAppendingPathComponent:@"history.csv"];
//        filePath = [filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSData* settingsData;
        settingsData = [mainString dataUsingEncoding: NSASCIIStringEncoding];

        NSError *error;
        [settingsData writeToFile:filePath atomically:YES];
//            NSLog(@"writeok"); 
        NSData *mediaData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMapped   error:&error];

        NSLog(@"Length:%d Error:%@",[mediaData length],[error localizedDescription]);

在这里,上面的代码运行良好我得到 [mediaData length] 我从这里附加 CSV 文件。

    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    // Attach an image to the email
    NSString *path = [[NSBundle mainBundle] pathForResource:@"history" ofType:@"csv"];
    NSData *myData = [NSData dataWithContentsOfFile:path];

    // Fill out the email body text
    NSString *emailBody = @"history";
    [picker setMessageBody:emailBody isHTML:NO];
    [picker addAttachmentData:myData  mimeType:@"text/cvs" fileName:@"history"];

    [self presentModalViewController:picker animated:YES];
    [picker release];

上面的代码也可以正常工作。它向我显示了附加的 CSV 文件,但是当我通过电子邮件发送邮件时,接收者没有收到附加的 CSV 文件。这段代码有什么问题。?为什么接收方没有收到附件。?

4

3 回答 3

3
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:@"CSV File"];

NSData *myData = [text dataUsingEncoding:NSUTF8StringEncoding];

[mailer addAttachmentData:myData mimeType:@"text/cvs" fileName:@"FileName"];

[self presentModalViewController:mailer animated:YES];

其中“文本”是一个字符串。

于 2013-02-26T12:55:29.053 回答
0

我已经通过在 MFMailComposeViewController 中附加文件和其他媒体属性解决了这个问题。

于 2012-12-31T12:54:36.343 回答
0
NSData *data=[[arr componentsJoinedByString:@","] writeToFile:@"Bhavesh.csv" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
[mail addAttachmentData:data mimeType:@"text/csv" fileName:@"Bhavesh.csv"];
于 2013-02-16T11:48:37.167 回答