iOS系统邮箱

作者: goyohol | 来源:发表于2017-08-23 23:44 被阅读88次

准备工作:

加入MessageUI.framework

加入MessageUI.framework

头文件<MessageUI/MessageUI.h>
#import <MessageUI/MessageUI.h>

协议MFMailComposeViewControllerDelegate

<UIAlertViewDelegate,MFMailComposeViewControllerDelegate>



确认发送的提示UIAlertView

UIAlertView * alertV = [[UIAlertView alloc] initWithTitle:@"发到邮箱" message:@"请输入邮箱地址" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
[alertV setAlertViewStyle:UIAlertViewStylePlainTextInput];//“输入文本”类型

//获取输入文本框
UITextField * textNameTF = [alertV textFieldAtIndex:0];
textNameTF.keyboardType = UIKeyboardTypeEmailAddress; //邮箱键盘
textNameTF.placeholder = @"请输入邮箱地址";
//看本地之前是否已经存储 (NSUserDefaults)
textNameTF.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"email"]?[[NSUserDefaults standardUserDefaults] objectForKey:@"email"]:@"";
textNameTF.clearButtonMode = UITextFieldViewModeWhileEditing;

[alertV show];

UIAlertView的代理

#pragma mark - UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    //NSLog(@"%ld",buttonIndex);
    if (buttonIndex == 1) { //确认按钮
        UITextField * toEmialTF = [alertView textFieldAtIndex:0];
    
        static NSString * const Regex_Email = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; //验证 邮箱号
        NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",Regex_Email];
        if ([predicate evaluateWithObject:toEmialTF.text] ) { 
            //是邮箱
            [self sendByEmailWith:toEmialTF.text andTitle:@""];//可获取邮箱标题
        } else {
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hud.mode = MBProgressHUDModeText;
            hud.label.text = @"请输入正确格式的邮箱";
            hud.margin = 10.f;
            hud.offset = CGPointMake(0, 0.f);
            hud.removeFromSuperViewOnHide = YES;
            [hud hideAnimated:YES afterDelay:1.5f];
        }
    
    }

}

发送邮件操作:

#pragma mark - 发送到邮箱
-(void)sendByEmailWith:(NSString *)toEmailStr andTitle:(NSString *)titleStr {
    // 邮件服务器
    MFMailComposeViewController * mailCompose = [[MFMailComposeViewController alloc] init];
    [mailCompose setMailComposeDelegate:self]; // 代理

    [mailCompose setSubject:@"邮件主题"];// 设置邮件主题
    [mailCompose setToRecipients:@[toEmailStr] ];// 设置收件人 (数组)
    [mailCompose setCcRecipients:@[] ];// 设置抄送人 (数组)
    [mailCompose setBccRecipients:@[] ];// 设置密抄送 (数组)


    /** 设置邮件的正文内容 */
    NSString * emailContent = @"邮件内容";

    // 是否为HTML格式
    [mailCompose setMessageBody:emailContent isHTML:NO];
    // 如使用HTML格式,则为以下代码
    //[mailCompose setMessageBody:@"<html><body><p>Hello</p><p>World!</p></body></html>" isHTML:YES];



    /**  添加附件 :文件 ➡️ NSData   */
    /** A.发送图片 */
    UIImage * image = [UIImage imageNamed:@"chx.jpg"];
    NSData *imageData = UIImagePNGRepresentation(image); //图片较大(画质高)
    //NSData *imageData = UIImageJPEGRepresentation(image, 1.0);//图片较小(画质低)
    [mailCompose addAttachmentData:imageData mimeType:@"" fileName:@"custom.jpg"];//邮件显示的文件名

    /** B.发送文档 */
    //NSString * pathStr = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/localFile"];
    //NSString * fileStr = [pathStr stringByAppendingPathComponent:@"蓝牙设备iOS SDK使用文档 -3.pdf"];
    //NSData * data = [NSData dataWithContentsOfFile:fileStr];//保存的数据
    //[mailCompose addAttachmentData:data mimeType:@"" fileName:@"蓝牙设备iOS SDK使用文档.pdf"];//邮件显示的文件名

    // 弹出邮件发送界面
     if (mailCompose) {  //如果没有设置邮件帐户,mailController为nil
       [self presentViewController:mailCompose animated:YES completion:nil];
    }

}



#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {

    switch(result) {
        case MFMailComposeResultCancelled:{ // 用户取消编辑
            NSLog(@"Mail send canceled...");
        }break;
        case MFMailComposeResultSaved:{ // 用户保存邮件
            NSLog(@"Mail saved...");
        }break;
        case MFMailComposeResultSent:{ // 用户点击发送
            NSLog(@"Mail sent...");
        }break;
        case MFMailComposeResultFailed:{ // 用户尝试保存或发送邮件失败
            NSLog(@"Mail send errored: %@...", [error localizedDescription]);
        
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hud.mode = MBProgressHUDModeText;
            hud.label.text = [error localizedDescription];
            hud.margin = 10.f;
            hud.offset = CGPointMake(0, 0.f);
            hud.removeFromSuperViewOnHide = YES;
            [hud hideAnimated:YES afterDelay:1.5f];
        }break;
    }

    // 关闭邮件发送视图
    [self dismissViewControllerAnimated:YES completion:nil];
}


操作:

  • 1.填入发送至邮箱
    填入 发送至的邮箱
    可能出现状况:提示没有系统邮箱账户
    提示没有邮箱账户
  • 2.添加打开 系统的邮箱账户
    设置→邮件→账户
    添加邮箱账户、打开邮件开关

    3.发送邮件
    往工程里,拖入的图片文件:
    拖入的图片文件
    编辑邮件内容:
    编辑邮件
    4.点击“发送”后,控制台输出:

    5.发送成功,接收邮件:
    QQ邮箱提醒
    接收到的邮件:

    代码地址demo传送门





很久没写简书了,主要是简书编辑器的bug!!!!!
身为一个Coder,看到bug,就浑身难受!!!!!
关键是还解决不了!

自己就跟工作人员交流了一下下。。


而现如今还是不能使用“</br>”: 真心有点伤心 💔

屏幕快照 2017-08-23 下午11.39.02.png

希望能快快恢复!🤓🤓🤓🤓🤓🤓🤓🤓🤓🤓🤓🤓🤓





goyohol's essay

相关文章

网友评论

    本文标题:iOS系统邮箱

    本文链接:https://www.haomeiwen.com/subject/zxburxtx.html