Search code examples
objective-ciosxcodeemailmfmailcomposeviewcontroller

Unable to send email through my app


I am new to iphone development. I am building an app through which i can send the email.I am using the following code

- (IBAction)btnsendPressAction:(id)sender {

    if([MFMailComposeViewController canSendMail]){

        MFMailComposeViewController *mailer=[[MFMailComposeViewController alloc]init];
        mailer.mailComposeDelegate=self;
        [mailer setSubject:@"A Message for testing"];
      NSArray *toRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];
        [mailer setToRecipients:toRecipients];

        UIImage *image=[UIImage imageNamed:@"logo.png"];
        NSData *imageData=UIImagePNGRepresentation(image);
        [mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"logo.png"];
        NSString *emailBody=@"This is test email";
        [mailer setMessageBody:emailBody isHTML:NO];
        [self presentModalViewController:mailer animated:YES];
        mailer.modalPresentationStyle = UIModalPresentationPageSheet ;


    }
    else{

        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"failure" message:@"Alert" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
}

My problem is mailComposeController delegate is not getting called and I am not able to send email through my app.I went through many links , but every where I found the same method. Can anyone help me on this ?


Solution

  • You won't be able to send email from Simulator. You can only check this functionality on iOS device with any account configured in Mail application.