简体   繁体   English

发送邮件失败-C#-Godaddy

[英]Failure sending mail - c# - godaddy

I'm trying to send email using the following code. 我正在尝试使用以下代码发送电子邮件。 It is hosted in godaddy. 它托管在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);

But I'm getting error: 但是我遇到了错误:

{System.Net.Sockets.SocketException (0x80004005): 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 74.125.130.109:25 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)} {System.Net.Sockets.SocketException(0x80004005):连接尝试失败,因为一段时间后连接方未正确响应,或者由于连接的主机未能在System.Net上响应74.125.130.109:25,连接建立失败.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,SocketAddress socketAddress)at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,Socket s4,Socket s6,Socket&socket,IPAddress&address,ConnectSocketState状态,IAsyncResult asyncResult,Exception&exception)}

You need to authenticate. 您需要验证。 See Send Email via C# through Google Apps account for example. 例如,请参阅通过C#通过Google Apps帐户发送电子邮件 Google even check that the authentication address and the "from" address corresponds... Google甚至检查验证地址和“发件人”地址是否对应...

To send mail using System.Net.Mail, you need to configure your SMTP service in your application's web.config file using these values for mailSettings: 要使用System.Net.Mail发送邮件,您需要使用以下mailSettings:值在应用程序的web.config文件中配置SMTP服务mailSettings:

<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>

Code Behind: 背后的代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM