Iphone – Display the success alert message when mail send from MFMailComposeViewController in iPhone

emailiphonemfmailcomposeviewcontroller

I need to display the alert message when the user send successfully mail from the iPhone using MFMailComposeViewController.

I tried with didFinishWithResult delegate but it is calling for both send and cancel then how we can determine we successfully send the message?

Best Answer

Try this code

-(IBAction)Btn_EmailPressed:(id)sender{
    if (![MFMailComposeViewController canSendMail]) {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Email cannot be configure." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }else {
        picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate=self;
        [picker setToRecipients:nil];
                [picker setSubject:@"Email"];
                [picker setMessageBody:nil isHTML:NO];
                NSArray *toRecipients = [[NSArray alloc] initWithObjects:lblSite.text,nil];
                [picker setToRecipients:toRecipients];
                [self presentModalViewController:picker animated:YES];
            }
}


- (void)mailComposeController:(MFMailComposeViewController*)mailController didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    NSString *msg1;
    switch (result)
    {
        case MFMailComposeResultCancelled:
            msg1 =@"Sending Mail is cancelled";
            break;
        case MFMailComposeResultSaved:
            msg1=@"Sending Mail is Saved";
            break;
        case MFMailComposeResultSent:
            msg1 =@"Your Mail has been sent successfully";
            break;
        case MFMailComposeResultFailed:
            msg1 =@"Message sending failed";
            break;
        default:
            msg1 =@"Your Mail is not Sent";
            break;
    }
    UIAlertView *mailResuletAlert = [[UIAlertView alloc]initWithFrame:CGRectMake(10, 170, 300, 120)];
    mailResuletAlert.message=msg1;
    mailResuletAlert.title=@"Message";
    [mailResuletAlert addButtonWithTitle:@"OK"];
    [mailResuletAlert show];
    [mailResuletAlert release];
    [self dismissModalViewControllerAnimated:YES];  
}
Related Topic