Search code examples
iosmfmailcomposeviewcontroller

Cannot send email in app using MFMailComposeViewController


I have an app which uses MFMailComposeViewController to send documents through email. I read somewhere that I need to enable at least one email so that the method [MFMailComposeViewController canSendEmail] will return YES and email the document. However, whenever I tap the email button, all it does is return to the previous view.

I checked the code and [MFMailComposeViewController canSendEmail] returns NO. Can anyone tell me why is this happening?

Here is the code:

- (void)sendEmail
{
   if ([MFMailComposeViewController canSendMail] == NO) return;
   NSURL *fileURL = document.fileURL; NSString *fileName = document.fileName;

   NSData *attachment = [NSData dataWithContentsOfURL:fileURL options:(NSDataReadingMapped|NSDataReadingUncached) error:nil];

        if (attachment != nil) 
        {
            MFMailComposeViewController *mailComposer = [MFMailComposeViewController new];

            [mailComposer addAttachmentData:attachment mimeType:@"application/pdf" fileName:fileName];

            [mailComposer setSubject:fileName]; 

            mailComposer.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
            mailComposer.modalPresentationStyle = UIModalPresentationFormSheet;

            mailComposer.mailComposeDelegate = self; 

            [self presentModalViewController:mailComposer animated:YES];

            [mailComposer release];
        }
}

Solution

  • First add and Import the MessageUI Framework

    #import <MessageUI/MessageUI.h>
    

    and Declare the MFMaileComposeViewControllerDelegate

    @interface MailViewController : UIViewController <MFMailComposeViewControllerDelegate>
    

    Write this code for sending the mail

    - (IBAction)openMail:(id)sender 
    {
    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
    
        mailer.mailComposeDelegate = self;
    
        [mailer setSubject:@"xyz"];
    
        NSArray *toRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];
        [mailer setToRecipients:toRecipients];
    
    
        NSData *pdfdata = [NSData dataWithContentsOfURL:"Your URL"]
    
        [mailController addAttachmentData:pdfData
                                 mimeType:@"application/pdf"
                                 fileName:@"file.pdf"];
    
        NSString *emailBody = @"xyz";
    
        [mailer setMessageBody:emailBody isHTML:NO];
    
        [self presentModalViewController:mailer animated:YES];
    
        [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 also write the delegate method of MFMailComposeViewControllerDelegate

    - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
    {   
        switch (result)
        {
            case MFMailComposeResultCancelled:
                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:
                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
        [self dismissModalViewControllerAnimated:YES];
    }