MFMailComposeViewController not populating recipients in ios 5

2.2k views Asked by At

I'm trying to send mail to list of email array that I receive from database, when I send the recipient list gets populated in iOS 7 but when I tried in iOS 5 the recipient list doesn't get populated. Any Idea why? This is my mail function

-(void)sendEmailToContacts:(NSArray *)fList withText:(NSString *)emailText withTag:(NSInteger )tag
{
    if([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
        mailComposer.view.tag=tag;
        NSString *htmlBody =[NSString stringWithFormat:@"<a href=\"%@\">%@</a>",_currentAdd.contentUrl,addtext];
        [mailComposer setMessageBody:htmlBody isHTML:YES];
        [mailComposer setSubject:_currentMail.subject];
        mailComposer.mailComposeDelegate = self;
        [mailComposer setToRecipients:fList];
        [self presentViewController:mailComposer animated:YES completion:nil];
    }
    else
    {
       NSLog(@"Device is unable to send email in its current state.");
    }
}

My fList (recipient list) is an NSArray, this is a sample output of my fList

(
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]"
)
3

There are 3 answers

0
Francis F On BEST ANSWER
-(void)sendEmailToContacts:(NSArray *)fList withText:(NSString *)emailText withTag:(NSInteger )tag
{
    if([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
        //mailComposer.view.tag=tag;
        NSString *htmlBody =[NSString stringWithFormat:@"<a href=\"%@\">%@</a>",_currentAdd.contentUrl,addtext];
        [mailComposer setMessageBody:htmlBody isHTML:YES];
        [mailComposer setSubject:_currentMail.subject];
        mailComposer.mailComposeDelegate = self;
        [mailComposer setToRecipients:fList];
        [self presentViewController:mailComposer animated:YES completion:nil];
    }
    else
    {
       NSLog(@"Device is unable to send email in its current state.");
    }
}

Apparently the issue was with setting tag if I try to set the tag before setToRecipients line it will not show the recipients list in iOS 5, it will work if the setting tag line is commented out or set after setToRecipients.

10
Bhavesh Lakum On

Try this one.

        NSArray *fList = [NSArray arrayWithObjects:@"[email protected]",@"[email protected]",@"[email protected]",@"[email protected]",@"[email protected]", nil];

        MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
        mailComposer.view.tag=tag;
        NSString *htmlBody =[NSString stringWithFormat:@"<a href=\"%@\">%@</a>",_currentAdd.contentUrl,addtext];
        [mailComposer setMessageBody:htmlBody isHTML:YES];
        mailComposer.mailComposeDelegate = self;
        [mailComposer setSubject:_currentMail.subject];
        mailComposer.delegate = self;
        [mailComposer setToRecipients:fList];
        [self presentViewController:mailComposer animated:YES completion:nil];
1
codercat On

Recipients are expected as immutable array. check your array type

  NSArray *usersTo = [NSArray arrayWithObject: @"[email protected]"];
    [mailComposer setToRecipients:usersTo];