2012-03-19 44 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是一個集合,所以就這樣對待它。我真的不知道你有標籤或者爲什麼做什麼,所以就忽略了現在,這樣的事情應該工作

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

謝謝它的工作原理 – 2012-03-19 15:45:29

0

你的邏輯流程就沒有意義了。你正在解析電子郵件,然後試圖通過一些有缺陷的邏輯解開你的電子郵件地址。取而代之的是,創建您的郵件消息,然後然後循環通過您的電子郵件地址,將每個添加到BCC。

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

謝謝它的工作原理 – 2012-03-19 15:45:41