iOS 发送邮件

来源:互联网 发布:合肥网络推广公司 编辑:程序博客网 时间:2024/06/05 10:03
IOS系统框架提供的两种发送Email的方法:openURL 和 MFMailComposeViewController。借助这两个方法,我们可以轻松的在应用里加入如用户反馈这类需要发送邮件的功能。
 
1.openURL
使用openURL调用系统邮箱客户端是我们在IOS3.0以下实现发邮件功能的主要手段。我们可以通过设置url里的相关参数来指定邮件的内容,不过其缺点很明显,这样的过程会导致程序暂时退出。下面是使用openURL来发邮件的一个小例子:
    1.    #pragma mark - 使用系统邮件客户端发送邮件  
    2.    -(void)launchMailApp  
    3.    {    
    4.        NSMutableString *mailUrl = [[[NSMutableString alloc]init]autorelease];  
    5.        //添加收件人  
    6.        NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];  
    7.        [mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];  
    8.        //添加抄送  
    9.        NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];    
    10.        [mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]];  
    11.        //添加密送  
    12.        NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];    
    13.        [mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]];  
    14.        //添加主题  
    15.        [mailUrl appendString:@"&subject=my email"];  
    16.        //添加邮件内容  
    17.        [mailUrl appendString:@"&body=<b>email</b> body!"];  
    18.        NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];    
    19.        [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];    
    20.    } 
 
2.MFMailComposeViewController
MFMailComposeViewController是在IOS3.0新增的一个接口,它在MessageUI.framework中。通过调用MFMailComposeViewController,可以把邮件发送窗口集成到我们的应用里,发送邮件就不需要退出程序了。MFMailComposeViewController的使用方法:
        1.项目中引入MessageUI.framework;
        2.在使用的文件中导入MFMailComposeViewController.h头文件;
        3.实现MFMailComposeViewControllerDelegate,处理邮件发送事件;
        4.调出邮件发送窗口前先使用MFMailComposeViewController里的“+ (BOOL)canSendMail”方法检查用户是否设置了邮件账户;
        5.初始化MFMailComposeViewController,构造邮件体
 
    1.    //  
    2.    //  ViewController.h  
    3.    //  MailDemo  
    4.    //  
    5.    //  Created by LUOYL on 12-4-4.  
    6.    //  Copyright (c) 2012年 http://luoyl.info. All rights reserved.  
    7.    //  
    8.     
    9.    #import <UIKit/UIKit.h>  
    10.    #import <MessageUI/MFMailComposeViewController.h>  
    11.     
    12.    @interface ViewController : UIViewController<MFMailComposeViewControllerDelegate>  
    13.     
    14.    @end 
 
    1.    #pragma mark - 在应用内发送邮件  
    2.    //激活邮件功能  
    3.    - (void)sendMailInApp  
    4.    {  
    5.        Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));   
    6.        if (!mailClass) {  
    7.            [self alertWithMessage:@"当前系统版本不支持应用内发送邮件功能,您可以使用mailto方法代替"];  
    8.            return;  
    9.        }  
    10.        if (![mailClass canSendMail]) {  
    11.            [self alertWithMessage:@"用户没有设置邮件账户"];  
    12.            return;  
    13.        }  
    14.        [self displayMailPicker];  
    15.    }  
    16.     
    17.    //调出邮件发送窗口  
    18.    - (void)displayMailPicker  
    19.    {  
    20.        MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];    
    21.        mailPicker.mailComposeDelegate = self;    
    22.          
    23.        //设置主题    
    24.        [mailPicker setSubject: @"eMail主题"];    
    25.        //添加收件人  
    26.        NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com"];  
    27.        [mailPicker setToRecipients: toRecipients];    
    28.        //添加抄送  
    29.        NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];    
    30.        [mailPicker setCcRecipients:ccRecipients];        
    31.        //添加密送  
    32.        NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];    
    33.        [mailPicker setBccRecipients:bccRecipients];    
    34.          
    35.        // 添加一张图片    
    36.        UIImage *addPic = [UIImage imageNamed: @"Icon@2x.png"];    
    37.        NSData *imageData = UIImagePNGRepresentation(addPic);            // png       
    38.        //关于mimeType:http://www.iana.org/assignments/media-types/index.html  
    39.        [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];    
    40.       
    41.        //添加一个pdf附件  
    42.        NSString *file = [self fullBundlePathFromRelativePath:@"高质量C++编程指南.pdf"];  
    43.        NSData *pdf = [NSData dataWithContentsOfFile:file];  
    44.        [mailPicker addAttachmentData: pdf mimeType: @"" fileName: @"高质量C++编程指南.pdf"];    
    45.     
    46.        NSString *emailBody = @"<font color='red'>eMail</font> 正文";    
    47.        [mailPicker setMessageBody:emailBody isHTML:YES];    
    48.        [self presentModalViewController: mailPicker animated:YES];    
    49.        [mailPicker release];    
    50.    }  
    51.     
    52.    #pragma mark - 实现 MFMailComposeViewControllerDelegate   
    53.    - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error  
    54.    {  
    55.        //关闭邮件发送窗口  
    56.        [self dismissModalViewControllerAnimated:YES];  
    57.        NSString *msg;    
    58.        switch (result) {    
    59.            case MFMailComposeResultCancelled:    
    60.                msg = @"用户取消编辑邮件";    
    61.                break;    
    62.            case MFMailComposeResultSaved:    
    63.                msg = @"用户成功保存邮件";    
    64.                break;    
    65.            case MFMailComposeResultSent:    
    66.                msg = @"用户点击发送,将邮件放到队列中,还没发送";    
    67.                break;    
    68.            case MFMailComposeResultFailed:    
    69.                msg = @"用户试图保存或者发送邮件失败";    
    70.                break;    
    71.            default:    
    72.                msg = @"";  
    73.                break;    
    74.        }    
    75.        [self alertWithMessage:msg];  
    76.    }  
0 0
原创粉丝点击