Tuesday, June 10, 2014

How to send mail from GoDaddy server in ASP.NET C#


I found some errors in sending mail from a GoDaddy server, and after searching a lot I found the correct solution to send mail.

First add these namespaces in your project





using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.Net.Mail;

Now the send mail method is



public void Send_User_Confirmation_Mail()
{
    MailMessage mail = new MailMessage();
    mail.To.Add("<email to send>");
    mail.From = new MailAddress("<send mail>");
 
    mail.Subject = "Account Created.";
          
    string Body = "<body>";
 
    mail.Body = Body;
    mail.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "relay-hosting.secureserver.net";
    smtp.EnableSsl = true;
    smtp.Credentials =  
      new System.Net.NetworkCredential("<send email>"), "<send password>");
    ServicePointManager.ServerCertificateValidationCallback =  
      delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
      { return true; };
    smtp.Port = 25;
    smtp.Send(mail);
}

These are main things to add with your normal mail sending code.



smtp.Host = "relay-hosting.secureserver.net";
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["Send_Mail_From_Email"].ToString(), ConfigurationManager.AppSettings["Send_Mail_From_Password"].ToString());
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
smtp.Port = 25;

Before adding there 3rd line of credential I was getting error of some invalid certificate issue to send a mail. But adding these all the issue become normal and mail is sending without any problem.

1 comment:

  1. ZeroGravity Solutions is a web design & development firm based in Delhi-NCR, INDIA. since last few years we've made a reputation for building websites that look great and are completely easy-to-use.
    Adopting ethical and transparent practices in all of our business dealings, we have successfully garnered a long list of clients. We diligently work to understand the clients’ requirements and incorporate the same in our solutions.

    http://www.zerogravitysol.com

    ReplyDelete

Popular Posts

Pageviews