• 发送邮件


    调用手机内部邮件发送邮件,首先要对邮件进行设置,确定可以发送邮件后才可以,否则邮件无法正常发送。

    (一) 头文件

    #import <UIKit/UIKit.h>
    #import <MessageUI/MessageUI.h>
    
    #import "MailViewController.h"
    
    @interface MailViewController ()<MFMailComposeViewControllerDelegate>
    {
        UILabel *sendResult;
    }
    
    @end

    (二)发送邮件

    -(void)sendMail
    {
        if ([MFMailComposeViewController canSendMail])
        {
            NSLog(@"canSend:YES");
            sendResult.text=@"canSend:Yes";
            
            // 创建邮件发送界面
            MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
            // 设置邮件代理
            [mailCompose setMailComposeDelegate:self];
            // 设置收件人
            [mailCompose setToRecipients:@[@"liuliangchb@163.com"]];
            // 设置抄送人
            [mailCompose setCcRecipients:@[@"807243674@qq.com"]];
            // 设置密送人
            [mailCompose setBccRecipients:@[@"15690725786@163.com"]];
            // 设置邮件主题
            [mailCompose setSubject:@"Test"];
            //设置邮件的正文内容
            NSString *emailContent = @"Test mail";
            // 是否为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:@"imageName"];//添加图片附件
            NSData *imageData = UIImagePNGRepresentation(image);
            [mailCompose addAttachmentData:imageData mimeType:@"" fileName:@"imageName.png"];
    NSString *file = [[NSBundle mainBundle] pathForResource:@"pdfFile" ofType:@"pdf"];//添加子文件路径 NSData *pdf = [NSData dataWithContentsOfFile:file]; [mailCompose addAttachmentData:pdf mimeType:@"" fileName:@"pdfFile.pdf"];

          NSString *file = [[NSString alloc] initWithString:filePath];//通过全路径filePath添加文件附件
          NSData *pdf = [NSData dataWithContentsOfFile:file];
          [mailView addAttachmentData:pdf mimeType:@"" fileName:[[NSString alloc] initWithString:fileName]];
    */ // 弹出邮件发送视图 [self presentViewController:mailCompose animated:YES completion:nil]; } else { NSLog(@"canSend:NO"); sendResult.text=@"canSend:No"; } }

    (三)通过代理获取发送结果

    -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
        switch (result) {
            case MFMailComposeResultCancelled:
                sendResult.text=@"Canceled";
                break;
            case MFMailComposeResultSaved:
                sendResult.text=@"Saved";
                break;
            case MFMailComposeResultSent:
                sendResult.text=@"Sent";
                break;
            case MFMailComposeResultFailed:
                sendResult.text=@"Failed";
                break;
        }
        // 关闭邮件发送视图
        [self dismissViewControllerAnimated:YES completion:nil];
    }

    //---------------------------------代码---------------------------------//

    #import "MailViewController.h"
    
    @interface MailViewController ()<MFMailComposeViewControllerDelegate>
    {
        UILabel *sendResult;
    }
    
    @end
    
    @implementation MailViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.view.backgroundColor=[UIColor blueColor];
        sendResult=[[UILabel alloc] init];
        sendResult.text=@"Result";
        sendResult.frame=CGRectMake(150, 200, 100, 30);
        [self.view addSubview:sendResult];    // Do any additional setup after loading the view.
        
        UIButton *sendBtn=[[UIButton alloc] initWithFrame:CGRectMake(150, 400, 100, 30)];
        sendBtn.backgroundColor=[UIColor redColor];
        [sendBtn setTitle:@"Send" forState:UIControlStateNormal];
        [sendBtn addTarget:self action:@selector(sendMail) forControlEvents:UIControlEventTouchDown];
        [self.view addSubview:sendBtn];
    }
    
    -(void)sendMail
    {
        if ([MFMailComposeViewController canSendMail])
        {
            NSLog(@"canSend:YES");
            sendResult.text=@"canSend:Yes";
            
            // 创建邮件发送界面
            MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
            // 设置邮件代理
            [mailCompose setMailComposeDelegate:self];
            // 设置收件人
            [mailCompose setToRecipients:@[@"11111111111111111111@163.com"]];
            // 设置抄送人
            [mailCompose setCcRecipients:@[@"222222222222@qq.com"]];
            // 设置密送人
            [mailCompose setBccRecipients:@[@"33333333333333333@163.com"]];
            // 设置邮件主题
            [mailCompose setSubject:@"Test"];
            //设置邮件的正文内容
            NSString *emailContent = @"Test mail";
            // 是否为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:@"imageName"];
            NSData *imageData = UIImagePNGRepresentation(image);
            [mailCompose addAttachmentData:imageData mimeType:@"" fileName:@"imageName.png"];
            NSString *file = [[NSBundle mainBundle] pathForResource:@"pdfFile" ofType:@"pdf"];
            NSData *pdf = [NSData dataWithContentsOfFile:file];
            [mailCompose addAttachmentData:pdf mimeType:@"" fileName:@"pdfFile.pdf"];
             */
            // 弹出邮件发送视图
            [self presentViewController:mailCompose animated:YES completion:nil];
        }
        else
        {
            NSLog(@"canSend:NO");
            sendResult.text=@"canSend:No";
        }
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
        switch (result) {
            case MFMailComposeResultCancelled:
                sendResult.text=@"Canceled";
                break;
            case MFMailComposeResultSaved:
                sendResult.text=@"Saved";
                break;
            case MFMailComposeResultSent:
                sendResult.text=@"Sent";
                break;
            case MFMailComposeResultFailed:
                sendResult.text=@"Failed";
                break;
        }
        // 关闭邮件发送视图
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
    /*
    #pragma mark - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    
    @end
  • 相关阅读:
    会话状态服务器解决方法
    让笔记本在插上外置鼠标时触摸板自动关闭
    “检测到有潜在危险的 Request.Form(QueryString) 值”的解决方法
    SQL Server2008不能登录解决方法
    SqlHelper
    修改IE查看源代码编辑器
    由于启动用户实例的进程时出错,导致无法生成 SQL Server 的用户实例解决办法
    性能测试用户模型(二):用户模型图
    索引帖:性能测试新手误区系列
    性能测试用户模型(三):基础数据分析、场景数据
  • 原文地址:https://www.cnblogs.com/llstart-new0201/p/9722253.html
Copyright © 2020-2023  润新知