从 Objective C 中的数据库向 arraylist 收件人发送邮件

Send mail to an array list recipients from a database in ObjectiveC

我正在使用 ObjectiveC,我想向 sqlite 数据库中的电子邮件地址列表发送电子邮件。电子邮件数组包含我要将邮件发送到的地址。 我给你看我的代码

- (void)sendEmailButtonClicked: (id)sender {

 // Email Subject
    NSString *emailTitle = @"Test Email";
    // Email Content
    NSString *messageBody = @"iOS programming is so fun!";
    // To address
    NSArray *toRecipents = [NSArray  arrayWithObject:emailArray];

NSLog(@"What are the emais %@",toRecipents);
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];

    mc.mailComposeDelegate = self;
    [mc setSubject:emailTitle];
    [mc setToRecipients:toRecipents];
    [mc setMessageBody:messageBody isHTML:NO];


    // Present mail view controller on screen
    [self presentViewController:mc animated:YES completion:NULL];
}





- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(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 localizedDescription]);
            break;
        default:
            break;
    }


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

我收到此错误:*** 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因:'-[__NSArrayM mf_isLegalCommentedEmailAddress]:无法识别的选择器发送到实例 0x78e5e180'

有人可以帮助我吗?非常感谢!

toRecipents应该是一个数组,其中只包含一个电子邮件地址。

如下,

toRecipents = @[@"foo@example.com"];

如果,你想给一些人发邮件,你应该使用setCcRecipients方法。

首先应该是:

NSArray *toRecipents = [NSArray arrayWithArray:emailArray];

但为什么不这样做:

[mc setToRecipients:emailArray]; 

假设电子邮件数组是一个有效的数组对象,因为我们无法从您的代码中看到 where/how 您创建了它。

编辑

如果没有设置电子邮件帐户,您可能无法从模拟器发送电子邮件。使用以下内容检查:

if ([MFMailComposeViewController canSendMail]) {
    //Do your email stuff
}

else {
    //Present an error etc ...
}

你的数组有问题。此代码在发送给多个收件人时工作正常:

    //Email
-(void)mailButton {
    NSArray *emailArray = @[@"me@gmail.com", @"you@gmail.com", @"him@gmail.com", @"her@gmail.com", @"everyone@gmail.com"];

if ([MFMailComposeViewController canSendMail]) {
    NSString *subject = @"Subject";
    NSString *messageBody = [NSString stringWithFormat:@"Message Body"];
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
    [mc setSubject:subject];
    [mc setMessageBody:messageBody isHTML:NO];
    [mc setToRecipients:emailArray];
    [self presentViewController:mc animated:YES completion:NULL];
}
else {
    UIAlertView *emailError = [[UIAlertView alloc] initWithTitle:@"Email Unavailable"
                                                         message:@"Sorry, were unable to find an email account on your device.\nPlease setup an account in your devices settings and try again."
                                                        delegate:self
                                               cancelButtonTitle:@"Close"
                                               otherButtonTitles:nil];
    [emailError show];
}
}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    switch (result) {
        case MFMailComposeResultCancelled:
            break;
        case MFMailComposeResultSaved:
            break;
        case MFMailComposeResultSent:
            break;
        case MFMailComposeResultFailed:
            break;
        default:
            break;
    }
    [self dismissViewControllerAnimated:YES completion:NULL];
}