p.m.a
p.m.a

Reputation: 45

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

I am using ObjectiveC and I want to send an email to a list of email addresses that I have in a sqlite database. The email array hold the addresses that I want to send the mail to. I show you my code

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

I obtain this error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM mf_isLegalCommentedEmailAddress]: unrecognized selector sent to instance 0x78e5e180'

Anyone can help me please? Thank you so much!

Upvotes: 2

Views: 510

Answers (3)

CW0007007
CW0007007

Reputation: 5681

First of all it should be:

NSArray *toRecipents = [NSArray arrayWithArray:emailArray];

But why not just do:

[mc setToRecipients:emailArray]; 

Assuming email array is a valid array object as we can't see from your code where/how you create it.

EDIT

You likely can't send an email from the simulator if there isn't an email account setup. Check for this with the following:

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

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

Upvotes: 3

Daniel Storm
Daniel Storm

Reputation: 18898

There's an issue with your array. This code works fine when sending to multiple recipients:

    //Email
-(void)mailButton {
    NSArray *emailArray = @[@"[email protected]", @"[email protected]", @"[email protected]", @"[email protected]", @"[email protected]"];

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

Upvotes: 0

Yuichiro Suzuki
Yuichiro Suzuki

Reputation: 29

toRecipents should be an array, which contains only one email address.

As below,

toRecipents = @[@"[email protected]"];

If, you want to send email some people, you should use setCcRecipients method.

Upvotes: -1

Related Questions