| 本帖最后由 竹林风 于 2018-12-15 14:02 编辑 
 
   文章导航   
 
 系统为我们提供了两种发送Email的方法:
 
 1.OpenUrl
 2.MFMailComposeViewController。
 
 OpenUrl
 
 使用openUrl调用系统邮箱是iOS3.0以下实现发邮件功能的主要手段。我们可以通过设置url里面的相关参数来指定邮件的内容,不过缺点也很明显,这样的过程会导致程序暂时退出。如:
 
 
 [Objective-C] 纯文本查看 复制代码 #pragma mark - 使用系统邮件客户端发送邮件   
-(void)launchMailApp   
{     
    NSMutableString *mailUrl = [[[NSMutableString alloc]init]autorelease];   
    //添加收件人   
    NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];   
    [mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];   
    //添加抄送   
    NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];     
    [mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]];   
    //添加密送   
    NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];     
    [mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]];   
    //添加主题   
    [mailUrl appendString:@"&subject=my email"];   
    //添加邮件内容   
    [mailUrl appendString:@"&body=<b>email</b> body!"];   
    NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];     
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];     
}  MFMailComposeViewController
 
 使用MFMailComposeViewController,我们需要引入MessageUI.framework.通过调用MFMailComposeViewController,可以把邮件发送窗口集成到我们的应用里面,发送邮件就不需要再退出程序了。
 
 
 1.引用MessageUI.framework
 2.在使用的文件中导入MFMailComposeViewController.h头文件
 3.实现MFMailComposeViewControllerDelegate,处理邮件发送事件;
 4.调用邮件发送窗口前先使用MFMailComposeViewController里面的“canSendMail”方法检查用户是否设置了邮件帐户;
 5.初始化MFMailComposeViewController,构造邮件体
 
 
 发送代码实现:
 
 
 
 [Objective-C] 纯文本查看 复制代码 //调出邮件发送窗口
- (void)displayMailPicker
{
    if ([MFMailComposeViewController canSendMail]) {
        // 用户已设置邮件账户
        // 邮件服务器
        MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
        // 设置邮件代理
        [mailCompose setMailComposeDelegate:self];
        
        // 设置邮件主题
        [mailCompose setSubject:@"我是邮件主题"];
        
        // 设置收件人
        [mailCompose setToRecipients:@[@"1780575208@qq.com"]];
        // 设置抄送人
        [mailCompose setCcRecipients:@[@"1780575208@qq.com"]];
        // 设置密抄送
        [mailCompose setBccRecipients:@[@"1780575208@qq.com"]];
        
        /**
         *  设置邮件的正文内容
         */
        NSString *emailContent = @"我是邮件内容";
        // 是否为HTML格式
        [mailCompose setMessageBody:emailContent isHTML:NO];
        // 如使用HTML格式,则为以下代码
        //    [mailCompose setMessageBody:@"<html><body><p>Hello</p><p>World!</p></body></html>" isHTML:YES];
        
        /**
         *  添加附件
         */
        UIImage *image = [UIImage imageNamed:@"apple"];
        NSData *imageData = UIImagePNGRepresentation(image);
        [mailCompose addAttachmentData:imageData mimeType:@"" fileName:@"apple"];
        
        NSString *file = [[NSBundle mainBundle] pathForResource:@"info" ofType:@"plist"];
        NSData *pdf = [NSData dataWithContentsOfFile:file];
        [mailCompose addAttachmentData:pdf mimeType:@"" fileName:@"file"];
        
        // 弹出邮件发送视图
        [self presentViewController:mailCompose animated:YES completion:nil];
        
    }else{
        NSLog(@"请先设置登录邮箱号");
    }
}
 
 MFMailComposeViewControllerDelegate的代理方法
 
 
 
 [Objective-C] 纯文本查看 复制代码 #pragma mark - 实现 MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    //关闭邮件发送窗口
    [self dismissViewControllerAnimated:YES completion:^{
        NSString *msg;
        switch (result) {
            case MFMailComposeResultCancelled:
                msg = @"用户取消编辑邮件";
                break;
            case MFMailComposeResultSaved:
                msg = @"用户成功保存邮件";
                break;
            case MFMailComposeResultSent:
                msg = @"用户点击发送,将邮件放到队列中,还没发送";
                break;
            case MFMailComposeResultFailed:
                msg = @"用户试图保存或者发送邮件失败";
                break;
            default:
                msg = @"";
                break;
        }
        NSLog(@"%@",msg);
    }];
}
 附件:
  Test-SendEmail.zip
(169.38 KB, 下载次数: 0) |