Kokombads Kokombads - 1 month ago 6
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.To.Add(goingTo);
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;
mail.CC.Add("dummiesekend@gmail.com");

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

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

try
{
smtp.Send(mail);
}
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

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.

Comments