C#Godaddy服务器上的电子邮件发送错误

时间:2013-09-09 05:27:07

标签: c#

我有一个托管在Godaddy服务器上的Web应用程序。我被困在发送邮件错误。我把我的代码放在这里

public void Mailing()
{
 MailMessage message = new MailMessage();
 try
{
    MailAddress fromAddress = new MailAddress("mailadress@server.net");
    message.From = fromAddress;
    message.To.Add("toresive@server.com");
    message.CC.Add("ccresive@server.com");

    message.Subject = "Hello client";
    message.IsBodyHtml= true;
    string body = "Hello!<br> How are you?";

    message.Body = body;

    SmtpClient smtpClient = new SmtpClient();
    smtpClient.Host = "relay-hosting.secureserver.net";
    smtpClient.Port = 465;
    smtpClient.EnableSsl = false;
    smtpClient.UseDefaultCredentials = false;
    smtpClient.Credentials = new NetworkCredential("mailadress@server.net", "Password");
   // AdvancedIntellect.Ssl.SslSocket ssl = new AdvancedIntellect.Ssl.SslSocket();
    smtpClient.Send(message);
    smtpClient.Timeout = 10000;
    }
    catch (Exception ex)
    {
        label2.Text = ex.Message;
    }
}

}

但我收到了错误

  

System.Net.Mail.SmtpException:发送邮件失败。 ---&GT;   System.Net.WebException:无法连接到远程服务器---&gt;   System.Net.Sockets.SocketException:连接尝试失败   因为关联方在一段时间后没有正确回应   时间或已建立的连接因连接的主机而失败   未能回应68.178.232.62:25 at   System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,   SocketAddress(socketAddress)at   System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,   插座s4,插座s6,插座&amp; socket,IPAddress&amp;地址,   ConnectSocketState状态,IAsyncResult asyncResult,Int32超时,   异常和放大器;例外)---内部异常堆栈跟踪结束--- at   System.Net.ServicePoint.GetConnection(PooledStream PooledStream,   对象所有者,布尔异步,IPAddress&amp;地址,插座&amp; abortSocket,   插座及放大器; abortSocket6,Int32 timeout)at   System.Net.PooledStream.Activate(Object owningObject,Boolean async,   Int32超时,GeneralAsyncDelegate asyncCallback)at   System.Net.PooledStream.Activate(Object owningObject,   GeneralAsyncDelegate asyncCallback)at   System.Net.ConnectionPool.GetConnection(Object owningObject,   GeneralAsyncDelegate asyncCallback,Int32 creationTimeout)at   System.Net.Mail.SmtpConnection.GetConnection(的ServicePoint   servicePoint)at   System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)   在System.Net.Mail.SmtpClient.GetConnection()处   System.Net.Mail.SmtpClient.Send(MailMessage消息)---内部结束   异常堆栈跟踪--- at   System.Net.Mail.SmtpClient.Send(MailMessage消息)at   Form1.Mailing()in   C:\的Inetpub \虚拟主机\ pay2school.net \的httpdocs \ IPTest \管理员\ Form1.aspx.cs:线   139

我依赖此错误消失。 :( 我用smtpout.secureserver.net更改了我的SMTP服务器但没有工作。 请帮帮我。提前谢谢。

3 个答案:

答案 0 :(得分:1)

EnableSsl应该是真的。 如果您需要超时,则应在发送电子邮件之前设置其属性,尝试按此调整

SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "relay-hosting.secureserver.net";
smtpClient.Port = 465;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = 
          new NetworkCredential("mailadress@server.net", "Password");

...
smtpClient.Timeout = 10000;
smtpClient.Send(message);

答案 1 :(得分:0)

当我说我以前的C#工作代码(使用Godaddy服务器发送电子邮件)不再发送电子邮件时,我遇到了同样的问题。所以我花了5个小时来找到实际问题

问题是Godaddy 更改了服务器上的设置。因此,您无法使用以下方式发送电子邮件:

  1. &#34; smtpout.secureserver.net&#34;主机。
  2. 本地主机(本地系统)。
  3. 我通过做两件事来解决这个问题:

    1. 使用SmtpClient主机作为&#34; relay-hosting.secureserver.net&#34;。

    2. 将我的网页放在我的Godaddy共享服务器上(由于Godaddy添加了安全性,localhost仍然无法发送smtp电子邮件)。

    3. 这是我这边的正确的工作代码

      MailMessage mailMessage = new MailMessage();
      mailMessage.From = new MailAddress("test@yourdomain.com");
      mailMessage.To.Add(new MailAddress("senderemail@gmail.com"));
      
      mailMessage.Subject = "hello";
      mailMessage.IsBodyHtml = true;
      mailMessage.Body = "hello how are you";
      
      SmtpClient client = new SmtpClient();
      client.Credentials = new System.Net.NetworkCredential("test@yourdomain.com", "yourpassword");
      client.Host = "relay-hosting.secureserver.net";
      client.Send(mailMessage);
      

      在上面的代码中,看看我添加了主机

      client.Host = "relay-hosting.secureserver.net";
      

      邮件将在25到30秒的时间内到达。如果您仍然无法找到该电子邮件,请检查您的垃圾邮件文件夹。

      <强>证明

      在撰写此答案时,我测试了我的代码并找到了发送电子邮件到我的Gmail收件箱。请参见下图作为证明: enter image description here

答案 2 :(得分:-1)

检查以下代码。您必须更改client.Host = "smtpout.secureserver.net."

MailMessage msg = new MailMessage(txtemail.Text, "shopover@vibrantinfosystems.com");
msg.Subject = "Contact Us";
string body = "Name: " + txtname.Text.Trim() + "";
body += "<br />Email:" + txtemail.Text + "";
body += "<br /><br /> Subject :" + txtsubject.Text + "";
body += "<br /><br />Message : " + txtmessage.Text + "";
msg.Body = body;

msg.IsBodyHtml = true;

SmtpClient client = new SmtpClient();
client.Host = "smtpout.secureserver.net.";

client.Send(msg);
client.Timeout = 10000;
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Mail Sent Successfully!!');", true);