Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.0.0+

Reply to a message in its existing Gmail thread

See more GMail REST API Examples

Live send. Supply the Gmail ID of a received message and use an authorized From address. Fetch its raw MIME so Chilkat.Email can parse encoded headers and recipient addresses. Send a plain-text reply to Reply-To, falling back to From. The reply goes to the original sender or reply address.

The request supplies Gmail’s threadId and matching Subject. The MIME In-Reply-To contains the original RFC Message-ID. References preserves the conversation ancestry and appends that Message-ID. Select a message with a valid Message-ID and review its recipient before running this sending example. Authorize both gmail.readonly and gmail.send, or use gmail.modify as shown below.

Authorize the application with https://www.googleapis.com/auth/gmail.modify. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.

Google documentation

Chilkat Objective-C Downloads

Objective-C
#import <CkoJsonObject.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoStringBuilder.h>
#import <NSString.h>
#import <CkoBinData.h>
#import <CkoEmail.h>

BOOL success = NO;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

CkoJsonObject *token = [[CkoJsonObject alloc] init];
success = [token LoadFile: @"gmail-token.json"];
if (success != YES) {
    NSLog(@"%@",@"Could not read gmail-token.json from the private working directory.");
    return;
}

if (([[token JsonTypeOf: @"access_token"] intValue] != 1) || ([token StringOfEquals: @"access_token" value: @"" caseSensitive: YES] == YES)) {
    NSLog(@"%@",@"An OAuth access token is required.");
    return;
}

CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
http.AuthToken = [token StringOf: @"access_token"];
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
CkoJsonObject *reply = [[CkoJsonObject alloc] init];
CkoStringBuilder *url = [[CkoStringBuilder alloc] init];
[url Append: @"https://gmail.googleapis.com/gmail/v1/users/me/messages/"];
[url Append: @"GMAIL_MESSAGE_ID"];
[url Append: @"?format=raw"];
success = [http HttpNoBody: @"GET" url: [url GetAsString] response: resp];
if ([resp.StatusCode intValue] == 0) {
    NSLog(@"%@",@"Gmail did not return an HTTP response.");
    return;
}

if ((success != YES) || ([resp.StatusCode intValue] < 200) || ([resp.StatusCode intValue] >= 300)) {
    NSLog(@"%@%d",@"Gmail HTTP ",[resp.StatusCode intValue]);
    success = [reply Load: resp.BodyStr];
    if (success == YES) {
        NSLog(@"%@",[reply StringOf: @"error.message"]);
    }

    return;
}

success = [reply Load: resp.BodyStr];
if (success != YES) {
    NSLog(@"%@",@"Expected a Gmail JSON response.");
    return;
}

if (([[reply JsonTypeOf: @"threadId"] intValue] != 1) || ([reply StringOfEquals: @"threadId" value: @"" caseSensitive: YES] == YES)) {
    NSLog(@"%@",@"Expected threadId in the response.");
    return;
}

if (([[reply JsonTypeOf: @"raw"] intValue] != 1) || ([reply StringOfEquals: @"raw" value: @"" caseSensitive: YES] == YES)) {
    NSLog(@"%@",@"Expected raw in the response.");
    return;
}

NSString *threadId = [reply StringOf: @"threadId"];
CkoBinData *originalMime = [[CkoBinData alloc] init];
success = [reply BytesOf: @"raw" encoding: @"base64url" bd: originalMime];
if (success != YES) {
    NSLog(@"%@",reply.LastErrorText);
    return;
}

CkoEmail *original = [[CkoEmail alloc] init];
success = [original SetFromMimeBd: originalMime];
if (success != YES) {
    NSLog(@"%@",original.LastErrorText);
    return;
}

NSString *messageIdHeader = [original GetHeaderField: @"Message-ID"];
if ((original.LastMethodSuccess != YES) || (messageIdHeader == @"")) {
    NSLog(@"%@",@"The original message needs a Message-ID for this threaded reply.");
    return;
}

CkoEmail *email = [[CkoEmail alloc] init];
email.From = @"Sender <sender@example.com>";
//  Use the parsed Reply-To mailbox list when present, otherwise use From.
NSString *replyAddress = original.ReplyTo;
if (replyAddress == @"") {
    replyAddress = original.From;
}

success = [email AddMultipleTo: replyAddress];
if (success != YES) {
    NSLog(@"%@",email.LastErrorText);
    return;
}

if ([email.NumTo intValue] == 0) {
    NSLog(@"%@",@"The original message has no usable reply address.");
    return;
}

email.Subject = original.Subject;
email.Charset = @"utf-8";
email.Body = @"Thank you for your message.";
[email AddHeaderField: @"In-Reply-To" fieldValue: messageIdHeader];
CkoStringBuilder *references = [[CkoStringBuilder alloc] init];
NSString *previousReferences = [original GetHeaderField: @"References"];
if ((original.LastMethodSuccess == YES) && (previousReferences != @"")) {
    [references Append: previousReferences];
    [references Append: @" "];
}

[references Append: messageIdHeader];
[email AddHeaderField: @"References" fieldValue: [references GetAsString]];
CkoBinData *mime = [[CkoBinData alloc] init];
success = [email GetMimeBd: mime];
if (success != YES) {
    NSLog(@"%@",email.LastErrorText);
    return;
}

CkoJsonObject *request = [[CkoJsonObject alloc] init];
success = [request UpdateBd: @"raw" encoding: @"base64url" bd: mime];
if (success != YES) {
    NSLog(@"%@",request.LastErrorText);
    return;
}

success = [request UpdateString: @"threadId" value: threadId];
if (success != YES) {
    NSLog(@"%@",request.LastErrorText);
    return;
}

success = [http HttpJson: @"POST" url: @"https://gmail.googleapis.com/gmail/v1/users/me/messages/send" json: request contentType: @"application/json" response: resp];
if ([resp.StatusCode intValue] == 0) {
    NSLog(@"%@",@"Gmail did not return an HTTP response.");
    return;
}

if ((success != YES) || ([resp.StatusCode intValue] < 200) || ([resp.StatusCode intValue] >= 300)) {
    NSLog(@"%@%d",@"Gmail HTTP ",[resp.StatusCode intValue]);
    success = [reply Load: resp.BodyStr];
    if (success == YES) {
        NSLog(@"%@",[reply StringOf: @"error.message"]);
    }

    return;
}

success = [reply Load: resp.BodyStr];
if (success != YES) {
    NSLog(@"%@",@"Expected a Gmail JSON response.");
    return;
}

if (([[reply JsonTypeOf: @"id"] intValue] != 1) || ([reply StringOfEquals: @"id" value: @"" caseSensitive: YES] == YES)) {
    NSLog(@"%@",@"Expected id in the response.");
    return;
}

NSLog(@"%@%@",@"Sent reply ID: ",[reply StringOf: @"id"]);
NSLog(@"%@%@",@"Thread ID: ",[reply StringOf: @"threadId"]);