2011-02-09 5 views
0

私のアプリからメールを送信しようとしています。私は(動的に)受信者ID、ccid、サブとメッセージを入力したいと思います。あなたのビューコントローラヘッダー内のiOS 3+ インポートの#import上iPhoneアプリからメールを送るには?

おかげ

Senthilkumar

答えて

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

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