5

i have created one csv file and i am also attaching it to MFMailComposer and it shows me to my mail composer but when i send it to user email it does not shows me attached csv file in email. i have used this code to create csv file and adding data in it.

        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]);

here the above code is working good i am getting [mediaData length] i am attaching the CSV file from here.

    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];

the above code is also working properly. it shows me attached CSV file but when i am sending the mail by email at that time receiver is not getting the attached CSV file. what's wrong in this code.? why the receiver is not getting the attached file.?

3
  • hey anybody can help me? May 9, 2012 at 8:06
  • hey, any body please give me idea about to attach csv file in our mail composer window. May 28, 2012 at 13:06
  • picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; [picker setToRecipients:[NSArray arrayWithObjects:@"abc.xyz.com", nil]]; [picker setSubject:@"iMoneyManager - CSV Exported"]; [picker setMessageBody:@"" isHTML:NO]; [picker addAttachmentData:mediaData mimeType:@"text/csv" fileName:@"MoneyManager"]; picker.navigationBar.tintColor=[UIColor blackColor]; [self presentModalViewController:picker animated:YES];
    – K.D
    Aug 29, 2012 at 7:05

3 Answers 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];

Where the 'text' is a string.

0

i have solved this issue with attaching files and other media property in MFMailComposeViewController.

0
NSData *data=[[arr componentsJoinedByString:@","] writeToFile:@"Bhavesh.csv" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
[mail addAttachmentData:data mimeType:@"text/csv" fileName:@"Bhavesh.csv"];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.