LoginSignup
3
3

More than 5 years have passed since last update.

【Objective-C】mailCore2をCocoaPodsで導入

Last updated at Posted at 2016-07-13

iOSアプリでメール送信機能をつけたい場合にはCocoaPodsで公開されているmailCore2を利用するのが便利。ネイティブアプリからメーラーの機能を使ってメールの送受信ができる。今回は送信機能を実装した際の自分用メモ。

前提条件

  • CocoaPodを導入済みであること

導入方法

  • Podfileに以下を記述
pod 'mailcore2-ios', '~> 0.6'
  • ターミナルで以下を実行
pod update


---
Update all pods
For more information see http://blog.cocoapods.org
and the CHANGELOG for this version http://git.io/BaH8pQ.

Analyzing dependencies
Downloading dependencies

Installing mailcore2-ios (0.6.4)
Generating Pods project
Integrating client project

Pod installation complete! There are 11 dependencies from the Podfile and 13 total pods installed.
  • 実装内容
sendMail.m
- (void)sendMailBySample {

    MCOSMTPSession *smtpSession     = [[MCOSMTPSession alloc] init];
    smtpSession.hostname            = HOST_NAME;   // SMTPサーバのアドレス
    smtpSession.port                = SMTP_PORT;   // ポート番号
    smtpSession.username            = USER_NAME;   // メールサーバのユーザ名
    smtpSession.password            = PASSWORD;    // メールサーバのパスワード
    smtpSession.authType            = MCOAuthTypeSASLPlain;
    smtpSession.connectionType      = MCOConnectionTypeStartTLS;

    [smtpSession setCheckCertificateEnabled:NO];

    MCOMessageBuilder *builder      = [[MCOMessageBuilder alloc] init];

    // 送信元メールアドレス
    MCOAddress *sourceAddress       = [MCOAddress addressWithDisplayName:DISPLAY_NAME
                                                                 mailbox:SOURCE_ADDRESS];
    // 送信先メールアドレス
    MCOAddress *destinationAddress  = [MCOAddress addressWithDisplayName:nil
                                                                 mailbox:DESTINATION_ADDRESS];

    [[builder header] setFrom:sourceAddress];
    [[builder header] setTo:@[destinationAddress]];
    [[builder header] setSubject:[NSString stringWithFormat:@"Subject"]];


    [builder setHTMLBody:Body];

    NSData *messageBody = [builder data];

    MCOSMTPSendOperation *sendOperation = [smtpSession sendOperationWithData:messageBody];

    [sendOperation start:^(NSError *error) {
        if(error) {
            [self popAlertWithMessage:@"送信結果" message:@"送信に失敗しました"];
        } else {
            [self popAlertWithMessage:@"送信結果" message:@"送信に成功しました"];
        }
    }];
}

所感

githubからmailcore2をcloneしてソースを取り込み自前実装する方法もあったが、CocoaPodsを利用したほうが楽だったことに気づいた。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3