Kokombads Kokombads - 1 year ago 70
C# Question

Email sending always fails, what is wrong with my contact us code

I am currently working with my Contact Us Page and I have this method:

private bool SendEmail()

string subject = SubjectTextBox.Text;
const string goingTo = "dummie@gmail.com";
const string smtpUser = "dummie@gmail.com";
const string smtpPassword = "dummypass";

SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.Port = 587;
smtp.UseDefaultCredentials = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = true;

MailMessage mail = new MailMessage();

mail.From = new MailAddress(smtpUser);
mail.Subject = subject;
mail.IsBodyHtml = true;

string collectBody = string.Format("From: {0}<br/>Email: {1}<br/>Contact: {2}<br/><br/><br/><br/>{3}",
NameTextBox.Text, EmailTextBox.Text, ContactTextBox.Text, InquiryTextBox.Text);

mail.Body = collectBody;

smtp.Host = "smtp.gmail.com";

smtp.Credentials = new NetworkCredential(smtpUser, smtpPassword);

catch (Exception)

return false;

return true;

in my personal laptop, this works, but in my workplace's laptop, it doesn't. I tried almost all the given codes but it always fails.

Answer Source

Please Turn off your firewall or disabled antivirus ? If it works, then your firewall or antivirus is blocking something.It may your IP of workplace has blocked for sending mail.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download