Sample code for 30+ languages & platforms
Objective-C

Send Pre-Built MIME via SMTP

See more SMTP Examples

Demonstrates the Chilkat MailMan.SendMime method, which sends an email from caller-supplied MIME text. The arguments are the from address, the recipient list (the SMTP envelope recipients), and the MIME source. This example builds an email, gets its MIME, and sends that MIME.

Important: When sending caller-supplied MIME, ensure the Message-ID header is unique for each message. Sending MIME whose Message-ID was previously used can cause the message to be silently discarded as a duplicate by mail servers — see chilkatsoft.com/email_duplicate_message_id.asp.

Background: Sometimes you already have a complete message as raw MIME — loaded from a .eml file, generated elsewhere, or archived earlier — and just need to deliver it as-is. SendMime hands that MIME straight to the SMTP server. Note the distinction between the envelope recipients (the second argument, who the server actually delivers to) and the To/Cc headers inside the MIME (what the recipient sees) — they can legitimately differ, which is how Bcc works.

Chilkat Objective-C Downloads

Objective-C
#import <CkoMailMan.h>
#import <CkoEmail.h>
#import <NSString.h>

BOOL success = NO;

//  Demonstrates the MailMan.SendMime method, which sends an email from caller-supplied MIME
//  text.  The arguments are the from address, the recipient list (the SMTP envelope
//  recipients), and the MIME source.

CkoMailMan *mailman = [[CkoMailMan alloc] init];

//  Configure the SMTP server connection.
mailman.SmtpHost = @"smtp.example.com";
mailman.SmtpPort = [NSNumber numberWithInt:465];
mailman.SmtpSsl = YES;
mailman.SmtpUsername = @"user@example.com";
mailman.SmtpPassword = @"myPassword";

//  Obtain the MIME to send.  Here we build an Email and get its MIME, but the MIME could
//  come from any source.
CkoEmail *email = [[CkoEmail alloc] init];
email.Subject = @"Test email";
email.From = @"alice@example.com";
[email AddTo: @"Bob" emailAddress: @"bob@example.com"];
email.Body = @"Hello, this message was sent as MIME.";
NSString *mimeText = [email GetMime];

//  IMPORTANT: When sending caller-supplied MIME, make sure the Message-ID header is unique
//  for each message.  Sending MIME that contains a Message-ID that was previously sent can
//  cause the message to be silently discarded as a duplicate by mail servers.  For details,
//  see: https://www.chilkatsoft.com/email_duplicate_message_id.asp

//  Send the MIME to the envelope recipient(s).

success = [mailman SendMime: @"alice@example.com" recipients: @"bob@example.com" mimeMsg: mimeText];
if (success == NO) {
    NSLog(@"%@",mailman.LastErrorText);
    return;
}

NSLog(@"%@",@"MIME email sent.");

//  Note: Explicitly connecting/authenticating is optional.  Chilkat MailMan automatically
//  connects and authenticates -- using the property settings above -- whenever a server
//  operation requires it.  Calling the explicit connect/authenticate methods can still be
//  helpful to determine whether a failure occurs while connecting or while authenticating.