2013-11-21 20 views
4

我想發送一個電子郵件asp.net,使用System.Net.Mail.SmtpClient類。參數'地址'不能爲空字符串

但是我收到以下異常消息:

The parameter 'addresses' cannot be an empty string. 
    Parameter name: addresses 

這是我發送電子郵件代碼:

private void SendEmailUsingGmail(string toEmailAddress) 
    { 
     try 
     { 
      SmtpClient smtp = new SmtpClient(); 
      smtp.Credentials = new NetworkCredential("[email protected]", 
      "sdsdasd"); 
      smtp.Port = 587; 
      smtp.Host = "smtp.gmail.com"; 
      smtp.EnableSsl = true; 
      MailMessage message = new MailMessage(); 
      message.From = new MailAddress("[email protected]"); 
      message.To.Add(toEmailAddress); 
      message.Subject = "Write your email subject here"; 
      message.Body = "write the content of the email here"; 
      smtp.Send(message); 
     } 
     catch (Exception ex) 
     { 
      Response.Write("Error occured: " + ex.Message.ToString()); 
     } 
    } 

異常正陷入SendEmailUsingGmail catch塊。

這是調用代碼:

protected void Button1_Click(object sender, EventArgs e) 
{    
    string connStr = ConfigurationManager.ConnectionStrings["mydms"].ConnectionString; 
    SqlConnection mySQLconnection = new SqlConnection(connStr); 
    if (mySQLconnection.State == ConnectionState.Closed) 
    { 
     mySQLconnection.Open(); 
     for (int i = 0; i < Repeater2.Items.Count; i++) 
     { 
      DropDownList DropDownListcontrol = ((DropDownList)Repeater2.Items[i].FindControl("DropDownList4")); 
      Label DocId = ((Label)Repeater2.Items[i].FindControl("DocId")); 

      SqlCommand cmd = new SqlCommand("approveddd", mySQLconnection); 
      cmd.CommandType = CommandType.StoredProcedure; 

      cmd.Parameters.Add("@DocID", SqlDbType.Int).Value = Convert.ToInt32((DocId.Text)); 

      cmd.Parameters.Add("@ApproveID", SqlDbType.Int).Value = Convert.ToInt32(DropDownListcontrol.SelectedValue); 
      cmd.Parameters.Add("@ApproveBy", SqlDbType.VarChar, 50).Value = (Session["Login2"]); 
      try 
      { 
       cmd.ExecuteNonQuery(); 
       string emailId = ((Label)Repeater2.Items[i].FindControl("Label2")).Text; 
       SendEmailUsingGmail(emailId); 
      } 
      catch (Exception ex) 
      { 
       Supvisor.Text=(ex.Message); 
      } 
      cmd.ExecuteNonQuery(); 
      //UPDATE APPPROVEID IN DOCUMENTINFO TABLE 
      //DMSLIB.Doc myDoc = new DMSLIB.Doc(); 
      //myDoc.MarkDocAs(Convert.ToInt16(DocId.Text), 
      Convert.ToInt32(DropDownListcontrol.SelectedValue)); 
     } 

    } 
    else 
    { 
     Supvisor.Text = "Error"; 
    } 
    if (mySQLconnection.State == ConnectionState.Open) 
    { 
     mySQLconnection.Close(); 
    } 
} 

當管理員批准/決絕的文檔,然後將數據保存到數據庫中這樣

SeqNo DocID ApproveID ApproveBy 
82  20  3   john 
83  21  1   john 
84  18  2   kety 
85  19  1   emel 

現在我也 發送電子郵件時對按鈕管理員點擊即可電子郵件發送到相應的電子郵件ID 這樣我在中繼器表

DocID DocName Uplaodedfile UserEmail   DocType DepType ApproveID 
1  ABC  def.pdf   [email protected] pdf  hr  (In this i set dropdown values are (approve/reject/pending) 

回答

5

嘗試

message.To.Add(New MailAddress(toEmailAddress)); 

,並確認變量 「toEmailAddress」 的內容。

+0

當我添加此它顯示我的錯誤在這條線..Error \t \t 200)預期\t,錯誤\t \t 201無效表達術語 ')',錯誤\t \t 202;預計 – user2931015

+0

變量「toEmailAddress」有什麼值? – Nacho

+0

在這一行我稱之爲標籤...(標籤)Repeater2.Items [i] .FindControl(「Label2」))。在像這樣的​​ HTML的 <%#的DataBinder.Eval(的Container.DataItem, 「USEREMAIL」)%> – user2931015

2

此例外意味着您嘗試添加一個空字符串作爲電子郵件地址。哪一個是無效的。 在這種情況下,這意味着toEmailAddress是一個空字符串。

相關問題