2010-04-04 125 views
3

我在这里做错了什么?代码发送电子邮件

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(); 
    } 

我得到了以下错误:

连接尝试失败,因为连接的方没有正确一段时间后响应或已建立的连接失败,因为连接的主机未能响应209.191.108.191: 25

回答

5

您正在使用wrong server。您将需要使用SMTP设置。

试试这台服务器:plus.smtp.mail.yahoo.com他们的站点将此主机记录为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); 
} 
+0

用户名和密码应该是发件人密码? – aherlambang 2010-04-04 17:17:56

+0

我还收到以下错误: 现有连接被远程主机强制关闭 – aherlambang 2010-04-04 17:19:20

+0

是的,用户名和密码必须是发件人的密码。用户名可能需要是您的电子邮件地址。您也可以尝试删除SSL和登录,但这取决于雅虎。我没有与他们的帐户,所以我不能为你测试它。 – 2010-04-04 18:59:42

5

您需要使用SMTP服务器,看起来像您使用的是POP3服务器。

0

要使用Yahoo邮件服务器发送电子邮件,您需要在SmtpClient实例上设置EnableSSL = true。

您还需要使用正确的端口是465

有这个网站很多教程,这实际上涉及如何使用System.Net.Mail命名空间: