문제

I have a very simple app to test sending email, but the email never arrives. I have included the MessageUI framework in the app, and implemented the MFMailComposeViewControllerDelegate as well. The two methods in the app are as follows:

- (IBAction)showEmail:(id)sender
{
    // Email Subject
    NSString *emailTitle = @"Test Email";
    // Email Content
    NSString *messageBody = @"iOS programming is so fun!";
    // To address
    NSArray *toRecipents = [NSArray arrayWithObject:@"email@address.com"];

    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
   [mc setSubject:emailTitle];
   [mc setMessageBody:messageBody isHTML:NO];
   [mc setToRecipients:toRecipents];

   // Present mail view controller on screen
   [self presentViewController:mc animated:YES completion:NULL];

}

and the delegate method:

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            break;
        default:
            break;
    }

    // Close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:NULL];
}

When I press the email button in my app the first method works perfectly and when I click the send button that is presented, the "Mail Sent" message appears in the log. The email simply never arrives.

Everything seems to work as advertised, with the exception of the email never arriving at its destination.

I am running this on the iPad not in the simulator, and I have a good network connection.

What am I missing?

도움이 되었습니까?

해결책

Well apparently this was an email configuration problem with the iPad itself. After a reboot of the device the above code works perfectly. I absolutely hate this kind of problem.

다른 팁

Use the canSendMail method. Use the following code to check if a device mail configuration is setup or not. Otherwise your app will crash.

if ([MFMailComposeViewController canSendMail])
{
    // Create and show the MailComposeViewController
}
else
{
    // Show No mail account set up on device.
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top