Cannot send email in app using MFMailComposeViewController

删除回忆录丶 提交于 2020-01-04 10:58:10

问题


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

回答1:


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:@"fisrtMail@example.com", @"secondMail@example.com", 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];
}



回答2:


because your mail app in iphone not auth. Go to preferences -> Mail (or simply open Mail app) and auth from google or another service and you can sent emails through MFMailComposeViewController. (this in on real iPhone - I don't try it on simulator)



来源:https://stackoverflow.com/questions/11863527/cannot-send-email-in-app-using-mfmailcomposeviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!