Objective-C Requires Chilkat v11.0.0+
Objective-C
Add an attachment and an inline image
See more Email Object Examples
Add a file attachment from memory and embed an image in the HTML body using its Content-ID. Start with any email that has an HTML body, or use text-and-html.eml from the preceding example. Save the result as with-attachments.eml.
Chilkat Objective-C Downloads
#import <CkoEmail.h>
#import <CkoBinData.h>
#import <NSString.h>
#import <CkoStringBuilder.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkoEmail *email = [[CkoEmail alloc] init];
success = [email LoadEml: @"text-and-html.eml"];
if (success != YES) {
NSLog(@"%@",email.LastErrorText);
return;
}
CkoBinData *data = [[CkoBinData alloc] init];
success = [data AppendString: @"Time,Activity\r\n09:00,Welcome\r\n10:00,Field session\r\n" charset: @"utf-8"];
if (success != YES) {
NSLog(@"%@",@"Failed: data.AppendString.");
return;
}
success = [email AddAttachmentBd: @"agenda.csv" binData: data contentType: @"text/csv"];
if (success != YES) {
NSLog(@"%@",email.LastErrorText);
return;
}
// AddRelatedBd returns the identifier that must appear after cid: in the HTML.
CkoBinData *image = [[CkoBinData alloc] init];
success = [image AppendEncoded: @"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAASUlEQVR4nGMQK/ImBv3//x/CYCBSNQkaIKqJ1YCmGkUDsigu1egakOWwqsaiAaICl2p0P8DV4VKNxdP4VWMPJTyqiY04sZGuAQBhyhp6A/ap2AAAAABJRU5ErkJggg==" encoding: @"base64"];
if (success != YES) {
NSLog(@"%@",@"Failed: image.AppendEncoded.");
return;
}
NSString *cid = [email AddRelatedBd: @"confirmed.png" binData: image];
if (email.LastMethodSuccess != YES) {
NSLog(@"%@",email.LastErrorText);
return;
}
CkoStringBuilder *html = [[CkoStringBuilder alloc] init];
[html Append: @"<p><img alt='Confirmed' src=\"cid:"];
[html Append: cid];
[html Append: @"\"></p>"];
[html Append: [email GetHtmlBody]];
success = [email AddHtmlAlternativeBody: [html GetAsString]];
if (success != YES) {
NSLog(@"%@",email.LastErrorText);
return;
}
success = [email SaveEml: @"with-attachments.eml"];
if (success != YES) {
NSLog(@"%@",email.LastErrorText);
return;
}
NSLog(@"%@",@"Saved with-attachments.eml.");