3

So I am using MFMailComposeViewController to attach pdf files to an email and send it out using the default iOS mail app set up with an exchange account.

If I send a file with size 4863K it works perfectly. Everything goes off as expected.

If I send a file with size 5688K it seems like its working and gets MFMailComposeResultSent as the result sent back to the delegate.

However, the mail never arrives and when I look in the mail app, I see that it is stuck in the Outbox with an error of:

The server rejected the message

What's super weird is that when I try to resent the message from the outbox, it sends out perfectly fine and gives no further error message.

I am sort of at my wits end and have not the first clue what could be going wrong. Clearly I cannot screw around with apple's code, but there don't seem to be many options with the MFMailComposeViewController that I could possibly have screwed up.

The code is fairly straightforward:

- (void) setAttachments:(NSArray*)attatchments
                ofDraft:(MFMailComposeViewController*)draft
{
  if (attatchments)
  {
    for (NSString* path in attatchments)
    {
        NSData* data = [self getDataForAttachmentPath:path];

        NSString* basename = [self getBasenameFromAttachmentPath:path];
        NSString* pathExt  = [basename pathExtension];
        NSString* fileName = [basename pathComponents].lastObject;
        NSString* mimeType = [self getMimeTypeFromFileExtension:pathExt];

        // Couldn't find mimeType, must be some type of binary data
        if (mimeType == nil) mimeType = @"application/octet-stream";

        [draft addAttachmentData:data mimeType:mimeType fileName:fileName];
    }
  }
}

The fact that this works for smaller files implies to me that there is nothing wrong with this code and it is copied out of a cordova plugin that sees pretty wide usage. But for whatever reason, files that are over ~5MB just fail for no apparent reason.

Thanks for your help.

Update I captured the http traffic and discovered that in the situation where the mail is sent using the MFMailComposeViewController, the POST request has a content-length of 0, which is not allowed, so the server returns a 400 error. When I try to resend the same message from the mail app directly, the content-length is the size expected.

This appears to be a limitation of the MessageUI framework, but I cannot verify that there is a universal limitation imposed by Apple.

Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
Jason Bray
  • 502
  • 1
  • 4
  • 15
  • 1
    It's probably an issue with your mail provider, not the code. – rmaddy Jan 07 '16 at 16:57
  • 1
    Odd that it sends fine when I try to resend it though. To your knowledge is there any difference between a mail message sent from another app and one sent directly from the iOS mail app? – Jason Bray Jan 07 '16 at 17:22
  • Normally you always send it through the iOS mail app. You will get forwarded to the app. The delegates you use are the one iOS mail app will work with. You could just print some logs and look them up inside the phones log, maybe it points you to the problem – Alex Cio Jan 07 '16 at 18:46
  • I figured out that in Xcode you can view device logs for other apps and I see the following line: 0x15669bf10|EAS|Error|ASSendMailTask failed: Error Domain=ASHTTPConnectionErrorDomain Code=400 "(null)". The internet tells me this is involved with exchange, but I have been unable to diagnose it any further. It would be nice if I could see the request itself. – Jason Bray Jan 07 '16 at 18:50
  • Having viewed the actual http request being made, it seems that when a certain size is hit, the request comes out with no body on the POST request. This appears to be an issue with the actual "send" functionality on the view itself. – Jason Bray Jan 07 '16 at 19:31
  • 1
    Did you end up finding an answer to this one? I'm having the same issue, ever since installing iOS 9.2. – Gary Thackrah Mar 05 '16 at 05:15
  • Well, I never found an actual answer as to why or whether this was going to be a permanent fix, but I ended up sending all the emails as non-html messages and this appears to have fixed the problem. – Jason Bray Mar 07 '16 at 15:14
  • Hmm, I am sending the body as non-HTML, but am still experiencing the issue. Any other things you discovered? – elsurudo Aug 09 '16 at 15:48
  • No that worked for me and I didn't look into it any further. Sorry I couldn't be of more help. – Jason Bray Aug 09 '16 at 16:17

0 Answers0