1.加入邮箱的框架
#import <MessageUI/MFMailComposeViewController.h>
#import <MessageUI/MessageUI.h>
2.添加委托
@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate>
3.实现代码
- (void)alertWithTitle:(NSString *)title msg:(NSString *)msg
{
if (title && msg)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:msg
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (IBAction)OnButtonWriteLog:(id)sender
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy_MM_dd_HH_mm"]; // 设置格式为年-月-日 时:分:秒:毫秒
NSString *timeStr = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
NSArray *userPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePackageName = [userPaths objectAtIndex:0];
NSString* fileNamePackageRes = [[NSString alloc] initWithFormat:@"%@/%@_%@",filePackageName,timeStr,@"log.txt"];
NSString* strLogData = [[NSString alloc]initWithFormat:@"my is log time:%@",timeStr];
[strLogData writeToFile:fileNamePackageRes atomically:NO encoding:NSUTF8StringEncoding error:nil];
[_logName release];
_logName = [[NSString alloc ]initWithString:fileNamePackageRes];
[fileNamePackageRes release];
[self alertWithTitle:@"提示" msg:@"写入成功"];
}
- (IBAction)OnButtonSendMailToMe:(id)sender {
NSString *logData = [[NSString alloc]initWithContentsOfFile:_logName encoding:NSUTF8StringEncoding error:nil];
if (logData)
{
MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
if(mailCompose)
{
//设置代理
[mailCompose setMailComposeDelegate:self];
NSArray *toAddress = [NSArray arrayWithObject:@"98zg@sina.cn"];
NSArray *ccAddress = [NSArray arrayWithObject:@"17333245@qq.com"];;
NSString *emailBody = @"<H1>日志信息</H1>";
//设置收件人
[mailCompose setToRecipients:toAddress];
//设置抄送人
[mailCompose setCcRecipients:ccAddress];
//设置邮件内容
[mailCompose setMessageBody:emailBody isHTML:YES];
NSData* pData = [[NSData alloc]initWithContentsOfFile:_logName];
//设置邮件主题
[mailCompose setSubject:@"这里是主题"];
//设置邮件附件{mimeType:文件格式|fileName:文件名}
[mailCompose addAttachmentData:pData mimeType:@"txt" fileName:@"日志.txt"];
//设置邮件视图在当前视图上显示方式
[self presentModalViewController:mailCompose animated:YES];
}
[mailCompose release];
return;
}
[logData release];
[self alertWithTitle:@"提示" msg:@"请先点击写入日志按钮"];
}
- (void)dealloc
{
[_logName release];
[super dealloc];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
NSString *msg;
switch (result)
{
case MFMailComposeResultCancelled:
msg = @"邮件发送取消";
break;
case MFMailComposeResultSaved:
msg = @"邮件保存成功";
[self alertWithTitle:nil msg:msg];
break;
case MFMailComposeResultSent:
msg = @"邮件发送成功";
[self alertWithTitle:nil msg:msg];
break;
case MFMailComposeResultFailed:
msg = @"邮件发送失败";
[self alertWithTitle:nil msg:msg];
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}