如何使用 MFMailComposerViewController 从自定义 UITableViewCell 发送邮件

How to send mail from a custom UITableViewCell using MFMailComposerViewController

我想在使用 MFMailComposeViewController 的自定义 UITableViewCell 中单击按钮时发送邮件。

感谢 Objective-C 中的答案。

.h 文件中的以下代码

#import <MessageUI/MFMailComposeViewController.h>

给代表<MFMailComposeViewControllerDelegate>

.m 文件中的以下代码

//Where you want to open dialog write below code

if([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
        mailCont.mailComposeDelegate = self; // Required to invoke mailComposeController when send

        [mailCont setSubject:@"Your Subject!"];
        [mailCont setToRecipients:[NSArray arrayWithObject:@"hello@test.com"]];
        [mailCont setMessageBody:@"Your Body" isHTML:NO];

        [self presentViewController:mailCont animated:YES completion:nil];
    }

//Delegate Method

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            //YOUR ACTION
            break;
        case MFMailComposeResultSent:
            //YOUR ACTION
            break;
        case MFMailComposeResultSaved:
            //YOUR ACTION
            break;
        case MFMailComposeResultFailed:
            //YOUR ACTION
            break;
        default:
            break;
    }
}

您可以通过此代码关闭视图 - [self dismissViewControllerAnimated:YES completion:nil];

确保您是在真实设备而不是模拟器中测试它,并且您在设备中配置了邮件 ID

#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

@interface ViewController ()<MFMailComposeViewControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)sendmail:(id)sender {

    if ([MFMailComposeViewController canSendMail])
    {
            MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
            mailer.mailComposeDelegate = self;
            [mailer setSubject:@"Subject"];
            NSArray *toRecipients = [NSArray arrayWithObjects:@"Recipients", nil];
            [mailer setToRecipients:toRecipients];
            NSString *emailBody = @"Body";
            [mailer setMessageBody:emailBody isHTML:NO];
            [self presentViewController:mailer animated:YES completion:nil];
    }
}

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{

    // Close the Mail Interface
    [self dismissViewControllerAnimated:NO completion:NULL];
}