Reputation: 1122
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)}
Upvotes: 0
Views: 2518
Reputation: 4262
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);
Upvotes: -1
Reputation: 912
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...
Upvotes: 0