在 iPhone 上的 MessageUI 中使用 MFMailComposeViewController 从应用程序发送电子邮件

Posted

技术标签:

【中文标题】在 iPhone 上的 MessageUI 中使用 MFMailComposeViewController 从应用程序发送电子邮件【英文标题】:Sending email message from the app using MFMailComposeViewController in MessageUI on iPhone 【发布时间】:2011-04-25 09:03:46 【问题描述】:

我是一名软件开发人员,我正在为电子邮件制作应用程序,我有以下代码:

// Header file  

// importing the MessageUI framework  
#import <MessageUI/MessageUI.h>  

// adding the delegate functionality to the class (<MFMailComposeViewControllerDelegate>)  
@interface TutorialProjectViewController : UIViewController <MFMailComposeViewControllerDelegate>   

  

- (IBAction)pressTheMailButtonDudeFunction:(id)sender

// Implementation file  

- (IBAction)pressTheMailButtonDudeFunction:(id)sender   

    // allocatind new message composer window  
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];  

    // setting a delegate method to "self"  
    mc.mailComposeDelegate = self;  

    // pre-populating the message subject  
    [mc setSubject:@"Send me a message"];  

    // adding content of the message as a plain text  
    [mc setMessageBody:@"Send me a message is you like this tutorial :)" ishtml:NO];  

    // adding content of the message as an HTML  
    [mc setMessageBody:@"<p>Send me a message is you like this tutorial :)<p>" isHTML:YES];  

    // adding recipients  
    [mc setToRecipients:[NSArray arrayWithObjects:@"Fuerte <info@fuerte.cz>", @"info@xprogress.com", nil]];  

    // adding recipients for a send copy to (arrayWithObject or arrayWithObjects)  
    [mc setCcRecipients:[NSArray arrayWithObject:@"test@example.com"]];  

    // adding hidden recipients  
    [mc setBccRecipients:[NSArray arrayWithObject:@"test@example.com"]];  

    // adding image attachment  
    // getting path for the image we have in the tutorial project  
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Extra_Xcode_100x100" ofType:@"png"];  

    // loading content of the image into NSData  
    NSData *imageData = [NSData dataWithContentsOfFile:path];  

    // adding the attachment to he message  
    [mc addAttachmentData:imageData mimeType:@"image/png" fileName:@"Collection"];  

    // setting different than the default transition for the modal view controller  
    [mc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];  

    /* 
     Modal view controllers transitions: 

     UIModalTransitionStyleCoverVertical => pops up from the bottom, default transition 
     UIModalTransitionStyleCrossDissolve => fade on the screen 
     UIModalTransitionStyleFlipHorizontal => page flip 
     */  

    // displaying our modal view controller on the screen (of course animated has to be set on YES if you want to see any transition)  
    [self presentModalViewController:mc animated:YES];  

    // releasing the controller  
    [mc release];  
  

// delegate function callback  
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error   
    // switchng the result  
    switch (result)   
        case MFMailComposeResultCancelled:  
            NSLog(@"Mail send canceled.");  
            /* 
             Execute your code for canceled event here ... 
             */  
            break;  
        case MFMailComposeResultSaved:  
            NSLog(@"Mail saved.");  
            /* 
             Execute your code for email saved event here ... 
             */  
            break;  
        case MFMailComposeResultSent:  
            NSLog(@"Mail sent.");  
            /* 
             Execute your code for email sent event here ... 
             */  
            break;  
        case MFMailComposeResultFailed:  
            NSLog(@"Mail send error: %@.", [error localizedDescription]);  
            /* 
             Execute your code for email send failed event here ... 
             */  
            break;  
        default:  
            break;  
      
    // hide the modal view controller  
    [self dismissModalViewControllerAnimated:YES];  
  

而且我没有得到正确的答案...这是正确的代码吗?

【问题讨论】:

