ios如何利用系统邮件发送邮件

本文介绍了如何在iOS应用中使用MFMailComposeViewController组件来显示邮件发送窗口,并提供了配置邮件发送的基本步骤,包括添加收件人、邮件主题和邮件正文。
#pragma mark - 在应用内发送邮件
//调出邮件发送窗口
- (void)showMailPicker:(NSInteger)tag
{
    if ([MFMailComposeViewController canSendMail]){
        [self displayMailComposerSheet:tag];
    }else{
        NSLog(@"Device not configured to send mail");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请配置邮件"
                                                        message:@"请先设置邮件账户:'设置'->'邮件'->'添加帐号'"
                                                       delegate:self
                                              cancelButtonTitle:@"好"
                                              otherButtonTitles:nil,nil];
        [alert show];
        //iOS8打开设定界面
        //    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        //    return;
    }
    
}

- (void)displayMailComposerSheet:(NSInteger)tag
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
//    [picker setSubject:@"eMail主题"];
    
//    // Set up recipients
//    NSString *email=detailArray[tag];
//    NSArray *toRecipients = [NSArray arrayWithObject:email];
//    //NSArray *ccRecipients = [NSArray arrayWithObjects:@"1528693256@qq.com", nil];
//    //NSArray *bccRecipients = [NSArray arrayWithObject:@"1528693256@qq.com"];
//    
//    [picker setToRecipients:toRecipients];//添加收件人
//    //[picker setCcRecipients:ccRecipients];//添加抄送
//    //[picker setBccRecipients:bccRecipients];//添加密送
//    
//    // Attach an image to the email
//    //NSString *path = [[NSBundle mainBundle] pathForResource:@"swift" ofType:@"png"];
//    //NSData *myData = [NSData dataWithContentsOfFile:path];
//    //[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"swift.png"];
//    
//    // 邮件正文
//    NSString *emailBody = @"测试";
//    [picker setMessageBody:emailBody isHTML:NO];
    
    if (tag==4) {
        [picker setSubject:@"商务合作"];
        NSString *email=detailArray[tag];
        NSArray *toRecipients = [NSArray arrayWithObject:email];
        [picker setToRecipients:toRecipients];//添加收件人
        // 邮件正文
        NSString *emailBody = @"内容: \n联系方式:";
        [picker setMessageBody:emailBody isHTML:NO];
    } else if(tag==5){
        [picker setSubject:@"应聘(XXX职位)"];
        NSString *email=detailArray[tag];
        NSArray *toRecipients = [NSArray arrayWithObject:email];
        [picker setToRecipients:toRecipients];//添加收件人
        // 邮件正文
        NSString *emailBody = @"内容:你好,我想加入飞磨的大家庭,成为飞磨公司的(XXX职位)\n个人简介:\n工作经验:";
        [picker setMessageBody:emailBody isHTML:NO];
    }
    
    [self presentViewController:picker animated:YES completion:NULL];
}

#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    //关闭邮件发送窗口
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    NSString *msg;
    switch (result) {
        case MFMailComposeResultCancelled:
            msg = @"用户取消编辑邮件";
            break;
        case MFMailComposeResultSaved:
            msg = @"用户成功保存邮件";
            break;
        case MFMailComposeResultSent:
            msg = @"用户点击发送,将邮件放到队列中,还没发送";
            break;
        case MFMailComposeResultFailed:
            msg = @"用户试图保存或者发送邮件失败";
            break;
        default:
            msg = @"";
            break;
    }
    //APP_ALERT_SHOW_TOP(msg);
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"邮件提示"
                                                    message:msg
                                                   delegate:self
                                          cancelButtonTitle:@"我知道了"
                                          otherButtonTitles:nil,nil];
    [alert show];

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值