GTLRGmailService failed execute query when sending email with attachment (6MB) api gmail/v1/users/me/messages/send

Skip to first unread message

mihir mehta

Apr 5, 2019, 3:00:25 PM4/5/19
to Google APIs Client Library for Objective-C
Hi There,

I am trying to send email within my iOS app, The code works fine as long as attachment is small. But if i try to send large attachment (> 6MB) the query instantly fails. 

Here is my code

        let group = DispatchGroup()

        let message = GTLRGmail_Message()

        self.gmailService = GTLRGmailService()

        let rawMessage:String = generateRawString() // Raw Message in Base64 String

        message.raw = rawMessage

        let query: GTLRGmailQuery = GTLRGmailQuery_UsersMessagesSend.query(withObject: message, userId: BSMConstants.BSMGmailAPIUserIdME, uploadParameters: nil)

        var gmailMessage: GTLRGmail_Message?



        _ = gmailService?.executeQuery(query, completionHandler: { (_, message, error) in

            if error == nil && message is GTLRGmail_Message {

                gmailMessage = message as? GTLRGmail_Message






        return gmailMessage


Please note that the above code works just fine when attachment size is small .... 

Here is the raw email string in plain text (Email addresses are valid but i have hide it.)

Date: Thu, 04 Apr 2019 11:45:06 +0530

From: Mihir Mehta99 <>

To: mihir mehta <>

Subject: With attachment

Content-Type: multipart/mixed; boundary="dejhLO8fc9OT6X"


Content-Type: multipart/alternative; boundary="CmIHym3S6rVtvf"


Content-Type: text/plain; charset="UTF-8"


Sent from Blueskyme App


Content-Type: text/html; charset="UTF-8"

Content-Transfer-Encoding: base64




Content-Type: image/png; name="3CDE712B-71F3-436D-9C34-E7100A7987F4.png"

Content-Disposition: attachment; filename="3CDE712B-71F3-436D-9C34-E7100A7987F4.png"

Content-Transfer-Encoding: base64

///////////////////// BASE64 STRING OF ATTACHMENT GOES HERE (SIZE AROUND 9 MB) //////////////////////////////


the completion block gives me following error


    NSErrorFailingURLKey = "";

    NSErrorFailingURLStringKey = "";

    NSLocalizedDescription = "unknown error";

    NSUnderlyingError = "Error Domain=kCFErrorDomainCFNetwork Code=-998 \"(null)\" UserInfo={NSErrorPeerAddressKey=<CFData 0x600002bbcff0 [0x110c19b68]>{length = 16, capacity = 16, bytes = 0x100201bbacd91bca0000000000000000}, _kCFStreamErrorCodeKey=0, _kCFStreamErrorDomainKey=2}";

    "_NSURLErrorFailingURLSessionTaskErrorKey" = "LocalDataTask <400B1836-8DDB-43D3-96FB-C110931F71CF>.<1>";

    "_NSURLErrorRelatedURLSessionTaskErrorKey" =     (

        "LocalDataTask <400B1836-8DDB-43D3-96FB-C110931F71CF>.<1>"


    "_kCFStreamErrorCodeKey" = 0;

    "_kCFStreamErrorDomainKey" = 2;


It fails immediately  

Please note that device is properly connected to internet and has no any network issue.... as well as other functionality of the library works just fine. It only fails when attachment is bit large, works fine otherwise

Do i need to do something differently when attachment size is bit large ? 

Any help would be appreciated


mihir mehta

Apr 9, 2019, 2:40:52 PM4/9/19
to Google APIs Client Library for Objective-C
From library i am getting following NSURLResponse 

<NSHTTPURLResponse: 0x600000da2e20> { URL: } { Status Code: 413, Headers {
    "Cache-Control" =     (
        "private, max-age=0"
    "Content-Encoding" =     (
    "Content-Length" =     (
    "Content-Type" =     (
        "text/html; charset=UTF-8"
    Date =     (
        "Thu, 04 Apr 2019 06:55:24 GMT"
    Expires =     (
        "Thu, 04 Apr 2019 06:55:24 GMT"
    Server =     (
    "alt-svc" =     (
        "quic=\":443\"; ma=2592000; v=\"46,44,43,39\""
    "x-content-type-options" =     (
    "x-frame-options" =     (
    "x-xss-protection" =     (
        "1; mode=block"
} }

In Function 

- (void)URLSession:(NSURLSession *)session
:(NSURLSessionDataTask *)dataTask
:(NSURLResponse *)response
:(void (^)(NSURLSessionResponseDisposition))handler {

From  GTMSessionFetcherService.m

Thomas Van Lenten

Apr 15, 2019, 3:17:47 PM4/15/19
to Google APIs Client Library for Objective-C
Using a dispatch group like that likely isn't going to work since I believe NSURLSession likely wants working runloops, queues not blocked, etc.  You'd be better of using real async code and getting called back later when things finish without blocking the starting queue/thread.


mihir mehta

Apr 16, 2019, 7:33:42 AM4/16/19
to Google APIs Client Library for Objective-C
Thank you Thomas, 

I already try to run the same code without using dispatch group at all. It doesn't change the outcome. And the same code works fine when attachment is small (< 5~6 MB). 
Reply all
Reply to author
0 new messages