zoukankan      html  css  js  c++  java
  • iOS开发之---发送邮件

    通过邮件视图控制器封装了邮件发送功能,开发者不需要知道不同的邮件协议或者如何与邮件服务器连接建立等。在iOS中,这些功能都被打包好,通过

    MFMailComposeViewController提供给我们。

    引入MessageUI库。

    搭建UI界面,添加按钮的事件响应方法:

    - (IBAction)demoAction:(id)sender {
        UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"请选择任务" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"打开邮件" otherButtonTitles:@"打开微博",@"打开微信", nil];
        [action showInView:self.view];
    }

    效果:

    动作表单代理方法:

    #pragma mark - UIActionSheet代理方法
    // 处理动作表单点击事件
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if (buttonIndex == 0) {
            MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
            mailVC.mailComposeDelegate = self;
            [mailVC setSubject:@"发送测试邮件"];
            [mailVC setMessageBody:@"测试内容" isHTML:NO];
            [self presentViewController:mailVC animated:YES completion:nil];
        }
    }

    效果:

    处理邮件发送的代理方法:

    #pragma mark - MFMailComposeViewControllerDelegate方法
    - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
    {
        switch (result) {
            case MFMailComposeResultCancelled:
                NSLog(@"取消发送邮件");
                [controller dismissViewControllerAnimated:YES completion:nil];
                break;
                
            case MFMailComposeResultSent:
                NSLog(@"邮件发送成功");
                [controller dismissViewControllerAnimated:YES completion:nil];
                break;
            case MFMailComposeResultFailed:
                NSLog(@"邮件发送失败");
                break;
            case MFMailComposeResultSaved:
                NSLog(@"邮件成功保存");
                [controller dismissViewControllerAnimated:YES completion:nil];
            default:
                break;
        }
    }

    点击发送按钮:

    附注:

    modal展示:presentViewController: animated: completion: 对应dismissViewControllerAnimated: completion:

    push展示:push....对pop.....

  • 相关阅读:
    HTML基础——网站友情链接显示页面
    HTML基础——网站图片显示页面
    HTML基础——网站信息显示页面
    Java数据库连接——PreparedStatement的使用
    Java数据库连接——jdbc-odbc桥连接方式及汉字乱码问题
    Java文件(io)编程——简易记事本开发
    Java文件(io)编程——文件字符流的使用
    idea ---- 打开的文件多行显示
    idea ---- 代码提示大小写区分
    idea ---- 显示行号 和方法分隔符
  • 原文地址:https://www.cnblogs.com/yaoxc/p/3728539.html
Copyright © 2011-2022 走看看