Skip to content

Instantly share code, notes, and snippets.

@glennposadas
Last active March 4, 2021 00:10
Show Gist options
  • Save glennposadas/346fcfcb0b06d9a97d0e668002c9e51f to your computer and use it in GitHub Desktop.
Save glennposadas/346fcfcb0b06d9a97d0e668002c9e51f to your computer and use it in GitHub Desktop.
Sample full code of sending an email using MailCore2 in Objective-C
- (IBAction)sendEmail:(id)sender {
MCOMessageBuilder *builder = [[MCOMessageBuilder alloc] init];
MCOAddress *address = [MCOAddress addressWithDisplayName:@"Uplant Support" mailbox:@"noreply.uplant@gmail.com"];
NSArray *to = [NSArray arrayWithObject:[MCOAddress addressWithDisplayName:@"Glenn Posadas" mailbox:@"admin@glennvon.com"]];
[[builder header] setFrom:address];
[[builder header] setTo:to];
[[builder header] setSubject:@"This is a nice subject"];
[builder setHTMLBody:@"The motherloving body."];
NSData * data = [builder data];
MCOSMTPSession *smtpSession = [[MCOSMTPSession alloc] init];
[smtpSession setHostname:@"smtp.gmail.com"];
[smtpSession setUsername:@"noreply.uplant@gmail.com"];
[smtpSession setPassword:@"----"];
[smtpSession setPort:465];
[smtpSession setConnectionType:MCOConnectionTypeTLS];
MCOSMTPSendOperation *smtpSender = [smtpSession sendOperationWithData: data];
[smtpSender start:^(NSError * _Nullable error) {
NSLog(@"Error: %@", error.localizedDescription);
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment