2010-11-26 22 views
5

私のアプリからメールを送信しようとするのが苦労しています。 は私が(http://icodeblog.com/2009/11/18/iphone-coding-tutorial-in-application-emailing/)MFMailComposeViewControllerのアプリからメールを送信する際の問題

 
-(void)sendEmail:(id)sender 
{ 
    MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init]; 
    mail.mailComposeDelegate = self; 
    if ([MFMailComposeViewController canSendMail]) { 
      //Setting up the Subject, recipients, and message body. 
     [mail setToRecipients:[NSArray arrayWithObjects:@"[email protected]",nil]]; 
     [mail setSubject:@"Subject of Email"]; 
     [mail setMessageBody:@"Message of email" isHTML:NO]; 
      //Present the mail view controller 
     [self presentModalViewController:mail animated:YES]; 
    } 
     //release the mail 
    [mail release]; 
} 
    //This is one of the delegate methods that handles success or failure 
    //and dismisses the mail 
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{ 
    [self dismissModalViewControllerAnimated:YES]; 
    if (result == MFMailComposeResultFailed) { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message Failed!" message:@"Your email has failed to send" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
    } 
} 

をiCodeBlogから、このコードを試してみましたそれは、電子メールを送信していないと言いますエラーが発生しますが、受信トレイにはメールが届きません。 別のメールアカウントに送信しようとしましたが、別のアカウントからも送信しようとしましたが、エラーは発生しませんでしたが、メールを受け取ることはありません。 アイデア

それが重要な場合、私はへの入力を開始するとき、私はデバッガコンソールにこのメッセージが表示されます:メール

DAを| /tmp/DAAccountsLoading.lockでロックファイルを開けませんでした。 ===============================私たちは、とにかくアカウントをロードしますが、悪いことは

が起こることがあり====== EDIT ===================== 私はそれを実現しましたこれらのメールはすべてMail.appの送信トレイに送られました。送信をクリックすると自動的に送信されませんか?そうでない場合、ユーザーがMFMailComposeViewの[送信]ボタンを押したときに、それらを送信させるにはどうすればよいですか?あるいは、Mail.appを呼び出してそれらの電子メールを送信することもできます。

答えて

6

使用このコードこれは間違いなく動作します:

-(IBAction)send{ 

     [self callMailComposer]; 
    } 

    -(void)callMailComposer{ 

     Class mailClass = (NSClassFromString(@"MFMailComposeViewController")); 
     if (mailClass != nil) 
     { 
     // We must always check whether the current device is configured for sending emails 
      if ([mailClass canSendMail]) 
       [self displayComposerSheet]; 
      else 
       [self launchMailAppOnDevice]; 
     } 

     else 
     { 
      [self launchMailAppOnDevice]; 
     } 
    } 


    #pragma mark - 
    #pragma mark Compose Mail 
    #pragma mark 

    // Displays an email composition interface inside the application. Populates all the Mail fields. 
    -(void)displayComposerSheet{ 

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


     picker.mailComposeDelegate = self; 
     NSString *tosubject [email protected]""; 
     [picker setSubject:tosubject]; 


     // Set up recipients 
     [picker setCcRecipients:nil]; 
     [picker setBccRecipients:nil]; 

     [picker setToRecipients:nil]; 



     [picker setMessageBody:strNewsLink isHTML:NO]; 

     [self presentModalViewController:picker animated:YES]; 

     if(picker) [picker release]; 
     if(picker) picker=nil; 

    } 


    // Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation. 

     - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{  
    NSString* alertMessage; 
    // message.hidden = NO; 
    // Notifies users about errors associated with the interface 
    switch (result) 
    { 
    case MFMailComposeResultCancelled: 
     alertMessage = @"Email composition cancelled"; 
     break; 
    case MFMailComposeResultSaved: 
     alertMessage = @"Your e-mail has been saved successfully"; 

     break; 
    case MFMailComposeResultSent: 
     alertMessage = @"Your email has been sent successfully"; 

     break; 
    case MFMailComposeResultFailed: 
     alertMessage = @"Failed to send email"; 

     break; 
    default: 
     alertMessage = @"Email Not Sent"; 

     break; 
    } 

    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"My app name" 
                 message:alertMessage 
                delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    [alertView show]; 
    [self dismissModalViewControllerAnimated:YES]; 
} 


    #pragma mark 
    #pragma mark Workaround 
    #pragma mark 
    // Launches the Mail application on the device. 

     -(void)launchMailAppOnDevice{ 

     NSString *recipients = @"mailto:?cc=&subject="; 
     NSString *body = @"&body="; 
     NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body]; 
     email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]]; 

    } 
+0

コードが混乱しているように見えますが、私は多くのアプリケーションでメールコンポーザーを実装していますが、これは絶対にうまく動作します – Aditya

+0

あなたのコードはいくつか役立ちました。ありがとう! –

+0

コードが見当たらずに見える場合、コードとして表示されていない行の前に必要なスペースが4つありません。 –

0

Btwこのコードをシミュレータで実行しようとすると、デバイスでこの機能をテストすると、正常に送信された電子メールが表示されますが、電子メールは送信されません。

おかげ

+0

はい、私は私のデバイス上でそれをテストしていますを確認することでした。それは電子メールが送信されたが、それは私の受信トレイにはありません... –

1

はここに古いスレッドを掘るMFMAilComposerViewControllerを扱うとき...多分私は電子メールを送信しないこと、将来の不満を保存することができます。

私のアプリは私の5つのテストデバイスのうちの4つにメールを送信し、その違いが5番目のものであることを理解できませんでした。問題はGmailの設定が間違っていたことです。 MFMailComposerViewControllerエラートラップメソッドはエラーを返さず、電子メールを送信しませんでした。問題は不正な電子メールアドレスまたは電子メールパスワードでした。デバイスのユーザーに電子メールのログオン情報を要求し、Gmailアカウントにログオンしようとするとエラーが表示されました。仮定は、はい、私の悪い、canSendMailは...有効な電子メールアカウントの

0

スウィフト4バージョン

let myController:MFMailComposeViewController = MFMailComposeViewController() 
    if MFMailComposeViewController.canSendMail() { 
     myController.mailComposeDelegate = self 
     myController.setToRecipients(["[email protected]"]) 
     self.present(myController, animated: true, completion: nil) 
    }