IOS发送带附件的邮件

本文转载至  http://blog.csdn.net/zltianhen/article/details/7693810

 

 

1.加入邮箱的框架

 

  1. #import <MessageUI/MFMailComposeViewController.h>  
  2. #import <MessageUI/MessageUI.h>  

 

 

2.添加委托

 

  1. @interface ViewController : UIViewController<MFMailComposeViewControllerDelegate>  

 

 

3.实现代码

 

    1. - (void)alertWithTitle:(NSString *)title  msg:(NSString *)msg     
    2. {    
    3.     if (title && msg)   
    4.     {  
    5.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title     
    6.                                                         message:msg     
    7.                                                        delegate:nil     
    8.                                               cancelButtonTitle:@"确定"     
    9.                                               otherButtonTitles:nil];    
    10.         [alert show];    
    11.         [alert release];    
    12.     }  
    13.       
    14. }    
    15.   
    16. - (IBAction)OnButtonWriteLog:(id)sender   
    17. {     
    18.     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];  
    19.     [dateFormatter setDateFormat:@"yyyy_MM_dd_HH_mm"];  // 设置格式为年-月-日 时:分:秒:毫秒  
    20.       
    21.     NSString *timeStr = [dateFormatter stringFromDate:[NSDate date]];  
    22.     [dateFormatter release];  
    23.       
    24.       
    25.     NSArray *userPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    26.     NSString *filePackageName = [userPaths objectAtIndex:0];  
    27.       
    28.   
    29.     NSString* fileNamePackageRes = [[NSString alloc] initWithFormat:@"%@/%@_%@",filePackageName,timeStr,@"log.txt"];  
    30.       
    31.       
    32.     NSString* strLogData = [[NSString alloc]initWithFormat:@"my is log time:%@",timeStr];  
    33.     [strLogData writeToFile:fileNamePackageRes atomically:NO encoding:NSUTF8StringEncoding error:nil];  
    34.   
    35.       
    36.     [_logName release];  
    37.     _logName = [[NSString alloc ]initWithString:fileNamePackageRes];  
    38.       
    39.     [fileNamePackageRes release];  
    40.       
    41.     [self alertWithTitle:@"提示" msg:@"写入成功"];  
    42. }  
    43.   
    44.   
    45.   
    46. - (IBAction)OnButtonSendMailToMe:(id)sender {  
    47.       
    48.     NSString *logData = [[NSString alloc]initWithContentsOfFile:_logName encoding:NSUTF8StringEncoding error:nil];  
    49.     if (logData)  
    50.     {  
    51.         MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];  
    52.         if(mailCompose)  
    53.         {  
    54.             //设置代理  
    55.             [mailCompose setMailComposeDelegate:self];  
    56.               
    57.             NSArray *toAddress = [NSArray arrayWithObject:@"98zg@sina.cn"];  
    58.             NSArray *ccAddress = [NSArray arrayWithObject:@"17333245@qq.com"];;  
    59.             NSString *emailBody = @"<H1>日志信息</H1>";  
    60.               
    61.             //设置收件人  
    62.             [mailCompose setToRecipients:toAddress];  
    63.             //设置抄送人  
    64.             [mailCompose setCcRecipients:ccAddress];  
    65.             //设置邮件内容      
    66.             [mailCompose setMessageBody:emailBody isHTML:YES];  
    67.               
    68.             NSData* pData = [[NSData alloc]initWithContentsOfFile:_logName];  
    69.               
    70.             //设置邮件主题  
    71.             [mailCompose setSubject:@"这里是主题"];  
    72.             //设置邮件附件{mimeType:文件格式|fileName:文件名}  
    73.             [mailCompose addAttachmentData:pData mimeType:@"txt" fileName:@"日志.txt"];  
    74.             //设置邮件视图在当前视图上显示方式  
    75.             [self presentModalViewController:mailCompose animated:YES];  
    76.         }  
    77.         [mailCompose release];  
    78.           
    79.         return;  
    80.     }  
    81.   
    82.       
    83.     [logData release];  
    84.       
    85.       
    86.     [self alertWithTitle:@"提示" msg:@"请先点击写入日志按钮"];  
    87. }  
    88.   
    89.   
    90. - (void)dealloc  
    91. {  
    92.     [_logName release];  
    93.     [super dealloc];  
    94. }  
    95.   
    96.    
    97.   
    98. - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error   
    99. {  
    100.   
    101.     NSString *msg;    
    102.       
    103.     switch (result)     
    104.     {    
    105.         case MFMailComposeResultCancelled:    
    106.             msg = @"邮件发送取消";    
    107.             break;    
    108.         case MFMailComposeResultSaved:    
    109.             msg = @"邮件保存成功";    
    110.             [self alertWithTitle:nil msg:msg];    
    111.             break;    
    112.         case MFMailComposeResultSent:    
    113.             msg = @"邮件发送成功";    
    114.             [self alertWithTitle:nil msg:msg];    
    115.             break;    
    116.         case MFMailComposeResultFailed:    
    117.             msg = @"邮件发送失败";    
    118.             [self alertWithTitle:nil msg:msg];    
    119.             break;    
    120.         default:    
    121.             break;    
    122.     }    
    123.       
    124.     [self dismissModalViewControllerAnimated:YES];   
    125. }  
posted @ 2014-05-12 16:45  天牛  阅读(611)  评论(0编辑  收藏  举报