2012-03-19 73 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