Search code examples
iosobjective-cmfmailcomposeviewcontroller

MFMailViewComposer does not return an error when writing a wrong target address format


When I try to send an email to a non-existing address (for example, "hello"), MFMail Composer should return MFMailComposeResultFailed, I guess, but this does not occur, and the message seems to be sent correctly (obviously, it is not sent). I tried the same with "Mail" and in this case, "Mail" answers that there has been an error sending the message, however, when I do the same in my app, there are no errors. How can I fix this?

This is the code I use when I get into the mail screen:

if ([MFMailComposeViewController canSendMail]) {
    MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc]init];
    mailer.mailComposeDelegate = self;
    [mailer setSubject:@"Notification"];
    User *user = [user_array objectAtIndex:0];
      NSArray *toRecipients = [NSArray arrayWithObjects:user.eMail, nil];
      [mailer setToRecipients:toRecipients];

    [mailer setMessageBody:self.label1.text isHTML:NO];
    [self presentViewController:mailer animated:YES completion:nil];
    [mailer release];
}
else
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
                                                    message:@"Your device doesn't support the composer sheet"
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles: nil];
    [alert show];
    [alert release];
}

}

and this is the code for the responder:

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
NSString *string = @"";
switch (result)
{
    case MFMailComposeResultCancelled:
        string = @"cancelled";
        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:
        string = @"sent";
        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
int abc = MFMailComposeErrorCodeSendFailed;
NSLog(@"ERROR LOG %i", abc);
[self dismissViewControllerAnimated:YES completion:nil];
if ([string isEqualToString:@"cancelled"]) {
    ViewController *second = [self.storyboard instantiateViewControllerWithIdentifier:@"Notif_detail"];
    [second setString1:self.string1];
    [second setString2:self.string2];
    [second setString3:self.string3];
    [second setString4:self.string4];
    [self.navigationController pushViewController:second animated:YES];
}
else if ([string isEqualToString:@"sent"]){

    ViewController *second = [self.storyboard instantiateViewControllerWithIdentifier:@"View"];
   [self.navigationController pushViewController:second animated:YES];
}

}

Thanks in advance!


Solution

  • I guess there is actually no error when didFinishWithResult gets triggered, because the message is queued and is about to be sent. The error occurs at a later point of time when you get response from the mail server (smtp code) due to not delivered message.