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("Brain.Checker11@yahoo.com");
        mM.To.Add("dilip.aim11@gmail.com");
        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("smtp.mail.yahoo.com");
        sC.Port = 587;
        sC.Credentials = new NetworkCredential("Brain.Checker11@yahoo.com", "9098031933");
        //sC.EnableSsl = true;
        sC.Send(mM);
        //lbReport.Tex


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
HI

use this code

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

	msg.To = "das@silicomm.com";
	msg.From = "das@aspalliance.com";
	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";

	SmtpMail.Send(msg);
	msg = null;
	lblMsg.Text = "An Email has been send to " + "das@silicomm.com";

}

refer the links also
http://www.4guysfromrolla.com/articles/072606-1.aspx#postadlink
http://aspalliance.com/149_how_to_send_email_from_asp_net_
Reena Jain replied to Dilip Sharma on 27-Jul-11 02:12 AM
Hi,

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, smtp.gmail.com.


MailMessage mail = new MailMessage();
 mail.To.Add("to@gmail.com");
 
mail.From = new MailAddress("from@gmail.com");
 
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;  
 
smtp.Send(mail);

Web.Config


<appSettings>
  <add key="SMTP" value="smtp.gmail.com"/>
  <add key="FROMEMAIL" value="mail@gmail.com"/>
  <add key="FROMPWD" value="password"/>
  </appSettings>

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. http://msdn.microsoft.com/en-us/library/system.web.mail.smtpmail.smtpserver.aspx.
If you are testing this from your local machine you may be able to use localhost as a mail server as a temporary solution.

Thanks
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
    
<system.net>
<mailSettings>
<smtp>
<network host="smtp.gmail.com"
  port="587"
  userName=""
  password=""
</smtp>
</mailSettings>
</system.net>
 
Step 2 : Mail Sending Code
protected void btnSent_Click(object sender, EventArgs e)
{
   string toEmailAddress = "xx@gmail.com";
   string GmailId = "abc@gmail.com";
   string bodyMsg = "helo its testing mail";
   string subject = "testing mail";
   MailMessage mail = new MailMessage();
   mail.To.Add(toEmailAddress);
  mail.From = new MailAddress(GmailId);
   mail.Subject = subject;
   mail.Body = bodyMsg;
   mail.IsBodyHtml = true;
   SmtpClient smtp = new SmtpClient();
  smtp.EnableSsl = true;
  smtp.Send(mail);
}
 
Sreekumar P replied to Dilip Sharma on 27-Jul-11 05:16 AM


Hi,

There is some change over here

SmtpClient sC = new SmtpClient("plus.smtp.mail.yahoo.com");
sC.Port = 587; //ALSO try with Port No: 465
Sreekumar P replied to Dilip Sharma on 27-Jul-11 05:20 AM
http://techblissonline.com/yahoo-pop3-and-smtp-settings/
Reena Jain replied to Dilip Sharma on 27-Jul-11 06:49 AM
hi,

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 asp.net, it cannot because the asp.net 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

<configuration>
<system.net>
<defaultProxy>
<proxy
usesystemdefault = "false"
proxyaddress="http://address/:port"
bypassonlocal="false"
/>
</defaultProxy>
</system.net>
<system.web>

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;
MyWebServiceClass.MyWebMethod();



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