ios 发送邮件,短信

Posted 预估计

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios 发送邮件,短信相关的知识,希望对你有一定的参考价值。

第一种方式:openURL

    #pragma mark - 使用系统邮件客户端发送邮件   
    -(void)launchMailApp   
    {     
        NSMutableString *mailUrl = [[[NSMutableString alloc]init]autorelease];   
        //添加收件人   
        NSArray *toRecipients = [NSArray arrayWithObject: @"[email protected]"];   
        [mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];   
        //添加抄送   
        NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];     
        [mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]];   
        //添加密送   
        NSArray *bccRecipients = [NSArray arrayWithObjects:@"[email protected]", nil];     
        [mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]];   
        //添加主题   
        [mailUrl appendString:@"&subject=my email"];   
        //添加邮件内容   
        [mailUrl appendString:@"&body=<b>email</b> body!"];   
        NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];     
        [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];     
    }  

第二种方式:MFMailComposeViewController

让控制器遵守<MFMailComposeViewControllerDelegate>协议

#pragma mark - 在应用内发送邮件
//激活邮件功能
- (void)sendMailInApp
{
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (!mailClass) {
        [WSYFunction alertViewWithTitle:@"提示" message:@"当前系统版本不支持应用内发送邮件功能,您可以使用mailto方法代替" buttonTitle:@"确定"];
        return;
    }
    if (![mailClass canSendMail]) {
        [WSYFunction alertViewWithTitle:@"提示" message:@"用户没有设置邮件账户" buttonTitle:@"确定"];
        return;
    }
    [self displayMailPicker];
}

//调出邮件发送窗口
- (void)displayMailPicker
{
    MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
    mailPicker.mailComposeDelegate = self;
    
    //设置主题
    [mailPicker setSubject: [NSString stringWithFormat:@"%@",self.bookNameArray[ChooseBook]]];
    //添加收件人
    NSArray *toRecipients = [NSArray arrayWithObject: @""];
    [mailPicker setToRecipients: toRecipients];
    //添加抄送
//    NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];
//    [mailPicker setCcRecipients:ccRecipients];
    //添加密送
//    NSArray *bccRecipients = [NSArray arrayWithObjects:@"[email protected]", nil];
//    [mailPicker setBccRecipients:bccRecipients];
    
//    // 添加一张图片
    UIImage *addPic = [UIImage imageNamed: @"lanch.jpeg"];
    NSData *imageData = UIImagePNGRepresentation(addPic);            // png
    //关于mimeType:http://www.iana.org/assignments/media-types/index.html
    [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"lanch.jpeg"];

    //构造字符串文件的存储路径
    // 获取Document目录
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *strPath = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",self.bookNameArray[ChooseBook]]];
    //构造字符串对象
    NSString *book_str = @"";
    for (int i = 0; i < self.array.count; i++) {
        book_str = [book_str stringByAppendingString:[NSString stringWithFormat:@"\n\n%@",self.array[i]]];
    }
    //通过将writeToFile:atomically:encoding:error:方法发送给字符串对象完成字符串存储到文件内的功能
    [book_str writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
    NSData *bookData = [NSData dataWithContentsOfFile:strPath];
    
    //发送txt文本附件
    [mailPicker addAttachmentData:bookData mimeType:@"text/txt" fileName:[NSString stringWithFormat:@"%@.txt",self.bookNameArray[ChooseBook]]];
    NSString *emailBody = @"<font color=‘red‘>eMail</font> 正文";
    [mailPicker setMessageBody:emailBody isHTML:YES];
    [self presentModalViewController: mailPicker animated:YES];
}

#pragma mark - 实现 MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    //关闭邮件发送窗口
    [self dismissModalViewControllerAnimated:YES];
    NSString *msg;
    switch (result) {
        case MFMailComposeResultCancelled:
            msg = @"您已取消编辑邮件";
            break;
        case MFMailComposeResultSaved:
            msg = @"您已成功保存邮件";
            break;
        case MFMailComposeResultSent:
            msg = @"您点击发送,将邮件放到队列中,还没发送";
            break;
        case MFMailComposeResultFailed:
            msg = @"您试图保存或者发送邮件失败";
            break;
        default:
            msg = @"";
            break;
    }
    [WSYFunction alertViewWithTitle:@"" message:msg buttonTitle:@"确定"];
}

最后使用直接调用方法:

[self sendMailInApp];

 

以上是关于ios 发送邮件,短信的主要内容,如果未能解决你的问题,请参考以下文章

如何从 iPhone 向蓝牙 LE 设备发送电子邮件、短信通知?

ios UIActivityViewController 没有附加图片发送短信

vbs代码发送邮件,电脑开机后向手机发短信

vbs代码发送邮件,实现电脑开机后向手机发短信

MFMailComposeViewController iOS 上缺少取消和发送按钮

无法在片段内查看接收短信