sending email on local machine is not working.
- by haansi
I am using my gmail's email account to send emails in asp.net website. It works fine on hosting server but it donot works if I try to sent email on loclserver.
Please guide me what I should do to make it sending emails even on localserver ? Do I need to install some smtp server on my local machine ? I have not installed any smtp server on my machine. How and where from I can get smtp server and kindly also guide how I can do its setting to use on local machine.
Thnaks
Here is my Code
public string SendEmail(Email email)
{
string errmsg = null;
if (dt != null)
{
try
{
dt = systemrep.GetSystemInfo();
dr = dt.Rows[0];
From = dr["nm_EmailFrom"].ToString();
SMTP = dr["nm_SMTP"].ToString();
Port = dr["amt_Port"].ToString();
EmailId = dr["nm_emailUserId"].ToString();
EmailPassword = dr["nm_emailPassword"].ToString();
DefaultCredations = Convert.ToBoolean(dr["ind_Credentials"].ToString());
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
NetworkCredential mailAuthentication = new NetworkCredential(EmailId, EmailPassword);
message.To.Add(new MailAddress(email.To));
message.From = new MailAddress(From);
message.IsBodyHtml = true;
message.Subject = email.Subject;
message.Body = email.Message;
smtp.UseDefaultCredentials = DefaultCredations;
smtp.EnableSsl = true;
smtp.Port = 25;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Host = SMTP;
smtp.Credentials = new NetworkCredential(EmailId, EmailPassword);
smtp.Send(message);
}
catch (SmtpException smtpEx)
{
errmsg = string.Format("alert('There was a problem in sending the email: {0}');", smtpEx.Message.Replace("'", "\\'"));
}
catch (Exception generalEx)
{
errmsg = string.Format("alert('There was a general problem: {0}');", generalEx.Message.Replace("'", "\\'"));
}
}
else
errmsg = "An error accured whilte getting email settings from database, process couldn't be completed";
return errmsg;
}
}