Click here to Skip to main content
15,799,257 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have written a code for sending mail. It works perfectly fine on my local host but when I uploaded it and run on my web server its not working. Please Help me ASAP. I have to give the code to the client ASAP.
Here go's the code
protected void addtocart_Click1(object sender, EventArgs e)
	CdataTable = ObjBussiness.login_validate(Session["userid"].ToString(), Session["status"].ToString(), 17, source).Tables[0];
                for (int i = 0; i < CdataTable.Rows.Count; i++)
                    listemailids += CdataTable.Rows[i]["address"].ToString() + ",";

                string ToAddress = listemailids.Remove(listemailids.Length - 1);
                MailMessage mm = new MailMessage("", ToAddress);
                mm.Subject = "Order Cart";
                mm.Body = mailbody.ToString();
                mm.IsBodyHtml = true;

                SmtpClient smtp = new SmtpClient();
                smtp.Host = "";


                Session["productid"] = "";
                Session["qty"] = "";
        catch (System.Data.SqlClient.SqlException ex_msg)
            string msg = "Error occured while login.";
            msg += ex_msg.Message;
            //throw new Exception(msg);
            Label1.Visible = true;
            Label1.Text = msg;

Updated 21-Apr-11 21:04pm
nit_singh 22-Apr-11 1:29am    
If you can tell us what problem you are getting on your web server, then it will be easy to understand the problem. Another thing add another generic catch block, so that it will handle all the exception not of a particular type.
ubaidh sayed 22-Apr-11 3:03am    
Thanks nit_singh.
Its resolved.

As already pointed out, I see you are using 'Host = "";' which is a localhost ip. This might not work on webserver. You need to have proper smtp server name to communicate.

Further, it can be because of other various reasons. Like: Is the port open? Firewall permissions in place?

I would suggest you to configure SMTP configuration in Web.Config:
     <smtp from="">
       <network host="somesmtpserver" port="25" userName="name" password="pass" defaultCredentials="true" />
Share this answer
Monjurul Habib 22-Apr-11 2:55am    
seems 5.
My guess is that smtp.Host is incorrect. Your web provider should be able to give you the correct address; usually something like or similar.
Share this answer
Thanks digital man &amp;amp; Sandeep Mewara.
That worked for me :-)
Thanks a million for your time.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900