-
Notifications
You must be signed in to change notification settings - Fork 631
SMTP Examples
In order to send a message using MailCore 2, we first have to compose that message using MCOMessageBuilder
, then we can hand it off to an asynchronous operation generated by out SMTP session to deliver that message to the SMTP server. Here's a quick example, assuming you have arrays of email addresses for to/cc/bcc:
MCOSMTPSession *smtpSession = [[MCOSMTPSession alloc] init];
smtpSession.hostname = @"smtp.gmail.com";
smtpSession.port = 465;
smtpSession.username = USERNAME;
smtpSession.password = PASSWORD;
smtpSession.connectionType = MCOConnectionTypeTLS;
MCOMessageBuilder * builder = [[MCOMessageBuilder alloc] init];
[[builder header] setFrom:[MCOAddress addressWithDisplayName:nil mailbox:USERNAME]];
NSMutableArray *to = [[NSMutableArray alloc] init];
for(NSString *toAddress in RECIPIENTS) {
MCOAddress *newAddress = [MCOAddress addressWithMailbox:toAddress];
[to addObject:newAddress];
}
[[builder header] setTo:to];
NSMutableArray *cc = [[NSMutableArray alloc] init];
for(NSString *ccAddress in CC) {
MCOAddress *newAddress = [MCOAddress addressWithMailbox:ccAddress];
[cc addObject:newAddress];
}
[[builder header] setCc:cc];
NSMutableArray *bcc = [[NSMutableArray alloc] init];
for(NSString *bccAddress in BCC) {
MCOAddress *newAddress = [MCOAddress addressWithMailbox:bccAddress];
[bcc addObject:newAddress];
}
[[builder header] setBcc:bcc];
[[builder header] setSubject:SUBJECT];
[builder setHTMLBody:BODY];
NSData * rfc822Data = [builder data];
MCOSMTPSendOperation *sendOperation = [smtpSession sendOperationWithData:rfc822Data];
[sendOperation start:^(NSError *error) {
if(error) {
NSLog(@"%@ Error sending email:%@", USERNAME, error);
} else {
NSLog(@"%@ Successfully sent email!", USERNAME);
}
}];
// if you're running in a command line tool.
// [[NSRunLoop currentRunLoop] run];
So as you can see, we compose the message, then put our callback code inside of the start
block.
What remains missing in the above example so far is how to add attachments to the email.
Assuming we have a simple text to add which is stored in textAttachment it can be as simple as:
textAttachment = @"Some text which should get sent as attachment";
MCOAttachment *attachment = [MCOAttachment attachmentWithText:textAttachment];
[builder addAttachment:attachment];
First the attachment is created and then added.
While for a simple text this is very straight forward, it gets slightly more complicated in case an image should get attached. In the next code snippet we assume that an image is already present as base64 encoded text which starts with 'data:image/jpeg;base64,...'.
Now one can't simply add this as text it would just attach it as if it was just text. In order to attach this kind of image to the email it needs to get wrapped into a simple HTML element such as:
<img src='data:image/jpeg;base64,...' />
This element can then be added easily using attachmentWithHTMLString like this:
imageAttachment = @"<img src='data:image/jpeg;base64,...' />";
MCOAttachment *attachment = [MCOAttachment attachmentWithHTMLString:imageAttachment];
[builder addAttachment:attachment];
Similar to sending an email to more than one recipients, the above code can also easily get enhanced to send more than one attachments.