views:

98

answers:

3
+2  Q: 

code to send email

What am I doing wrong here?

 private void SendMail(string from, string body)
    {
        string mailServerName = "plus.pop.mail.yahoo.com";
        MailMessage message = new MailMessage(from, "[email protected]", "feedback", body);
        SmtpClient mailClient = new SmtpClient();
        mailClient.Host = mailServerName;
        mailClient.Send(message);
        message.Dispose();
    }

I got the following error:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 209.191.108.191:25

+4  A: 

You are using the wrong server. You will need to use the SMTP settings.

try this server: plus.smtp.mail.yahoo.com Their site notes this host as SSL.

private void SendMail(string from, string body) 
{ 
    string mailServerName = "plus.smtp.mail.yahoo.com"; 
    int mailServerPort = 465;
    string toAddress = "[email protected]";
    string subject = "feedback";

    string username = "user";
    string password = "password";

    SmtpClient mailClient = new SmtpClient(mailServerName, 
                                           mailServerPort); 
    mailClient.Host = mailServerName; 
    mailClient.Credentials = new NetworkCredential(username, 
                                                   password);
    mailClient.EnableSsl = true;

    using (MailMessage message = new MailMessage(from, 
                                                 toAddress, 
                                                 subject, 
                                                 body))
        mailClient.Send(message); 
} 
Matthew Whited
the username and password should be the senders password?
EquinoX
I also got the following error:An existing connection was forcibly closed by the remote host
EquinoX
yes, the username and password must be that of the sender. The username may need to be your email address. You can also try to remove the SSL and login, but that will depend on yahoo. I don't have an acount with them so I can't test it for you.
Matthew Whited
well...this is for a contact us form.. so therefore are you suggesting that I need the user to enter their username and password in the contact us form? Doesn't sound too good... usually a contact us form only inputs their email
EquinoX
No, you just need an account on the SMTP server. You should be able to put any address you want in the from line. (Typically the from address and the smtp account are the same, unless you are using SMTP as a relay as you described.) You may need to check with Yahoo though. Sometimes the user agreement/policy says that you will only use their servers for personal email.
Matthew Whited
+3  A: 

You need to use a SMTP server, looks like you are using a POP3 server.

Fogh
A: 

To send email using Yahoo mail servers you need to set EnableSSL = true on your SmtpClient instance.

You also need to use the correct port which is 465.

There are a lot of tutorials on this site which really cover how to use the System.Net.Mail namespace:

rtpHarry