0

Here is my code:

NSArray *recipient = [NSArray arrayWithObjects:@"testemail@office.co.uk",nil];

NSString *fileName = @"SurveyResults.txt";
NSString *homeDir = NSHomeDirectory();
NSString *fullPath = [homeDir stringByAppendingPathComponent:fileName];

[super viewDidLoad];
if([MFMailComposeViewController canSendMail]) {
    MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
    mailViewController.mailComposeDelegate = self;
    [mailViewController setSubject:[NSString stringWithFormat:@"Place Holder Subject"]];
    [mailViewController setMessageBody:@"See Attached" isHTML:NO];
    [mailViewController setToRecipients:recipient];
    [mailViewController addAttachmentData:[NSData dataWithContentsOfFile:fullPath] mimeType:@"text/txt" fileName:fileName];

    [self presentModalViewController:mailViewController animated:YES];
} else
    NSLog(@"Device cannot send email at this time. Please check you are connected to the internet and try again");

The issue is, when the MailComposer fires up, it has a small txt file image under "See attached" with the name SurveyResults underneath, however, when i actually send the email, the txt file is not attached, can anybody shed an light?

Donal Fellows
  • 120,022
  • 18
  • 134
  • 199
Refllux
  • 25
  • 3
  • Can nobody shed any light on this? The attachment appears on the mailcomposer, just isnt arriving at its destination. Also, it appears that this code is not sucessfully filling the NSData? – Refllux Sep 10 '12 at 21:46

1 Answers1

0

Try the following, worked for me. The thing is, the path given to fill the data needs to be of the type NSURL and not just a string path.

    NSURL *fileURL = [[NSURL alloc]initFileURLWithPath:string_path_of_the_file];
    NSData *data = [[NSData alloc]initWithContentsOfURL:fileURL];

    [mailViewController addAttachmentData:data mimeType:@"text/plain" fileName:fileNameString];   
     //mimeType needs to be changed accordingly
Breakpoint
  • 1,523
  • 1
  • 18
  • 39