Click here to Skip to main content
15,884,962 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am getting this error while sending an email to clients from my db:

Quote:
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 143


This is the code am using to send an email to all clients:

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

            DataTable dt = _UserInfo.GetUserAllByEmail();
            string bccAddresses = "";
            string Address = "";
            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)
            {
                bccAddresses = bccAddresses + " " + drRecipient["Email"].ToString() + ",";
                Address = bccAddresses.TrimEnd(',');
            }
            mailMessage.Bcc.Add(Address);
            mailMessage.Subject = "Test Email";
            mailMessage.IsBodyHtml = true;


And this is my query:

SQL
ALTER PROCEDURE [dbo].[GetUserByEmail]

AS
BEGIN

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


END


in case u want to know the values of Address, here it is: muhi@gmail.com, josh@hotmail.com, ihope031@gmail.com
And the line 143 it is:
mailMessage.Bcc.Add(Address);


Can someone help fixing this issue.
Posted

1 solution

It's somewhere in your string concatenation, which is not needed at all.
C#
foreach (DataRow drRecipient in dt.Rows)
{
   mailMessage.Bcc.Add(drRecipient["Email"].ToString());
}
 
Share this answer
 
Comments
Ankur\m/ 8-Apr-14 4:08am    
Correct, 5!
El Dev 8-Apr-14 5:10am    
Am still getting the same error(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 140)
this how I change the code as u said:
string[] recipients = new[] { "teke@gmail.com", "dan.jojo@gmail.com" };

DataTable dt = _UserInfo.GetUserAllByEmail();
string bccAddresses = "";
string Address = "";
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.Subject = "Test Email";
mailMessage.IsBodyHtml = true;
Bernhard Hiller 9-Apr-14 2:56am    
Hard to imagine that you cannot resolve it now. It means that somewhere in your database, you stored a value in "Email" which is not a valid email address. Use the debugger, and find out which one it is, and then correct the data!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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