2

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

(
    "john@gmail.com",
    "mary@gmail.com",
    "akhil@gmail.com",
    "tester@gmail.com"
)
Julian
  • 9,299
  • 5
  • 48
  • 65
Francis F
  • 3,157
  • 3
  • 41
  • 79
  • can you put breakpointed on sendEmailToContacts method and examine if fList contains specified email list, it could be nil? – ldindu Jan 10 '14 at 09:18
  • it is works for ios6 and above – codercat Jan 10 '14 at 09:49
  • i checked with breakpoint, the flist contains email as i have posted above, it works for ios7, i dont have ios6 sdk so dont know if it works for that – Francis F Jan 10 '14 at 11:39
  • @Gamerlegend Check updated answer of Bhavesh Lakum i hope this time it will work – Maul Jan 13 '14 at 10:10

3 Answers3

0

Recipients are expected as immutable array. check your array type

  NSArray *usersTo = [NSArray arrayWithObject: @"raja@apple.com"];
    [mailComposer setToRecipients:usersTo];
codercat
  • 22,873
  • 9
  • 61
  • 85
  • I tried troubleshooting by manually assigning the fList as you suggested, NSArray *fList = [NSArray arrayWithObject: @"raja@apple.com"]; BUT STILL I HAVE SAME ISSUE, it works on iOS7 but not on iOS5 – Francis F Jan 10 '14 at 11:51
0

Try this one.

        NSArray *fList = [NSArray arrayWithObjects:@"raja@apple.com",@"john@gmail.com",@"mary@gmail.com",@"akhil@gmail.com",@"tester@gmail.com", 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];
Bhavesh Lakum
  • 130
  • 1
  • 12
  • what is mailComposeViewController here in mailComposeViewController.mailComposeDelegate? – Francis F Jan 10 '14 at 11:42
  • No need to write that @Gamerlegend – Maul Jan 13 '14 at 09:03
  • @Gamerlegend mailComposeViewController.mailComposeDelegate is needed in ios 5 for cancel mail view and can you please tell what error comes ,because i tested this code its run perfect with me – Maul Jan 13 '14 at 09:36
  • I omitted that line, what is mailComposeViewController here, is it referring to my current View controller? – Francis F Jan 13 '14 at 09:46
  • no it is useful to close mail sending window when you press on cancel button in ios 5 – Maul Jan 13 '14 at 09:51
  • I have added mailComposer.mailComposeDelegate = self, in the answered code there is also mailComposeViewController.mailComposeDelegate = self; I ommited that one. There is no error but the recepient list is not getting populated in iOS 5 but gets populated in iOS 6 and above, I was wondering if I have to set any other delegate for iOS 5. – Francis F Jan 13 '14 at 09:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45104/discussion-between-maul-and-gamerlegend) – Maul Jan 13 '14 at 09:59
  • Just add this line and test mailComposer.delegate = self i hope this time it will work. see updated answer – Maul Jan 13 '14 at 10:04
  • I am now getting a warning on mailComposer.delegate = self; assigning to id from incompatible type const __Strong I have added the MFMailComposeViewControllerDelegate, is there anything else I have to add – Francis F Jan 13 '14 at 10:46
  • hello can you come in chat with simply click on link i have to discuss with you – Maul Jan 13 '14 at 11:05
0
-(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.

Francis F
  • 3,157
  • 3
  • 41
  • 79