'Sending Email Using SMTP Bluehost Server
I use ASP.NET WEB API and I want to send an email from my application from an email created by https://www.bluehost.com/
Here is the configuration in the web.config file:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="[email protected]">
<network host="mail.domain.com"
port="465"
userName="[email protected]"
password="*****"
enableSsl="true" />
</smtp>
</mailSettings>
</system.net>
Here is my code:
SmtpClient smtpClient = new SmtpClient();
smtpClient.Timeout = 120000;
MailMessage mail = new MailMessage("[email protected]", "[email protected]");
mail.Body = "Here is the body of my email";
mail.IsBodyHtml = true;
smtpClient.Send(mail);
I am receiving the following error:
Network Error (tcp_error)
A communication error occurred: "Operation timed out" The Web Server may be down, too busy, or experiencing other problems preventing it from responding to requests. You may wish to try again at a later time.
Note that I have tried configuring SMTP directly in the above code, but still it didn't work.
I had tested from host smtp.gmail.com and it worked fine, so I guess the issue is from the new host.
Any help is much appreciated.
Solution 1:[1]
Please try this below code segment.
SmtpClient smtpClient = new SmtpClient();
smtpClient.Port = 465; // can check port for ssl - 587 and non ssl - 25
smtpClient.Host = "mail.domain.com";
smtpClient.EnableSsl = true;
smtpClient.Timeout = 10000;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("[email protected]","password");
MailMessage mail = new MailMessage("[email protected]", "[email protected]", "test", "test");
mail.BodyEncoding = UTF8Encoding.UTF8;
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
smtpClient.Send(mail);
Solution 2:[2]
For SMTP, I used port 26 and mail.company.com. My host server is bluehost.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ritesh Roushan |
Solution 2 | danmbuen |