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

Send email to blind-copy recipients with Gmail

See more GMail REST API Examples

Live send. Replace all sample addresses. Gmail takes its recipients from the submitted MIME To, Cc, and Bcc headers. Use AddBcc on a new Email and keep the Bcc header when serializing it for Gmail. Treat the submission as private because it contains the blind-copy addresses.

Authorize the application with https://www.googleapis.com/auth/gmail.send. 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 <CkoEmail.h>
#import <CkoBinData.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];
CkoEmail *email = [[CkoEmail alloc] init];
//  Use the authorized mailbox or one of its configured send-as addresses.
email.From = @"Sender <sender@example.com>";
success = [email AddTo: @"Recipient" emailAddress: @"recipient@example.net"];
if (success != YES) {
    NSLog(@"%@",email.LastErrorText);
    return;
}

email.Subject = @"Sample message";
email.Charset = @"utf-8";
email.Body = @"Hello from the Gmail API.";
//  Gmail needs the blind recipient in the submitted MIME; use the normal Chilkat 11.x Bcc behavior.
success = [email AddBcc: @"Blind recipient" emailAddress: @"blind@example.org"];
if (success != YES) {
    NSLog(@"%@",email.LastErrorText);
    return;
}

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 = [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(@"%@%@",@"Gmail message ID: ",[reply StringOf: @"id"]);