2014-03-13 170 views
0

錯誤:在System.dll中發生類型'System.Format異常'的異常,但未在用戶代碼中處理。附加信息:指定的字符串不在所需的表單中爲一個電子郵件地址。System.FormatException發送電子郵件時出錯

我想發送一個郵件,但代碼給出了一個異常System.Format Exception.I試圖發送一段時間後的郵件。這裏是代碼

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using System.Data.SqlClient; 
using System.Net.Mail; 
using System.Net; 

namespace esaote 
{ 
    public partial class user : System.Web.UI.Page 
    { 
     SqlConnection con; 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      con = new SqlConnection("Data Source=ASHISH;Initial Catalog=esaote;Integrated Security=True"); 

      TextBox6.Text = DateTime.Now.ToShortDateString(); 
      TextBox7.Text = DateTime.Now.AddHours(1.00).ToShortDateString(); 
      maildate(); 
     } 

     protected void Button1_Click(object sender, EventArgs e) 
     { 

      string q = "insert into info(c_name,c_address, machine, s_version, email,i_date,due_date) values(@c_name, @c_address, @machine, @s_version, @email, @i_date,@due_date)"; 
      SqlCommand cmd = new SqlCommand(q, con); 

      cmd.Parameters.AddWithValue("@c_name", TextBox1.Text); 
      cmd.Parameters.AddWithValue("@c_address", TextBox2.Text); 
      cmd.Parameters.AddWithValue("@machine", TextBox3.Text); 
      cmd.Parameters.AddWithValue("@s_version", TextBox4.Text); 
      cmd.Parameters.AddWithValue("@email", TextBox5.Text); 
      cmd.Parameters.AddWithValue("@i_date",Convert.ToDateTime(TextBox6.Text)); 
      cmd.Parameters.AddWithValue("@due_date",Convert.ToDateTime(TextBox7.Text)); 
      //string due_date = DateTime.Now.ToShortDateString() + DateTime.Now.AddMonths(6).ToShortDateString(); 
      try 
      { 
       con.Open(); 
       if (cmd.ExecuteNonQuery() > 0) 
       { 
        Response.Write("<script languge='javascript'>alert('data inserted');</script>"); 
       } 

      } 
      catch (Exception exp) 
      { 
       Console.Write(exp.Message); 
      } 
      finally 
      { 
       con.Close(); 
      } 


     } 

     public void maildate() 
     { 
      SqlConnection con =new SqlConnection("Data Source=ASHISH;Initial Catalog=esaote;Integrated Security=True"); 
      string s = "select * from info"; 
      SqlCommand cmd = new SqlCommand(s,con); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 

      DataSet ds = new DataSet(); 
      da.Fill(ds); 

      for (int i = 0; i < ds.Tables[0].Rows.Count;i++) 
      { 
       DateTime id = Convert.ToDateTime(ds.Tables[0].Rows[i]["i_date"]); 
       DateTime pd = Convert.ToDateTime(ds.Tables[0].Rows[i]["due_date"]); 

       double diff = (pd - id).TotalDays; 
       if(diff>=1) 
       { 
        string email = Convert.ToString(ds.Tables[0].Rows[i]["email"]); 
        string customer = Convert.ToString(ds.Tables[0].Rows[i]["c_name"]); 
        using (MailMessage mm = new MailMessage(" Service Call","[email protected]")) 
        { 
         // mm.Body = "your sevice for '" + customer + "' are due."; 

         mm.IsBodyHtml = false; 
         SmtpClient smtp = new SmtpClient(); 
         smtp.Host = "smtp.gmail.com"; 
         smtp.EnableSsl = true; 
         NetworkCredential NetworkCred = new NetworkCredential("[email protected]", "062621562a"); 
         smtp.UseDefaultCredentials = true; 
         smtp.Credentials = NetworkCred; 
         smtp.Port = 587; 
         try 
          { 
          smtp.Send(mm); 
          ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true); 
          } 
          catch (Exception exp) { Console.Write("helllo" + exp); } 
         } 
        } 
       } 
      } 

     } 
     } 

回答

1

MailMessage類中的第一個參數是從地址,但你似乎可以用

MailMessage(" Service Call", 

其更改爲從地址要使用。

另外,您在哪裏設置To?你可能會更好構建MailMessage,並在Using陳述,這些屬性...

using (MailMessage mm = new MailMessage()) 
{ 
    mm.from = "[email protected]"; 
    mm.to = email; //I'm assuming email from your code. 
    mm.subject = "Service Call"; //again, this is just an assumption 
    ... 
} 

側面說明: 只是想我會提到這一點作爲一個側面說明;一些你在上面需要重構的代碼:

  • 的連接字符串可以移入config文件
  • 發送代碼的郵件可能被移動到一個新的類,並且暴露出SendMail方法,以幫助減少代碼複製
  • 您可以將Insert代碼包裝到DataHelper類中。

基本上,我說你可以在代碼隱藏文件之外移動很多代碼。

相關問題