zohre
zohre

Reputation: 21

Asp.net:How to fix exception thrown when sending mail message to multiple recipients?erroline:An invalid character was found in the mail header: ','

Error on this line message.To.Add(mailTo); when I try to take the email data from the database to send an email.

 void Sendmail_Click(object sender, EventArgs e)
        {

            List<DAL.Customer> mailId = (from rs in General.db.Customers
                                         where rs.IsActive == true
                                         select rs).ToList();

            StringBuilder mailstring = new StringBuilder();

                foreach (var item in mailId)
                {

                        mailstring.Append(item.Email).Append(",");

                }

            string mailTo = mailstring.ToString();

            if (mailTo.EndsWith(","))
            {

                mailTo = mailTo.Remove(mailTo.LastIndexOf(","));
            }


            SmtpClient smtp = new SmtpClient();
            mailMessage.From = fromMail;
            mailMessage.To.Add(new MailAddress(mailTo));

            mailMessage.IsBodyHtml = true;



            lblStatus.Text = "Email sent successfully";



    }

Upvotes: 2

Views: 999

Answers (3)

Tim
Tim

Reputation: 28530

I don't see any overload of the MailAddress constructor that takes a comma-delimited string. You need to loop through all the destination e-mail addresses one at a time, which will require a minor reorganization of your code:

SmtpClient smtp = new SmtpClient();
mailMessage.From = fromMail;

List<DAL.Customer> mailId = (from rs in General.db.Customers
                             where rs.IsActive == true
                             select rs).ToList();

foreach (var item in mailId)
{
    mailMessage.To.Add(new MailAddress(item.Email));
}

Upvotes: 0

Ant P
Ant P

Reputation: 25221

As Christian Mark suggests, you should probably just loop through each of the mail addresses and add them individually (since you are already looping through them to build a string).

However, the reason why this doesn't work in theory is because you are creating a single MailAddress with a comma-separated string of email addresses, with the following line:

mailMessage.To.Add(new MailAddress(mailTo));

You can pass a comma-separated list to the Add(string) overload of MailAddressCollection.Add but - because you are passing a MailMessage - this is not the overload you are using.

The following would work:

mailMessage.To.Add(mailTo);

Upvotes: 0

Mark
Mark

Reputation: 8431

Please take a look at this. I don't know the credentials so I assume you can add them up.

void Sendmail_Click(object sender, EventArgs e)
        {

            List<DAL.Customer> mailId = (from rs in General.db.Customers
                                         where rs.IsActive == true
                                         select rs).ToList();

            SmtpClient smtp = new SmtpClient();
            mailMessage.From = fromMail;
            mailMessage.IsBodyHtml = true;

            foreach (var item in mailId)
            {
               mailMessage.To.Add(new MailAddress(item.Email));
            }
            lblStatus.Text = "Email sent successfully";

    }

Upvotes: 2

Related Questions