Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my application I would like to send a mail and I am using the below code,
C#
protected void SendMail()
   {
       // Gmail Address from where you send the mail
       var fromAddress = "xxxxx@gmail.com";
       // any address where the email will be sending
       var toAddress = txtto.Text.ToString();
       //Password of your gmail address
       const string fromPassword = "*****";
       // Passing the values and make a email formate to display
       string subject = "Testing";
       string body = "xxx";

       // smtp settings
       var smtp = new System.Net.Mail.SmtpClient();
       {
           smtp.Host = "smtp.gmail.com"
           smtp.Port = 587;
           smtp.EnableSsl = true;
           smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
           smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
           smtp.Timeout = 2000000;
       }
       // Passing values to smtp object
       smtp.Send(fromAddress, toAddress, subject, body);
   }
   protected void btnsend_Click(object sender, EventArgs e)
   {
       try
       {
         
           {
               SendMail();
              
           }
       }
       catch (Exception ex)
       {
           Response.Write("Could not send the e-mail - error" +ex.Message);
       }

   }

but I getting an error:
Could not send the e-mail - error: Failure sending mail.

[Edit]Code block added[/Edit]
Posted
Updated 18-Dec-12 1:02am
v2
Comments
Am Gayathri 18-Dec-12 7:21am    
Its strange nobody knows the answer??
Am Gayathri 18-Dec-12 9:38am    
Error: Unable to connect to the remote server
Surendra0x2 18-Dec-12 12:20pm    
try to deactivate your Antivirus then send mail.

Have you tried this way refer link below

http://asp.net-tutorials.com/misc/sending-mails/[^]

you also need to have setting in web.config

refer

http://community.discountasp.net/showthread.php?t=9157[^]
 
Share this answer
 
v2
Comments
Am Gayathri 18-Dec-12 9:37am    
Getting error:
Unable to connect to the remote server
Try out this code :

C#
var smtp = new System.Net.Mail.SmtpClient();
   {
       smtp.Host = "smtp.gmail.com";
       smtp.Port = 587;
       smtp.EnableSsl = true;
       smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
       smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
       smtp.Timeout = 20000;
   }
   // Passing values to smtp object
   smtp.Send(fromAddress, toAddress, subject, body);
 
Share this answer
 
Comments
Am Gayathri 18-Dec-12 9:05am    
The same code am using...still getting error
Am Gayathri 18-Dec-12 9:37am    
Error is:
Unable to connect to the remote server
[no name] 18-Dec-12 11:40am    
How it could be possible.. I've tried it out by myself..
Am Gayathri 19-Dec-12 4:34am    
Its strange, might be problem with my proxy.
Am trying to send mail from my gmail account so anything need to change in gmail account settings?
[no name] 19-Dec-12 8:18am    
No..
Please refer following link:
http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail[^]

also check with debugger that smtp.send function has all proper values.
 
Share this answer
 
v2
Hi,

Have a look at this tip:
Sending an Email in C# with or without attachments: generic routine.[^]
Check also your firewall settings.
 
Share this answer
 
 
Share this answer
 
v2

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