发送邮件在本地工作,但不在服务器上?

问题描述:

此代码在本地工作,但是当我将它上传到Godaddy上的服务器时,它不会发送电子邮件。任何想法,为什么它不能在他们的服务器上工作?我需要改变什么?发送邮件在本地工作,但不在服务器上?

try { 
    MailMessage mail = new MailMessage(); 
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); 

    mail.From = new MailAddress("[email protected]"); 
    mail.To.Add("[email protected]"); 
    mail.Subject = "New sign up"; 
    mail.Body = "New member"; 

    SmtpServer.Port = 587; 
    SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "**Mypass**"); 
    SmtpServer.EnableSsl = true; 

    SmtpServer.Send(mail); 
} catch(Exception ex) { 
    throw ex; 
} 
+0

你捕获异常? – xxpor 2011-12-19 01:24:00

它们可能会阻止传出SMTP连接,以防止垃圾邮件发送者使用他们的服务发送垃圾邮件。您应该检查您收到的错误消息并检查您的服务器主机的策略。

+0

连接尝试失败,因为连接方在一段时间后没有正确响应,或建立的连接失败,因为连接的主机未能响应74.125.53.109:25 -------这是我得到的错误。 – CsharpBeginner 2011-12-19 01:24:18

+0

看起来他们阻止了25端口:-( – 2011-12-19 01:25:06

+0

Godaddy现在阻止了端口25一段时间http://www.stemkoski.com/godaddy-dedicated-server-hosting-blocks-port-25/ – 2011-12-19 03:57:29

您需要通过godaddy smtp服务器发送您的电子邮件。在我思考之前,我遇到了同样的问题。我相信他们会给出如何通过常见问题登录的说明。

如果您有SSH访问服务器,请尝试通过25和465端口telnet smtp.google.com。如果出现超时,那么您可能无法连接到某个IP范围之外的这些端口。 端口587适用于TLS。当您使用SSL时,请尝试使用端口465.

从Godaddy托管的站点内发送内容时,您需要执行几项操作。使用他们的中继服务器发送消息(这不适用于您的开发机器,您必须在上传后才能进行现场测试)。 Here是中继服务器信息。还要确保“发件人”地址是同一个域中的电子邮件。我通常使用相同的toAddress。请参阅here了解为什么这是必要的。

这是我使用的是从一个网站发送内部GoDaddy的代码:

 btnSend.Disabled = true; 

     const string serverHost = "relay-hosting.secureserver.net"; 
     var msg = new MailMessage(toAddress, toAddress); 
     msg.ReplyTo = new MailAddress(emailFrom); 

     msg.Subject = subject; 
     msg.Body = emailBody; 
     msg.IsBodyHtml = false; 

     try 
     { 
      var smtp = new SmtpClient(); 
      smtp.Host = serverHost; 
      smtp.Credentials = new System.Net.NetworkCredential("account", "password"); 
      smtp.Send(msg); 
     } 
     catch (Exception e) 
     { 
      //Log the errors so that we can see them somewhere 
     }