2012-03-19 16 views
0

私のコードで達成しようとしているのは、私のデータベースにある各電子メールアドレスの電子メールを送信することです。私の問題は、送信ボタンをクリックすると、mail.Bcc.Add(MyVar.Text)行の "The specified string is not in the form required for an e-mail address."というエラーが表示されます。mail.Bcc.Add()ASP .NetのエラーC#

private void sendmail() 
    { 
     Label MyVar = new Label(); 
     foreach (DataRowView UserEmail in SelectUserProfile.Select(DataSourceSelectArguments.Empty)) 
     { 
      MyVar.Text = ""; 
      MyVar.Text += UserEmail["EMAIL"].ToString() + "; "; 
     } 

     //This line takes the last ; off of the end of the string of email addresses 
     MyVar.Text += MyVar.Text.Substring(0, (MyVar.Text.Length - 2)); 

     MailMessage mail = new MailMessage(); 

     mail.Bcc.Add(MyVar.Text); 
     mail.From = new MailAddress("[email protected]"); 
     mail.Subject = "New Member Application"; 
     mail.Body = "Good day, in this e-mail you can find a word document attached in which it contains new membership application details."; 
     mail.IsBodyHtml = true; 
     SmtpClient smtp = new SmtpClient(); 
     smtp.Host = "smtp.gmail.com"; 
     smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "mypassword"); 
     smtp.EnableSsl = true; 
     smtp.Send(mail); 
    } 

アーニー

答えて

1

なぜBCC電子メールアドレスの文字列を埋め込みますか?

Bccはコレクションなので、そのまま扱ってください。それが動作ありがとう私はちょうど今のところ、このようなものが

MailMessage mail = new MailMessage(); 

foreach (DataRowView UserEmail in SelectUserProfile.Select(DataSourceSelectArguments.Empty)) 
{ 
    MyVar.Text = ""; 
    MyVar.Text += UserEmail["EMAIL"].ToString() + "; "; 

    try 
    { 
     mail.Bcc.Add(UserEmail["EMAIL"].ToString()); 
    } 
    catch(FormatException fe) 
    { 
     // Do something with the invalid email address error. 
    } 
} 
+0

の作品をありがとう働く必要があることを無視して、ラベルや理由でやっていることは本当にわからないんだけど –

0

あなたの論理の流れは意味がありません。あなたは、電子メールを一緒に解析し、いくつかの欠陥のあるロジックを使って電子メールアドレスの解析を解くことを試みています。代わりに、メールメッセージを作成してのメールアドレスをループし、それぞれをBCCに追加します。

// Create Message (...) 
foreach(...) 
{ 
    mail.Bcc.Add(UserEmail["EMAIL"].ToString()); 
} 
// Finalize and send (...) 
+0

はそれが –