2012-10-27 109 views
0

我試圖從文本字段和圖像發送數據的電子郵件,它不起作用,請指教。這裏是我的代碼:如何從應用程序內發送電子郵件

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
if(buttonIndex==1) 
{ 

    Cocktails*c=[[Cocktails alloc]init]; 

    _arrTextField=[NSArray arrayWithObjects:_txtName,_txtIngredients,_txtPreper,_txtServe,_txtFrom ,nil]; 

    NSLog(@"send email"); 

    if ([MFMailComposeViewController canSendMail]) 
    { 
    NSMutableArray*recipients=[[NSMutableArray alloc]init]; 

    [recipients addObject:@"[email protected]"]; 
    MFMailComposeViewController *controller= [[MFMailComposeViewController alloc]init]; 
    controller.mailComposeDelegate= self; 
    [controller addAttachmentData:_imgDrink mimeType:@"image/png" fileName:@"Myimage"]; 
    [controller setSubject:@"my cocktail"]; 
    [controller setMessageBody: _arrTextField isHTML:NO]; 
    [controller setToRecipients:recipients]; 
    } 
    else 
    { 
     UIAlertView* alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:@"Your devise is not set up for Email" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:Nil, nil]; 

     [alert show]; 
     // [alert release]; 
    } 

回答

0

你永遠不會呈現郵件控制器。它需要像任何其他模式視圖控制器一樣呈現。

0

這裏,應該幫助:

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error 
{ 
    switch (result) 
    { 
     case MFMailComposeResultCancelled: 
      NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued."); 
      break; 
     case MFMailComposeResultSaved: 
      NSLog(@"Mail saved: you saved the email message in the drafts folder."); 
      break; 
     case MFMailComposeResultSent: 
      NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send."); 
      break; 
     case MFMailComposeResultFailed: 
      NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error."); 
      break; 
     default: 
      NSLog(@"Mail not sent."); 
      break; 
    } 

    // Remove the mail view 
    [self dismissModalViewControllerAnimated:YES]; 
} 


-(IBAction)sendCode:(id)sender { 
    if ([MFMailComposeViewController canSendMail]) 
    { 
     MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init]; 

     mailer.mailComposeDelegate = self; 

     [mailer setSubject:@"Your subject"]; 

     NSArray *toRecipients = [NSArray arrayWithObjects:@"[email protected]", nil]; 
     [mailer setToRecipients:toRecipients]; 

UIImage *myImage = [UIImage imageNamed:@"MyImage.png"]; 
      NSData *imageData = UIImagePNGRepresentation(myImage); 
      [mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"myimag 

e"]; 


     NSString *emailBody = 
     @"your body"; 


     [mailer setMessageBody:emailBody isHTML:NO]; 


     [mailer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; 
     [self presentModalViewController:mailer animated:YES]; 

    } 
    else { 
     { 
      UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Error" 
                  message:@"Your device doesnt support that action." 
                  delegate:nil 
                cancelButtonTitle:@"Okay" 
                otherButtonTitles:nil]; 

      [alert2 show]; 



     } 
    } 
} 

和.h文件中加入:

#import <UIKit/UIKit.h> 
#import <MessageUI/MessageUI.h> 

@interface Controller : UIViewController <MFMailComposeViewControllerDelegate> { 


} 

-(IBAction)send:(id)sender; 

@end 
+0

這應該是你的解決方案! – MasterRazer

+0

謝謝,但我不想看到電子郵件屏幕,我希望它自動發送 – user1763326

+0

ohh好吧我的錯我會尋找它 – MasterRazer

0

你不能發送電子郵件automaticaly因爲蘋果不會讓你做這樣的事情在用戶不知情。您必須出示presentModalView!

相關問題