发送邮件失败 - c# - godaddy

时间:2016-12-25 16:57:51

标签: c# smtpclient

我尝试使用以下代码发送电子邮件。它托管在godaddy中。

MailMessage mail = new MailMessage("from@gmail.com", "to@gmail.com");
MailMessage mail = new MailMessage("from@gmail.com", "to@gmail.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.gmail.com";
mail.Subject = "Test email";
string body;
using (var sr = new StreamReader(HttpContext.Current.Server.MapPath("~/App_Data/Template/") + "Email.html"))
{
    body = sr.ReadToEnd();
}
string messageBody = string.Format(body, name, expDate);
mail.Body = messageBody;
Attachment doc = new Attachment(HttpContext.Current.Server.MapPath("~/App_Data/class_3b.pdf"));
mail.Attachments.Add(doc);
client.Send(mail);

但我收到错误:

  

{System.Net.Sockets.SocketException(0x80004005):连接尝试   失败,因为关联方在a之后没有正确回应   一段时间,或建立的连接失败,因为连接   主持人未能回复74.125.130.109:25 at   System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,   SocketAddress(socketAddress)at   System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,   插座s4,插座s6,插座& socket,IPAddress&地址,   ConnectSocketState状态,IAsyncResult asyncResult,Exception&   例外)}

2 个答案:

答案 0 :(得分:0)

您需要进行身份验证。例如,请参阅Send Email via C# through Google Apps account。谷歌甚至检查认证地址和"来自"地址对应...

答案 1 :(得分:-1)

要使用System.Net.Mail发送邮件,您需要使用web.config的这些值在应用程序的mailSettings:文件中配置SMTP服务

<system.net>
    <mailSettings>
      <smtp from="your email address">
        <network host="relay-hosting.secureserver.net" port="25" userName="your email address" password="******" defaultCredentials="true"/>
      </smtp>
    </mailSettings>
  </system.net>

后面的代码:

MailMessage message = new MailMessage();
message.From = new MailAddress("your email address");

message.To.Add(new MailAddress("your recipient"));

message.Subject = "your subject";
message.Body = "content of your email";

SmtpClient client = new SmtpClient();
client.Send(message);