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); }
}
}
}
}
}
}