I've been having issues sending a simple email through my ASP.NET application with my gmail account. I've looked for help as best I could, but for some reason it just keeps giving me an error and not sending any email.
Here is my code that I have set up to trigger during a button press:
protected void SubmitButton_Click(object sender, EventArgs e)
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(new System.Net.Mail.MailAddress("an email address"));
mail.From = new System.Net.Mail.MailAddress("From Email Address");
mail.Subject = "PDMS New User Request";
mail.IsBodyHtml = true;
mail.Body = "A new user has requested access" + "\n\n"
+ "Name: " + inputFName.Text.ToString() + " " + inputLName.Text.ToString()
+ "\n"
+ "Organization: " + inputOrg.Text.ToString()
+ "\n"
+ "Email: " + inputEmail.Text.ToString()
+ "\n\n"
+ "Please contact them with their login information"
+ "-PDMS System Message";
mail.IsBodyHtml = true;
System.Net.Mail.SmtpClient systemEmail = new System.Net.Mail.SmtpClient();
systemEmail.UseDefaultCredentials = false;
systemEmail.Credentials=new System.Net.NetworkCredential("From email address", "From Email Addresses' password");
systemEmail.Port = 587 ;
systemEmail.Host="smtp.gmail.com";
systemEmail.EnableSsl=true;
systemEmail.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
try
{
systemEmail.Send(mail);
Label1.Visible = true;
Label2.Visible = false;
}
catch (Exception ex)
{
Label2.Text = "Email could not be sent due to errors";
}
What is it that I am missing? I just can't seem to get this code to actually send anything - is there a setting in Gmail that I need to configure first perhaps?