> IOS开发在线手册 > iOS开发-邮件发送

Web开发的时候邮箱注册登录是必不可少的,手机号可以更换,不过相对而言,邮箱只是用于比较重要的时候用到,比如找工作的时候必填的邮箱,注册网站会员的邮箱验证。现在的手机和Web的其实操作是一样的,大多数情况还是邮件的发送,iOS上有两种邮件发送的方式,一种是使用内置的MFMailComposeViewController发送邮件,第二种就是通过第三方类库SKPSMTPMessage发送邮件(Https://github.com/jetseven/skpsmtpmessage).

MFMailComposeViewController发送

iOS3.0之后你能通过内置的MFMailComposeViewController构成一个邮件发送接口,不过模拟器上不支持,而且默认的界面不太符合中国人邮件发送的习惯,但是iOS既然有了,还是简单看下效果:

首先导入MessageUI(#import <MessageUI/MessageUI.h>),实现MFMailComposeViewControllerDelegate协议;

邮件发送方法:

-(void)displayComposerSheet
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    //设置主题
    [picker setSubject:@"博客园-FlyElephant"];
    
    //设置收件人
    NSArray *toRecipients = [NSArray arrayWithObjects:@"[email protected]",
                             nil];
    NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]",
                             @"[email protected]", nil];
    NSArray *bccRecipients = [NSArray arrayWithObjects:@"[email protected]",
                              nil];
    
    [picker setToRecipients:toRecipients];
    [picker setCcRecipients:ccRecipients];
    [picker setBccRecipients:bccRecipients];
    
    //设置附件为图片
    NSString *path = [[NSBundle mainBundle] pathForResource:@"man"
                                                     ofType:@"jpg"];
    NSData *myData = [NSData dataWithContentsOfFile:path];
    [picker addAttachmentData:myData mimeType:@"image/png"
                     fileName:@"man"];
    
    // 设置邮件发送内容
    NSString *emailBody = @"IOS中的个人博客地址:http://www.cnblogs.com/xiaofeixiang";
    [picker setMessageBody:emailBody isHTML:NO];
    
    //邮件发送的模态窗口
    [self presentModalViewController:picker animated:YES];
}

  邮件发送完成调用的方法:

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled: //取消
            NSLog(@"MFMailComposeResultCancelled-取消");
            break;
        case MFMailComposeResultSaved: // 保存
            NSLog(@"MFMailComposeResultSaved-保存邮件");
            break;
        case MFMailComposeResultSent: // 发送
            NSLog(@"MFMailComposeResultSent-发送邮件");
            break;
        case MFMailComposeResultFailed: // 尝试保存或发送邮件失败
            NSLog(@"MFMailComposeResultFailed: %@...",[error localizedDescription]);
            break;
    }
    
    // 关闭邮件发送视图
    [self dismissViewControllerAnimated:YES completion:nil];
}

 效果演示:

iOS开发-邮件发送

SKPSMTPMessage发送

这个第三方类库出现的比较早,引用的也比较多,把常用的邮件发送都封装到了起来,直接调用,是一个很简单的事情,下载之后会看到一个Demo和一个SMTPLibrary,直接将SMTPLibray拷贝到项目中,引用即可:

iOS开发-邮件发送

 

①需要发送的控制器上导入SKPSMTPMessage和NSData+base64Additions头文件

#import "SKPSMTPMessage.h"

#import "NSData+Base64Additions.h"

②实现SKPSMTPMessageDelegate协议;

③具体发送方法:

 SKPSMTPMessage *myMessage = [[SKPSMTPMessage alloc] init];
    myMessage.fromEmail=@"[email protected]";
    
    myMessage.toEmail=@"[email protected]";
    myMessage.bccEmail=@"[email protected]";
    myMessage.relayHost=@"smtp.163.com";
    
    myMessage.requiresAuth=YES;
    if (myMessage.requiresAuth) {
        myMessage.login=@"[email protected]";
        
        myMessage.pass=@"keso";
        
    }
    
    myMessage.wantsSecure =YES; //为gmail邮箱设置 smtp.gmail.com
    
    myMessage.subject = @"博客园-FlyElephant";

    myMessage.delegate = self;
    //设置邮件内容
    NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain; charset=UTF-8",kSKPSMTPPartContentTypeKey,
                               @"做一个简简单单,快快乐乐的程序员~keso",kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
    
    
    myMessage.parts = [NSArray arrayWithObjects:plainPart,nil];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [myMessage send];
    });

设置发送之后的处理:

- (void)messageSent:(SKPSMTPMessage *)message
{
  NSLog(@"恭喜,邮件发送成功");
}

- (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
    NSLog(@"不好意思,邮件发送失败");
    
}

 上面的本人通过163邮箱发送到qq邮箱,其他邮箱之间的发送没有尝试过,以上已经实现了简单的邮件发送,具体邮箱之间的设置,可以参考之前邮件发送文章:

C#邮件发送(最坑爹的邮箱-QQ邮箱)。邮件发送比较简单,如有问题,可随时与我联系~