In Asp.net c# i have created a web app which sends mail to desired destination and this thing works from localhost ,my live project is uploaded on solidcp panel and from that live website my mail sending function does not work.I also allowed less secure apps from gmail settings but still not working.
Is there something i should configure from solidcp panel or my code needs some changes?
What I have tried:
public void SendMail(string strTO, string strSubject, string strBody, FileUpload objFileUpload = null)
{
try
{
tblSettings objSettings = new tblSettings();
objSettings.LoadAll();
if (objSettings.RowCount > 0)
{
string strFrom = objSettings.AppSiteEmail;
if (strTO == "")
{
strTO = objSettings.AppRecepientEmail;
}
MailMessage mail = new MailMessage(strFrom, strTO, strSubject, strBody);
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = objSettings.AppSMTP;
if (!string.IsNullOrEmpty(objSettings.s_AppPortNumber))
{
smtp.Port = Convert.ToInt32(objSettings.AppPortNumber.ToString());
}
smtp.UseDefaultCredentials = false;
smtp.Timeout = 60000;
clsEncryption objEncrypt = new clsEncryption();
smtp.Credentials = new System.Net.NetworkCredential(strFrom, objEncrypt.Decrypt(objSettings.AppEmailPassword, appFunctions.strKey));
if (objSettings.s_AppIsSSL != "")
{
smtp.EnableSsl = Convert.ToBoolean(objSettings.AppIsSSL);
}
else
{
smtp.EnableSsl = false;
}
var str = objEncrypt.Decrypt(objSettings.AppEmailPassword, appFunctions.strKey.ToString());
string exe = " MailTO: " + strTO + " ,MailFrom: " + strFrom + " ,Subject :" + strSubject + " ,Host: " + smtp.Host + " ,Port: " + smtp.Port + " ,SSL: " + smtp.EnableSsl+ ",appSiteEmail: "+ objSettings.AppSiteEmail+ " ,appRecipientEmail "+ objSettings.AppRecepientEmail + " ,Password: "+str ;
LogError(exe);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mail);
LogError(strTO);
smtp = null;
mail = null;
}
}
catch (Exception ex)
{
string message = string.Format("Time: {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"));
message += Environment.NewLine;
message += "-----------------------------------------------------------";
message += Environment.NewLine;
message += string.Format("Message: {0}", ex.Message);
message += Environment.NewLine;
message += string.Format("StackTrace: {0}", ex.StackTrace);
message += Environment.NewLine;
message += string.Format("Source: {0}", ex.Source);
message += Environment.NewLine;
message += string.Format("TargetSite: {0}", ex.TargetSite.ToString());
message += Environment.NewLine;
message += "-----------------------------------------------------------";
message += Environment.NewLine;
string path = HttpContext.Current.Server.MapPath("~/Error/ErrorLog1.txt");
if (!File.Exists(path))
{
File.Delete(path);
var myFile = File.Create(path);
myFile.Close();
System.IO.File.WriteAllText(path, message);
myFile.Close();
return;
}
if (File.Exists(path))
{
File.Delete(path);
var myFile = File.Create(path);
myFile.Close();
System.IO.File.WriteAllText(path, message);
myFile.Close();
return;
}
}
}