0

I am trying to generate a text file and attach it to email. Previously I used this code to attach the file to email and it is done successfully.

 NSString *recipient = @"myemail@me.com";
    NSArray *recipients = [NSArray arrayWithObjects:recipient, nil];

    MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
    mailViewController.mailComposeDelegate = self;
    [mailViewController setSubject:@"Invalid Scan"];
    [mailViewController setToRecipients:recipients];
    [mailViewController setMessageBody:@"" isHTML:NO];
    mailViewController.navigationBar.tintColor = [UIColor blackColor];

    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *textFilePath = [documentsDirectory stringByAppendingFormat:@"/textfile.txt"];
    NSData *myData = [NSData dataWithContentsOfFile:textFilePath];

  //  NSLog(@"my nsdata is %@",myData);

    [mailViewController addAttachmentData:myData
                                 mimeType:@"text/csv"
                                 fileName:textfile.txt];

But when I tried to use this statement below ,the file didn't get attached :

     NSString *fileName_txtFile= @"textfile.txt";
NSString *textFilePath = [documentsDirectory stringByAppendingFormat:[NSString stringWithFormat:@"/%@",fileName_txtFile],nil];
    NSData *myData = [NSData dataWithContentsOfFile:textFilePath];



    [mailViewController addAttachmentData:myData
                                 mimeType:@"text/csv"
                                 fileName:fileName_txtFile];

I couldn't figure out, why the file is not attaching to the mail. Could you please help me in this issue?

Thanks in advance!!

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109

0 Answers0