System.FormatException发送电子邮件时出错

时间:2014-03-13 10:15:55

标签: c# exception

错误:System.dll中出现“System.Format Exception”类型的异常,但未在用户代码中处理。附加信息:指定的字符串不是电子邮件地址所需的格式。

我正在尝试发送邮件,但代码正在提供异常System.Format异常。我正在尝试在一段时间后发送邮件。这是代码

 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","ashishbhatt1501@gmailcom"))
                     {
                        // 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("ashishbhatt1501@gmail.com", "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 个答案:

答案 0 :(得分:1)

MailMessage类中的第一个参数是来自地址,但您似乎正在使用

MailMessage(" Service Call", 

将其更改为您要使用的发件人地址。

另外,你在哪里设置To?您可能最好不要构建MailMessage并在Using语句中设置这些属性......

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

旁注: 我只是想把它作为旁注来提及;您上面的一些代码应该重构:

  • 可以将连接字符串移动到config文件
  • 邮件发送代码可以移动到新类中,并公开SendMail方法以帮助最小化代码复制
  • 您可以将Insert代码打包到DataHelper类。

基本上,我说你可以在代码隐藏文件之外移动很多代码。