它显示了太多我无法理解的错误。它显示“邮件发送已取消。”、“邮件已保存。”、“邮件已发送。”同时 请详细说明您的问题。 @Nick Weaver:它显示了太多我无法理解的错误。它显示“邮件发送已取消。”、“邮件已保存。”、“邮件已发送。”同时... 我也将 MessageUI 框架“导入”到我的项目中,但我仍然无法得到答案 请详细说明您面临的确切问题。 【参考方案1】:
    确保将 MessageUI 框架包含到您的 ios 项目中。在 Xcode 4 中,您可以通过在左栏中选择您的项目来包含该框架。然后选择选项卡“构建阶段”。在这里,您可以单击“Link Binary With Libraries”左侧的箭头,您会看到已包含在您的应用程序中的框架列表。如果 MessageUI.framework 缺失 - 只需在此处添加即可。 您发布的代码看起来像是截断的完整教程代码...所以只使用您需要的代码...并逐步添加更多功能。这样,您将看到添加错误代码行的位置。可能您的 app bundle 中没有图片“Extra_Xcode_100x100.png”。

所以,这是一个“最小”的 MFMailComposeViewController:


- (IBAction)showMinimalModalMailView:(id)sender 
    // get a new new MailComposeViewController object 
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];  

    // his class should be the delegate of the mc  
    mc.mailComposeDelegate = self;  

    // set a mail subject ... but you do not need to do this :)
    [mc setSubject:@"This is an optional mail subject!"];  

    // set some basic plain text as the message body ... but you do not need to do this :)
    [mc setMessageBody:@"This is an optional message body plain text!" isHTML:NO];  

    // set some recipients ... but you do not need to do this :) 
    [mc setToRecipients:[NSArray arrayWithObjects:@"first.address@test.com", @"second.address@test.com", nil]];  

    // displaying our modal view controller on the screen with standard transition  
    [self presentModalViewController:mc animated:YES];  

    // be a good memory manager and release mc, as you are responsible for it because your alloc/init
    [mc release];  


【讨论】:

【参考方案2】:

我遇到了同样的问题,每次在发送消息时运行应用程序都会崩溃;我发现如果我删除了

[mc setToRecipients:[NSArray arrayWithObjects:@"first.address@test.com", @"second.address@test.com", nil]];

它工作正常,只是询问我的电子邮件地址。

【讨论】:

尝试以 [NSNull null] 结尾的数组,因为 nil 不能是预期对象的结尾。这是弧前的事情【参考方案3】:

要从应用程序发送电子邮件,您的设备应配置为电子邮件服务。

// importing the MessageUI framework  
#import <MessageUI/MessageUI.h>  

// adding the delegate functionality to the class (<MFMailComposeViewControllerDelegate>)  
@interface ViewController : UIViewController <MFMailComposeViewControllerDelegate>   



- (IBAction)sendEMailClick:(id)sender 

    //check mail service is configure to your device or not.
    if ([MFMailComposeViewController canSendMail]) 

        // get a new new MailComposeViewController object
        MFMailComposeViewController * composeVC = [MFMailComposeViewController new];

        // his class should be the delegate of the composeVC
        [composeVC setDelegate:self];

        // set a mail subject ... but you do not need to do this :)
        [composeVC setSubject:@"This is an optional mail subject!"];

        // set some basic plain text as the message body ... but you do not need to do this :)
        [composeVC setMessageBody:@"This is an optional message body plain text!" isHTML:NO];

        // set some recipients ... but you do not need to do this :)
        [composeVC setToRecipients:[NSArray arrayWithObjects:@"first.address@test.com", @"second.address@test.com", nil]];

        // Present the view controller modally.

        [self presentViewController:composeVC animated:true completion:nil];
     else 

        NSLog(@"Mail services are not available or configure to your device");
    

在电子邮件发送或取消后点击MFMailComposeViewControllerDelegateMFMailComposeViewController 委托方法被调用,因此您可以在此处查看电子邮件发送状态。

#pragma mark - MFMailComposeViewControllerDelegate Methode.
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(nullable NSError *)error 

    switch (result) 
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");

            break;

        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");

            break;

        case MFMailComposeResultSent:
            NSLog(@"Mail sent");

            break;

        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@",error.description);

            break;
    

    // Dismiss the mail compose view controller.
    [controller dismissViewControllerAnimated:true completion:nil];


【讨论】:

以上是关于在 iPhone 上的 MessageUI 中使用 MFMailComposeViewController 从应用程序发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

通过 MessageUI 在应用中发送短信

MFMailComposeViewController发送邮件的实例

在 Sprite Kit 中以编程方式发送邮件

发送短信

iOS app发送短信

iOS 发短信