Search code examples
iphoneemailmfmailcomposeviewcontroller

Application crashing on email in device


I have added a function of "Email" in my iPhone application and it is crashing on iPhone but working on simulator. Please guide me why it is crashing on Device

this is the code

-(IBAction)sendMail{
    MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
    controller.mailComposeDelegate = self;
    [controller setSubject:@"Contact"];

    [controller setToRecipients:[NSArray arrayWithObject:@"[email protected]"]];
    [controller setMessageBody:@"" isHTML:NO];
    [self presentModalViewController:controller animated:YES];
    [controller release];
}

- (void)mailComposeController:(MFMailComposeViewController*)controller  
          didFinishWithResult:(MFMailComposeResult)result 
                        error:(NSError*)error;
{
    if (result == MFMailComposeResultSent) {
        NSLog(@"It's away!");
    }
    [self dismissModalViewControllerAnimated:YES];
}

Solution

  • use this code its work fine.

    -(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];  
    }