2011-02-09 83 views

回答

3

在您的视图控制器头。

然后:

-(void)showMailPanel { 
    MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init]; 

     // only on iOS < 3 
    //if ([MFMailComposeViewController canSendMail] == NO) 
    // [self launchMailApp]; // you need to 

    mailComposeViewController.mailComposeDelegate = self; 
    [mailComposeViewController setToRecipients:[NSArray arrayWithObjects:@"email address1",@"email address 2",nil]]; 
    [mailComposeViewController setSubject:@"your subject"]; 
    [mailComposeViewController setMessageBody:@"your body" isHTML:YES]; 
    mailComposeViewController.delegate = self; 
    [self.navigationController presentModalViewController:mailComposeViewController animated:YES]; 

    [mailComposeViewController release]; 

} 


- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{ 
    // Notifies users about errors associated with the interface 
    switch (result) 
    { 
     case MFMailComposeResultCancelled: 
      NSLog(@"Result: canceled"); 
      break; 
     case MFMailComposeResultSaved: 
      NSLog(@"Result: saved"); 
      break; 
     case MFMailComposeResultSent: 
      NSLog(@"Result: sent"); 
      break; 
     case MFMailComposeResultFailed: 
      NSLog(@"Result: failed"); 
      break; 
     default: 
      NSLog(@"Result: not sent"); 
      break; 
    } 
    [controller dismissModalViewControllerAnimated:YES]; 
} 

-(void)launchMailApp { 
{ 
    NSString *recipients = @"dest_email"; 
    NSString *email = [NSString stringWithFormat:@"%@%@", recipients, subject]; 
    email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]]; 
} 
+0

这只用于发送静态邮件ID,但我需要动态发送。例如,应用程序运行时可以输入邮件标识主题和邮件内容,然后我想发送。 – Senthilkumar 2011-02-09 13:14:23

2

都需要这样的东西:

//Import this in your .h file 
#import <MessageUI/MessageUI.h> 

...

MFMailComposeViewController *mailController = [[[MFMailComposeViewController alloc] init] autorelease]; 
    mailController.mailComposeDelegate = self; 
    [mailController setSubject:[NSString stringWithFormat:@"Report a problem - #%@", yourUserID]]; 
    [mailController setToRecipients:[NSArray arrayWithObject:@"[email protected]"]]; 
    [self presentModalViewController:mailController animated:YES]; 

此外,父视图控制器(委托)需要符合MFMailComposeViewControllerDelegate协议:

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { 
     //Extra implementation here... 
     [self dismissModalViewControllerAnimated:YES]; 
    } 
+0

这只用于发送静态邮件ID,但我需要动态发送。例如,应用程序运行时可以输入邮件标识主题和邮件内容,然后我想发送。 – Senthilkumar 2011-02-09 13:15:05

相关问题