发送邮件---iphone开发

本文介绍如何在iOS应用中实现邮件发送功能,包括检测设备是否支持邮件发送、使用MFMailComposeViewController展示邮件编辑界面并设置邮件内容,以及在不支持直接发送的情况下通过URL Scheme启动邮件客户端。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、导入MessageUI.framework包

2、引入头文件#import <MessageUI/MessageUI.h> 

3、实现代理MFMailComposeViewControllerDelegate

代码如下:

 

1、监测手机是否遇有首发邮件功能

Class mailClass = (NSClassFromString(@"MFMailComposeViewController")); 
                    if (mailClass != nil)  
                    {  
                        if ([mailClass canSendMail])  
                        {  
                            [self displayComposerSheet];  
                        }   
                        else   
                        {  
                            [self launchMailAppOnDevice];  
                        }  
                    }   
                    else   
                    {  
                        [self launchMailAppOnDevice];  
                    }      

2、可以发送邮件

-(void)displayComposerSheet   
{  
    NSLog(@"可以发送邮件~~~~~~~~~~~~");
    MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];  
    
    mailPicker.mailComposeDelegate = self;  
    
    //设置主题  
    [mailPicker setSubject: @"eMail主题"];  
    
    // 添加发送者  
    NSArray *toRecipients = [NSArray arrayWithObject: @"243832207@qq.com"];  
    //NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];  
    //NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com", nil];  
    [mailPicker setToRecipients: toRecipients];  
    //[picker setCcRecipients:ccRecipients];      
    //[picker setBccRecipients:bccRecipients];  
    
    // 添加图片  
//    UIImage *addPic = [UIImage imageNamed: @"123.jpg"];  
//    NSData *imageData = UIImagePNGRepresentation(addPic);            // png  
    // NSData *imageData = UIImageJPEGRepresentation(addPic, 1);    // jpeg  
//    [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"123.jpg"];  
    
    NSString *emailBody = @"您好:您订阅的杂志,请点击一下连接进行支付!";  
    [mailPicker setMessageBody:emailBody isHTML:YES];  
    
    [self presentModalViewController: mailPicker animated:YES];  
    [mailPicker release];  
}  

3、不能发送邮件
-(void)launchMailAppOnDevice  
{  
    NSLog(@"不能发送邮件~~~~~~~~~~~~");
    NSString *recipients = @"mailto:first@example.com&subject=my email!";  
    //@"mailto:first@example.com?cc=second@example.com,third@example.com&subject=my email!";  
    NSString *body = @"&body=email body!";  
    
    NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];  
    email = [email stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];  
    
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];  
}

4、代理方法
- (void)mailComposeController:(MFMailComposeViewController *)controller   
          didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error   
{  
    NSString *msg;  
    
    switch (result)   
    {  
        case MFMailComposeResultCancelled:  
            msg = @"邮件发送取消";  
            break;  
        case MFMailComposeResultSaved:  
            msg = @"邮件保存成功";  
            [Utils alertWithTitle:nil message:msg];  
            break;  
        case MFMailComposeResultSent:  
            msg = @"邮件发送成功";  
            [Utils alertWithTitle:nil message:msg]; 
            break;  
        case MFMailComposeResultFailed:  
            msg = @"邮件发送失败";  
            [Utils alertWithTitle:nil message:msg];  
            break;  
        default:  
            break;  
    }  
    
    [self dismissModalViewControllerAnimated:YES];  
}  
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值