I am trying to send mail from Gmail and it's sent successfully in localhost but when publish the website on the server didn't send mail. website publish on somee and GoDaddy hosting server. when website is published then some exception has been generating as "Mail sending failed.".
Below function which I have used.
public static void SendMailMessage(string subject, string body, string receiverEmail)
{
try
{
var host = ConfigurationManager.AppSettings["EMAIL_SMTP_HOST"];
var port = ConfigurationManager.AppSettings["EMAIL_SMTP_PORT"];
var isSSL = ConfigurationManager.AppSettings["EMAIL_ENABLESSL"] == "1" ? true : false;
var isUseDefaultCredentials = ConfigurationManager.AppSettings["EMAIL_USE_DEFAULT_CREDENTIALS"] == "1" ? true : false;
var userName = ConfigurationManager.AppSettings["EMAIL_USERNAME"];
var password = ConfigurationManager.AppSettings["EMAIL_PASSWORD"];
var objMailMsg = new MailMessage
{
From = new MailAddress(userName),
Subject = subject,
Body = body,
IsBodyHtml = true,
Priority = MailPriority.High
};
objMailMsg.To.Add(new MailAddress(receiverEmail));
using (var msmtpClient = new SmtpClient())
{
msmtpClient.Host = host;
msmtpClient.Port = Convert.ToInt32(port);
msmtpClient.Credentials = new NetworkCredential(userName, password);
msmtpClient.EnableSsl = isSSL;
msmtpClient.UseDefaultCredentials = isUseDefaultCredentials;
msmtpClient.Send(objMailMsg);
}
}
catch (Exception ex)
{
// throw;
var url = ConfigurationManager.AppSettings["ApiPortalDomain"];
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(url + "Upload/Product/WriteLines2.txt"))
{
file.WriteLine(ex.Message);
}
//Console.Write(ex);
}
}
Also, I have tried this question answer but didn't succeed.