Objective-C Requires Chilkat v11.0.0+
Objective-C
Download the original Gmail message as EML
See more GMail REST API Examples
Supply a Gmail message ID and request format=raw. Decode the complete MIME message directly to bytes and save them in downloaded.eml. Parse those same bytes with Chilkat.Email to read the subject, sender, and attachment count without a text-encoding round trip.
Authorize the application with https://www.googleapis.com/auth/gmail.readonly. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
Chilkat Objective-C Downloads
#import <CkoJsonObject.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <CkoStringBuilder.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: @"raw"] intValue] != 1) || ([reply StringOfEquals: @"raw" value: @"" caseSensitive: YES] == YES)) {
NSLog(@"%@",@"Expected raw in the response.");
return;
}
CkoBinData *mime = [[CkoBinData alloc] init];
success = [reply BytesOf: @"raw" encoding: @"base64url" bd: mime];
if (success != YES) {
NSLog(@"%@",@"Could not decode the raw MIME.");
return;
}
success = [mime WriteFile: @"downloaded.eml"];
if (success != YES) {
NSLog(@"%@",@"Could not write downloaded.eml.");
return;
}
CkoEmail *email = [[CkoEmail alloc] init];
success = [email SetFromMimeBd: mime];
if (success != YES) {
NSLog(@"%@",email.LastErrorText);
return;
}
NSLog(@"%@%@",@"From: ",email.From);
NSLog(@"%@%@",@"Subject: ",email.Subject);
NSLog(@"%@%d",@"Attachments: ",[email.NumAttachments intValue]);