简体   繁体   中英

Failure sending mail - c# - godaddy

I'm trying to send email using the following code. It is hosted in 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)}

You need to authenticate. See Send Email via C# through Google Apps account for example. Google even check that the authentication address and the "from" address corresponds...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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