views:

675

answers:

2

I am trying to attach an image and a pdf to an email using the MessageUI framework. I followed the MailComposer example in the Apple documentation.

On the iPhone it seems to work perfectly, the image and the pdf both show up in the body of the send mail window as expected.

However, when I receive the email on my MacBook there are two problems.

1) myImage.png shows up as an attachment and is the correct dimensions but is totally blank

2) myPDF.pdf doesn't show up as an attachment at all

But, when I receive the mail on my iPhone, myImage.png shows up fine. myPDF.pdf still doesn't show up in mail on my iPhone though.

Hope someone can shed some light on what might be happening.

Here is the relevant code:

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

[picker setSubject:@"Test Email"];

 // Attach an image to the email
 NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"myImage" ofType:@"png"];
 NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
 [picker addAttachmentData:imageData mimeType:@"image/png" fileName:@"myImage"];


// Attach a PDF file to the email
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:@"myPDF" ofType:@"pdf"];
NSData *pdfData = [NSData dataWithContentsOfFile:pdfPath];
[picker addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"myPDF"];


// Fill out the email body text
NSString *emailBody = @"This is a test.";
[picker setMessageBody:emailBody isHTML:NO];

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

EDIT Instead of saving and retrieving my image and PDF to my mainBundle I used NSDocumentsDirectory and everything worked fine.

A: 

I don't know if it actually would cause your problem, but most people seem to include filename extensions on the fileName parameter to addAttachmentData, i.e.

[picker addAttachmentData:imageData mimeType:@"image/png" fileName:@"myImage.png"];
David Maymudes
Actually that is not necessary as the file type has already been specified in pathForResource:ofType: However, I did try it anyway and it made no difference.
Jason
pathForResourceOfType is about *loading* the data, whereas the fileName parameter might have affected how email clients were *interpreting* the data.... can you post a dump of the email as you're receiving it on the Mac?
David Maymudes