tags:

views:

182

answers:

3

Hi, I'm unable to send an email to yahoo server as my code is throwing exception as 'Failure Sending mail' in C# 2008. Please provide SMTP HostName, PortName for yahoo server and gmail server.

And also kindly provide a good working C# code with which i can send an email directly to any of the mail servers.

Please provide complete working code...so that i will copy into Visual studio environment and execute the same. As i'm getting exception since morning....unable to resolve the issue. Kindly help me in this regard.

+12  A: 

For Gmail:

var client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("[email protected]", "secret");

var mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test mail";
mail.Body = "test body";
client.Send(mail);

For Yahoo:

var client = new SmtpClient("smtp.mail.yahoo.com", 587);
client.Credentials = new NetworkCredential("[email protected]", "secret");

var mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test mail";
mail.Body = "test body";
client.Send(mail);
Darin Dimitrov
Sorry....... the above code is not at all working for me
Hariram
Kindly help me in this regard.Very utmost help is required.As i copied the same above code and provided my credentials accordingly in the above code and executed in console as well as in windows....but getting exception as 'Failure Sending mail'.Please help me...........
Hariram
Is it possible that you are behind a firewall that could block port 587?
Darin Dimitrov
Before posting the code I tested both with `yahoo` and `gmail` and it worked perfectly fine, so probably there's something related to your environment. Could post the entire exception stack trace you are getting?
Darin Dimitrov
A: 

Bear in mind that some ISPs (including mine) force their clients to use their SMTP server (as a relay). Spamming protection is the reason.

So you should avoid sending e-mail to the Internet from a client application, unless you give user a chance to specify his SMTP hostname or your app relies on the user's e-mail software (MAPI,...).

Serge - appTranslator
A: 

Hi,

Imagine how much easier it would be for us to help you, if you posted the complete exception message, along with a stack trace.

Also, go one step farther and enable logging for System.Net.Mail, so we can see any possible failures at the network level.

If you don't know how to enable logging for SNM, here is a link:

http://systemnetmail.com/faq/4.10.aspx

Thanks!

Dave

dave wanta