Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi Am getting this error: while sending to all the emails in from db:
The specified string is not in the form required for an e-mail address...

What I saw is due to a missing of a comma.
Because while testing by sending using this query, it is working:

SQL
ALTER PROCEDURE [dbo].[GetUserByEmail]

AS
BEGIN

    SELECT U.UserName as Email from [User] U
    left join [Member] M on M.UserId=U.UserId
    where U.UserId in(9, 52, 51)

END


But when I am removing this line of code
where U.UserId in(9, 52, 51)
it is not working: means like this

SQL
ALTER PROCEDURE [dbo].[GetUserByEmail]

AS
BEGIN

    SELECT U.UserName as Email from [User] U
    left join [Member] M on M.UserId=U.UserId


I am getting that error mentioned above.

And this is my c# code:

C#
string[] recipients = new[] { "teke@gmail.com", "dan.jojo@gmail.com" };

            DataTable dt = _UserInfo.GetUserAllByEmail();
            string recipient;
            mailMessage.From = new MailAddress("sales@gmail.com", "We have big Deals for you!");
            mailMessage.To.Add("dan@gmail.com");
            foreach (DataRow drRecipient in dt.Rows)
            {
                mailMessage.Bcc.Add(drRecipient["Email"].ToString());
                mailMessage.Bcc.Add(",");
            }
            mailMessage.Subject = "Test Email";
            mailMessage.IsBodyHtml = true;


as u can see on from this code , I am trying to seperate the email like this:
mailMessage.Bcc.Add(",");

But it is not working, I am still getting that error.
How can I separate the email with the comma?
Posted
Updated 4-Apr-14 7:08am
v3

you could create a string with all addresses and add BCC once.. like below

C#
string[] recipients = new[] { "teke@gmail.com", "dan.jojo@gmail.com" };

            DataTable dt = _UserInfo.GetUserAllByEmail();
            string recipient;
            mailMessage.From = new MailAddress("sales@gmail.com", "We have big Deals for you!");
            mailMessage.To.Add("dan@gmail.com");
            string bccAddresses = "";
            foreach (DataRow drRecipient in dt.Rows)
            {
              bccAddresses = bccAddresses + " " + drRecipient["Email"].ToString() +",";
            }

            mailMessage.Bcc.Add(bccAddresses);
            mailMessage.Subject = "Test Email";
            mailMessage.IsBodyHtml = true;
 
Share this answer
 
Comments
El Dev 4-Apr-14 13:19pm    
Hi Guruprasad! I have updated my code as u said but am still getting an error.This the error I am getting:

System.FormatException: The specified string is not in the form required for an e-mail address. at System.Net.Mime.MailBnfHelper.ReadMailAddress(String data, Int32& offset, String& displayName) at System.Net.Mime.MailBnfHelper.ReadMailAddress(String data, Int32& offset) at System.Net.Mail.MailAddressCollection.ParseValue(String addresses) at System.Net.Mail.MailAddressCollection.Add(String addresses) at Admin_newsletter.HtmlEmailTemplate() in d:\hosting\9780189\html\Admin\newsletter.aspx.cs:line 141

The line 141 is here mailMessage.Bcc.Add(bccAddresses); in my code.
Guruprasad.K.Basavaraju 4-Apr-14 13:45pm    
Dev, could you paste the value of the string "bccAddresses" by putting a break point please ?
El Dev 5-Apr-14 5:55am    
Hi Guruprasad! This is the value of bccAddresses: muhizi@gmail.com, josh@hotmail.com, etc
I can't list all the emails but this is what I am getting.
Have a look at this article.[^] You can't add a string value to the Bcc mail message property.

[EDIT]

Try this in your Add() method:
string bcc = string.Empty;
char[] comma = {','};
foreach (DataRow drRecipient in dt.Rows)
{
      bcc += drRecipient["Email"].ToString() + ",";
}
mailMessage.Bcc.Add(bcc.TrimEnd(comma));
 
Share this answer
 
v5
Comments
El Dev 4-Apr-14 12:29pm    
Hi Richard I do understand but I ma trying to separate the emails with comma because without separating with a comma, it is giving the error I mentioned. I can remove that line of code and it will work perfectly but it will give that error mentioned above.
Richard C Bishop 4-Apr-14 12:36pm    
Just add a comma after each address in one string value and pass that to the Add() method.
El Dev 4-Apr-14 12:43pm    
ok what if I have thousands of emails, which is my case!!
mailMessage.Bcc.Add(drRecipient["Email"].ToString());

This how I am adding all the emails and send to them a newsletter.And please take a look to my query again, u can see that I have to separate with the commas. but this is limited to some user,is why I have remove the where clause.Just inorder to send to all the email from my db.
Richard C Bishop 4-Apr-14 12:46pm    
See my updated solution above.
El Dev 4-Apr-14 12:49pm    
u mean this "Just add a comma after each address in one string value and pass that to the Add() method." if yes please I do not understand how, please an exemple.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900