unable to connect to the remote server c# smtp gmail

Aug 19 2017 3:41 AM
public string SendMail(string Emailids, string subject, string body)
{
string eroorMessage = string.Empty;
try
{
string hostname = ConfigurationManager.AppSettings["contactHost"];
int portno = Convert.ToInt32(ConfigurationManager.AppSettings["contactPort"]);
string frommailid = ConfigurationManager.AppSettings["contactMail"];
string frompwd = ConfigurationManager.AppSettings["contactPassword"];
var sendlist = Emailids.Split(',');
var Tolen = sendlist.Length;
string to = "";
if (Tolen == 1)
{
to = Emailids.Trim(',');
}
else
{
for (int i = 0; i < Tolen; i++)
{
to = sendlist[i];
}
}
var smptClient = new SmtpClient(hostname, portno)
{
Credentials = new NetworkCredential(frommailid, frompwd),
EnableSsl = true
};
smptClient.Send(frommailid, to, subject, body);
return "scucess";
}
catch (Exception ex)
{
eroorMessage = ex.Message.ToString() + ex.StackTrace.ToString() + ex.InnerException.ToString();
return eroorMessage;
}
}

Answers (2)