• 发送邮件


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

    (一) 头文件

    #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
  • 相关阅读:
    Java实现 洛谷 P1423 小玉在游泳
    Java设置session超时(失效)的时间
    How Tomcat works — 八、tomcat中的session管理
    三种常用的MySQL建表语句
    mysql和oracle的区别(功能性能、选择、使用它们时的sql等对比)
    oracle 基础表 mysql版
    oracle员工表和部门表基本操作
    Oracle
    java生成6位随机数
    用Ajax图片上传、预览、修改图片
  • 原文地址:https://www.cnblogs.com/llstart-new0201/p/9722253.html
Copyright © 2020-2023  润新知