zoukankan      html  css  js  c++  java
  • 发送邮件--MFMailComposeViewController

    只能在真机使用.

    模拟器没有E-mail发送功能.无法调用

    #import "EmailViewController.h"

    #import <UIKit/UIKit.h>

    #import <MessageUI/MFMailComposeViewController.h>

     

    @interface EmailViewController ()<MFMailComposeViewControllerDelegate>

     

    @end

     

    @implementation EmailViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view.

        

        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];

        btn.frame=CGRectMake(20, 100, 200, 100);

        

        [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];

        

        [self.view addSubview:btn];

        

    }

     

    -(void)click

    {

        Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));

        if (!mailClass) {

            [self alertWithMessage:@"当前系统版本不支持应用内发送邮件功能,您可以使用mailto方法代替"];

            return;

        }

        if (![mailClass canSendMail]) {

            [self alertWithMessage:@"用户没有设置邮件账户"];

            return;

        }

        [self displayMailPicker];

        

     

    }

     

    - (void)displayMailPicker

    {

        MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];

        mailPicker.mailComposeDelegate = self;

        

        //设置主题

        [mailPicker setSubject: @"主题"];

        //添加收件人

        NSArray *toRecipients = [NSArray arrayWithObject: @"liuliuliu@hotmail.com"];

        [mailPicker setToRecipients: toRecipients];

        //添加抄送

        //NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];

        //[mailPicker setCcRecipients:ccRecipients];

        //添加密送

        //NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];

        //[mailPicker setBccRecipients:bccRecipients];

        

        // 添加一张图片

        UIImage *addPic = [UIImage imageNamed: @"Icon@2x.png"];

        NSData *imageData = UIImagePNGRepresentation(addPic);            // png

        //关于mimeType:http://www.iana.org/assignments/media-types/index.html

        [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];

        

        NSString *emailBody = @"<font color='red'>eMail</font> 正文";

        [mailPicker setMessageBody:emailBody isHTML:YES];

        //[self presentModalViewController: mailPicker animated:YES];

        [self presentViewController:mailPicker animated:YES completion:nil];

    }

     

    #pragma mark - 实现 MFMailComposeViewControllerDelegate

    - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error

    {

        //关闭邮件发送窗口

        //[self dismissModalViewControllerAnimated:YES];

        [self dismissViewControllerAnimated:YES completion:nil];

        NSString *msg;

        switch (result) {

            case MFMailComposeResultCancelled:

                msg = @"用户取消编辑邮件";

                break;

            case MFMailComposeResultSaved:

                msg = @"用户成功保存邮件";

                break;

            case MFMailComposeResultSent:

                msg = @"用户点击发送,将邮件放到队列中,还没发送";

                break;

            case MFMailComposeResultFailed:

                msg = @"用户试图保存或者发送邮件失败";

                break;

            default:

                msg = @"";

                break;

        }

        [self alertWithMessage:msg];

    }

     

     

    - (void)alertWithMessage:(NSString*)message

    {

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil

                                                       message:message

                                                      delegate:nil

                                             cancelButtonTitle:@"确定"

                                             otherButtonTitles:nil, nil];

        [alert show];

    }

     

    让明天,不后悔今天的所作所为
  • 相关阅读:
    String cannot applied 202010231141
    Dos命令快速删除文件和文件夹202007210924
    在共享文件夹可以直接右键选择映射成本地磁盘20200703
    CMD不能正常使用ping命令 202006301610
    Burp Suite测试Bug2020061801
    java正则表达式匹配电子邮件地址20200608
    [国家集训队]矩阵乘法
    一个极其常见的式子
    AHOI2018 Day1
    [AHOI2013]作业
  • 原文地址:https://www.cnblogs.com/-yun/p/4503635.html
Copyright © 2011-2022 走看看