IPhone MessageUI email attachments not sent to receiver

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 be working fine, the image and the PDF are both displayed in the body of the sending mailbox as expected.

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

1) myImage.png is displayed as an attachment and is the correct size but completely empty

2) myPDF.pdf does not appear as an attachment at all

But, when I receive mail on my iPhone, myImage.png displays well. myPDF.pdf still does not appear in the mail on my iPhone.

Hope someone can shed light on what might happen.

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 extracting the image and PDF to my main unit, I used NSDocumentsDirectory and everything worked fine.

+4
source share
5 answers

I had the same problem with image attachment. Using this, I was able to send and actually receive the attached image:

 //attachment UIImage *image = [UIImage imageNamed:@"image.png"]; NSData *imageData = UIImagePNGRepresentation(image); [picker addAttachmentData:imageData mimeType:@"image/png" fileName:@"theImage"]; 

I did not try to send the pdf message, sorry.

Just my two cents

Sincerely.

+1
source

I don't know if this will actually cause your problem, but most people seem to include file name extensions in the fileName parameter for addAttachmentData, i.e.

 [picker addAttachmentData:imageData mimeType:@"image/png" fileName:@"myImage.png"]; 
0
source

I had the same problem, and I decided that MFMailComposeViewController packing the MIME for the 7-bit> PDF file, rather than citing or printing base-64 . Obviously, you cannot send binary files in 7-bit encoding. Thus, either there is a transmission encoding parameter on the MFMailComposeViewController that I am missing, or this object has a serious drawback.

0
source

Try it, this one worked for me.

 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.pdf"]; // Fill out the email body text NSString *emailBody = @"This is a test."; [picker setMessageBody:emailBody isHTML:NO]; // [self presentModalViewController:picker animated:YES]; [picker release]; 
0
source

I ran into this because I had the same problem ... small, but in case someone came across this, that’s what helped me ....

I managed to add my attachments as usual (using 6 attachments of various types) ... they will appear in the body of the message ... but the recipient will never receive all the attachments ... only one.

I pulled the files into my editor (dragged the files to m or h) and showed the path to the file. It turns out that I neglected to copy the first file to my project, so the application was pulled from my local disk and then stopped, because the other files that I tried to connect were not in the same folder. As soon as I pulled out all the attachments from the same place, they all pulled in perfectly.

Hope this helps someone.

0
source

Source: https://habr.com/ru/post/1301501/


All Articles