简体   繁体   中英

System.FormatException Error in sending a e-mail

Error:An exception of type 'System.Format Exception' occurred in System.dll but was not handled in user code.Additional information: The specified string is not in the form required for an e-mail address.

I am trying to send a mail but the code is giving an exception System.Format Exception.I am trying to send a mail after a certain time period. here is the code

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

        }
        }

The first argument in the MailMessage class is the from address, but you seem to be using

MailMessage(" Service Call", 

Change this to the from address you want to use.

Also, where are you setting the To ? You might be better off constructing the MailMessage and setting these properties in the Using statement...

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
  ...
}

Side Note: Just thought I'd mention this as a side note; some of the code you have above should be refactored:

  • The connections strings can be moved into a config file
  • The mail sending code could be moved into a new class, and expose a SendMail method to help minimize code replication
  • You could wrap the Insert code into a DataHelper class.

Basically, I'm saying you can move a lot of this code outside of the code-behind file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM