ASP.NET - Send Email from asp .net - Asked By Dilip Sharma on 27-Jul-11 01:57 AM

  MailMessage mM = new MailMessage();
        mM.From = new MailAddress("");
        mM.Subject = "Confirmation Mail";
        mM.Body = "Welcome \n now ur are registered in SaleInCity \n Your Password is";
        mM.IsBodyHtml = true;
        mM.Priority = MailPriority.High;
        SmtpClient sC = new SmtpClient("");
        sC.Port = 587;
        sC.Credentials = new NetworkCredential("", "9098031933");
        //sC.EnableSsl = true;

I m using above code to send mail....

but the error is come 

failure sending mail

{"Unable to connect to the remote server"}

Plese give me the solution
Ravi S replied to Dilip Sharma on 27-Jul-11 02:00 AM

use this code

public void Page_Load(object Sender, EventArgs E)
	MailMessage msg = new MailMessage();

	msg.To = "";
	msg.From = "";
	msg.Subject = "test";
	//msg.BodyFormat = MailFormat.Html
	msg.BodyFormat = MailFormat.Text;
	msg.Body = "hi";

	msg.Attachments.Add(new MailAttachment(Server.MapPath("EMAIL1.ASPX")));

	SmtpMail.SmtpServer = "localhost";

	msg = null;
	lblMsg.Text = "An Email has been send to " + "";


refer the links also
Reena Jain replied to Dilip Sharma on 27-Jul-11 02:12 AM

You are using gmail smtp server to send the mail which is correct. here is the complete code to send Email with Gmail SMTP Server from ASP.Net

To send email using gmail SMTP server you will require a valid gmail userid and password. If you didn’t specify a valid gmail userid and password then you will get the following error.

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

The below code can be used to send email using gmail SMTP server,

MailMessage mail = new MailMessage();
mail.From = new MailAddress("");
mail.Subject = "Test Email";
string Body = "<b>Welcome to CodeDigest.Com!!</b>";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTP"];
smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["FROMPWD"]);
smtp.EnableSsl = true;  


  <add key="SMTP" value=""/>
  <add key="FROMEMAIL" value=""/>
  <add key="FROMPWD" value="password"/>

Hope this will help you
Kalit Sikka replied to Dilip Sharma on 27-Jul-11 02:13 AM
Please check these points

a)  Here "smtp.server" need to be valid one.

b)  the server should using a valid port to send emails

c)  the credentials "id" and "pwd" need to be valid for sending email

Venkat K replied to Dilip Sharma on 27-Jul-11 02:17 AM
Was about to say it seems to be a mail server problem.  It was possible to use it without setting this property and then it used the local server's SMTP service.
If you are testing this from your local machine you may be able to use localhost as a mail server as a temporary solution.

Dilip Sharma replied to Reena Jain on 27-Jul-11 02:24 AM
hi reena

in my project this error is come

 failure sending mail
{"Unable to connect to the remote server"}

Can u tell me why this type of error is come...

if there is any setting so plse tell me...

dipa ahuja replied to Dilip Sharma on 27-Jul-11 02:27 AM
Untitled document
Step 1 : Add this in Web.config
<network host=""
Step 2 : Mail Sending Code
protected void btnSent_Click(object sender, EventArgs e)
   string toEmailAddress = "";
   string GmailId = "";
   string bodyMsg = "helo its testing mail";
   string subject = "testing mail";
   MailMessage mail = new MailMessage();
  mail.From = new MailAddress(GmailId);
   mail.Subject = subject;
   mail.Body = bodyMsg;
   mail.IsBodyHtml = true;
   SmtpClient smtp = new SmtpClient();
  smtp.EnableSsl = true;
Sreekumar P replied to Dilip Sharma on 27-Jul-11 05:16 AM


There is some change over here

SmtpClient sC = new SmtpClient("");
sC.Port = 587; //ALSO try with Port No: 465
Sreekumar P replied to Dilip Sharma on 27-Jul-11 05:20 AM
Reena Jain replied to Dilip Sharma on 27-Jul-11 06:49 AM

This error particularly occurs if you are behind a firewall or proxy. When you use a winforms app, it can autodetect the proxy using IE settings for current user, and it can connect out through the proxy. However, when in, it cannot because the user identitiy does not have the correct proxy settings.

To resolve this issue, you need to explicitly specify the proxy settings for your application. You can do it at various levels viz., at the Machine.Config file which will apply for all the applications running on the system, at the Web.Config file such that it applies for a single application, and at the Page Level programattically in the code behind / code inline.

Web.Config setting

usesystemdefault = "false"

For Machine.Config use the same settings as above.

To do it programmatically,

using System.Net;

WebServiceClass MyWebServiceClass = new WebServiceClass();
WebProxy proxyObject = new WebProxy("http://address:port", true);
MyWebServiceClass.Proxy = proxyObject;

where WebServiceClass is the name of the Web Service Class which you are consuming.

That should solve the issue with consuming the Web Service across Proxy Settings.

Hope this will help you