Reputation: 27
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("[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); }
}
}
}
}
}
}
Upvotes: 0
Views: 1890
Reputation: 18769
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:
config
fileSendMail
method to help minimize code replicationInsert
code into a DataHelper
class.Basically, I'm saying you can move a lot of this code outside of the code-behind file.
Upvotes: 1