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 comments:

Post a Comment

Follow by Email

Popular Posts

Pageviews