5

アプリからメールを送信するのに苦労しています。私はiCodeBlogからこのコードを試しました(http://icodeblog.com/2009/11/18/iphone-coding-tutorial-in-application-emailing/

-(void)sendEmail:(id)sender
{{
    MFMailComposeViewController * mail = [[MFMailComposeViewController alloc] init];
    mail.mailComposeDelegate = self;
    if([MFMailComposeViewController canSendMail]){
            //件名、受信者、メッセージ本文を設定します。
        [mail setToRecipients:[NSArray arrayWithObjects:@ "myEmail@email.com"、nil]];
        [メールsetSubject:@"メールの件名"];
        [mail setMessageBody:@ "Message of email" isHTML:NO];
            //メールビューコントローラを表示します
        [自己presentModalViewController:メールアニメーション:はい];
    }
        //メールを解放します
    [メールリリース];
}
    //これは成功または失敗を処理するデリゲートメソッドの1つです
    //そしてメールを却下します
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{{
    [自己dismissModalViewControllerAnimated:YES];
    if(result == MFMailComposeResultFailed){
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@ "メッセージが失敗しました!" メッセージ:@ "メールの送信に失敗しました"delegate:self cancelButtonTitle:@ "Dismiss" otherButtonTitles:nil];
        [アラートショー];
        [アラートリリース];
    }
}

メールを送信し、エラーは発生しないと表示されますが、受信トレイにメールが届きません。別のメールアカウントに送信してみましたが、別のアカウントからも送信してみましたが、エラーは発生しませんが、メールが届きません。何か案は?

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

DA|/tmp/DAAccountsLoading.lockにあるロックファイルを開くことができませんでした。とにかくアカウントをロードしますが、悪いことが起こる可能性があります

=====編集======

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

4

4 に答える 4

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 =@"";
        [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]];

    }
于 2010-11-26T13:38:02.393 に答える
1

ここで古いスレッドを掘り起こします...電子メールを送信しないMFMAilComposerViewControllerを処理するときに、将来のフラストレーションを節約できるかもしれません。

私のアプリは5つのテストデバイスのうち4つでメールを送信し、5日の違いが何であるか理解できませんでした。問題は、Gmailアカウントが正しく設定されていないことでした。MFMailComposerViewControllerエラートラップメソッドはエラーを返すことはなく、電子メールを送信しなかっただけです。問題は、メールアドレスまたはメールパスワードが正しくないことでした。デバイスユーザーにメールログオン情報を尋ねることでこれを発見し、Gmailアカウントにログオンしようとするとエラーアラートが表示されました。はい、私の悪いことですが、canSendMailが有効な電子メールアカウントをチェックするという仮定がありました...

于 2015-06-06T21:26:40.013 に答える
0

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

ありがとう

于 2010-11-26T13:32:01.427 に答える
0

Swift4バージョン

    let myController:MFMailComposeViewController = MFMailComposeViewController()
    if MFMailComposeViewController.canSendMail() {
        myController.mailComposeDelegate = self
        myController.setToRecipients(["example@example.com"])
        self.present(myController, animated: true, completion: nil)
    }
于 2018-01-18T07:18:38.577 に答える