Skip to content

Instantly share code, notes, and snippets.

@ksksue
Created January 22, 2015 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksksue/7321100b94dee083e1f5 to your computer and use it in GitHub Desktop.
Save ksksue/7321100b94dee083e1f5 to your computer and use it in GitHub Desktop.
#include <MailCore/MailCore.h>
...
-(void) sendEmail:(NSString *) message
{
MCOSMTPSession *smtpSession = [[MCOSMTPSession alloc] init];
smtpSession.hostname = @"smtp.gmail.com"; // SMTPサーバのアドレス
smtpSession.port = 465;
smtpSession.username = @"moemoe"; // SMTPサーバのユーザ名
smtpSession.password = @"tsun dere"; // SMTPサーバのパスワード
smtpSession.authType = MCOAuthTypeSASLPlain;
smtpSession.connectionType = MCOConnectionTypeTLS;
MCOMessageBuilder *builder = [[MCOMessageBuilder alloc] init];
MCOAddress *from = [MCOAddress addressWithDisplayName:nil
mailbox:@"from@mail.com"]; // 送信元メールアドレス
MCOAddress *to = [MCOAddress addressWithDisplayName:nil
mailbox:@"to@mail.com"]; // 送信先メールアドレス
[[builder header] setFrom:from];
[[builder header] setTo:@[to]];
[[builder header] setSubject:@"ここにサブジェクト"];
[builder setHTMLBody:message];
NSData * rfc822Data = [builder data];
MCOSMTPSendOperation *sendOperation =
[smtpSession sendOperationWithData:rfc822Data];
[sendOperation start:^(NSError *error) {
if(error) {
NSLog(@"Error sending email: %@", error);
} else {
NSLog(@"Successfully sent email!");
}
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment