0
votes

I'm trying to send email using the following code. It is hosted in godaddy.

MailMessage mail = new MailMessage("[email protected]", "[email protected]");
MailMessage mail = new MailMessage("[email protected]", "[email protected]");
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)}

2
Do you really try to send an email via Google´s SMTP server without any authentication? Or is this just sample data and your real SMTP-Server is not reachable via internet? This would be a spammer´s dream.Dirk Huber
@AndrewMorton this is not needed, as Google is sending the mail, not GoDaddy's SMTP server.George Chondrompilas
two things, you need authentication as others have said, second you need to enable SSL in your codeDaniel Manta
@GeorgeChond Thanks, it seemed a bit much. I edited my comment accordingly.Andrew Morton

2 Answers

0
votes

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

-1
votes

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