首页 > 代码库 > MFMailComposeViewController发送邮件

MFMailComposeViewController发送邮件

1.iPhone API已经提供了系统写邮件界面的接口,使用MFMailComposeViewController,用来显示界面.

2.项目中需要添加MessageUi.framework。头文件加入MFMailComposeViewControllerDelegate。#import <MessageUI/MessageUI.h>

 

- (void)viewDidLoad

{

  // 实例化按钮用来调用邮箱

  UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];

  button.frame = CGRectMake(0, 40, 320, 50);

  [button setTitle: @"Mail" forState: UIControlStateNormal];

  [button addTarget: self action: @selector(sendEmailAction) forControlEvents: UIControlEventTouchUpInside];

  [self.view addSubview: button];

 }

- (void)sendEmailAction

{

   Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));

    if (mailClass != nil)

    {

        if ([mailClass canSendMail])

        {

            [self displayComposerSheet ];

        }else{

            [self launchMailAppOnDevice];

        }

    }else {

        [self launchMailAppOnDevice];

    }

}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result

{

// [self dismissModalViewControllerAnimated:YES];

    [self dismissViewControllerAnimated:YES completion:nil];

  NSString*msg;

  switch (result) {

  case MessageComposeResultCancelled:

    msg = @"发送取消";

    break;

  case MessageComposeResultSent:

    msg = @"发送成功";

    break;

  case MessageComposeResultFailed:

    msg = @"发送失败";

    break;

    default:

    break;

}

    UIAlertView*alert = [[UIAlertView alloc] initWithTitle:nil

                                                   message:msg

                                                  delegate:nil

                                         cancelButtonTitle:@"关闭"

                                         otherButtonTitles:nil];

    [alert show];

}

//email界面,界面添加于window上

-(void)displayComposerSheet

{

    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];

        [picker setAccessibilityLanguage:@"Chinese"];

    if (!picker) {

          // 在设备还没有添加邮件账户的时候mailViewController为空,下面的present view controller会导致程序崩溃,这里要作出判断

          NSLog(@"设备还没有添加邮件账户");

          return;

    }

      picker.mailComposeDelegate = self;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0) {

        UIView *barBgV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];

        barBgV.backgroundColor = [UIColor colorWithRed:55/255.0 green:60/255.0 blue:100/255.0 alpha:1.0];

        UIImage *barBgImg = [Tool convertViewToImage:barBgV];

        [[picker navigationBar] setBackgroundImage:barBgImg forBarMetrics:UIBarMetricsDefault];

    }

 

// Set up recipients

      NSArray *toRecipients = [NSArray arrayWithObject:staff.email];

 

      [picker setToRecipients:toRecipients];

    

//    NSString *path = [[NSBundle mainBundle] pathForResource:@"Icon@2x.png"

//                                                     ofType:nil

//                                                inDirectory:nil];

//        NSData *myData = http://www.mamicode.com/[NSData dataWithContentsOfFile:path];

//        [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"icon"];

    [picker setMessageBody:nil isHTML:YES];

      [self presentViewController:picker animated:YES completion:^{

          picker.accessibilityElementsHidden = YES;

    }];

  // [self presentModalViewController:picker animated:YES];

   }

//跳转到系统email界面

-(void)launchMailAppOnDevice

{

    NSString *recipients = [NSString stringWithFormat:@"mailto:%@?&subject=",staff.email];

    

    NSString *email = recipients;

    email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

 

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];

}

#pragma mark - 代理

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error

{

      //关闭邮件发送窗口

      [self dismissViewControllerAnimated:YES completion:nil];

      NSString *msg;

    switch (result)

    {

  case MFMailComposeResultCancelled:

            msg = @"用户取消编辑邮件";

    break;

  case MFMailComposeResultSaved:

            msg = @"用户成功保存邮件";

    break;

  case MFMailComposeResultSent:

              msg = @"发送成功";

      break;

  case MFMailComposeResultFailed:

            msg = @"用户试图保存或者发送邮件失败";

    break;

    default:

    break;

}

    [[[UIAlertView alloc]initWithTitle:nil message:msg delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil, nil]show];

 

}

 

设置中文:

1.可以在项目的list属性文件中设置Localization native development region的属性值  为:China;

2.可以在调用MFMailComposeViewController的xib中设置Localization(方法如下:找到对应xib文件,右击该文件选择Get Infoà [General选项卡下单击{add Localization}])如中文简体{zh_CN}

3.或者在自己手机:设置-->通用--->多语言环境-->语言--->简体中文;

General--->International-->Language--->简体中文;