1

Im trying to send an email with c#, like so:

public string SendEmail(string employeeEmail, string clientEmail, string subject, string message) { MailMessage mailMessage = new MailMessage(employeeEmail, clientEmail) { Subject = subject, Body = message, BodyEncoding = Encoding.UTF8, IsBodyHtml = true }; SmtpClient client = new SmtpClient("smtp.gmail.com", 587); NetworkCredential basicCredential1 = new NetworkCredential("email", "password"); client.EnableSsl = true; client.UseDefaultCredentials = false; client.Credentials = basicCredential1; try { if (UserExists(employeeEmail)) { if (objDAL.ClientExists(clientEmail)) { client.Send(mailMessage); return "Email sent"; } else { return "Client email not found"; } } else { return "Employee email not found."; } } catch { return "ERROR"; } } 

But it throws a 'System.Net.Mail.SmtpException' in System.Net.Mail.dll in client.Send(mailMessage); and i have no idea why. I've been searching for an answer for a days... Can someone please help me understand whats going on ? Am i missing something ?

12
  • Are you inside a corporate network with an outlook server? If so you cannot access your gmail account. Try the code from home and see if it works. Inside a corporate network all port 587 is send to the company email proxy server which will give an error if you are using your gmail account. Commented Jan 25, 2022 at 13:12
  • @jdweng No, im not in a corporate network, im home. And both employeeEmail and clientEmail were created by me for test purposes Commented Jan 25, 2022 at 13:54
  • Still if you are home your may still be behind a firewall. You are missing the FROM property which is required and has to be the same as the email account. Commented Jan 25, 2022 at 14:30
  • im using the FROM property, its the employeeEmail, and yes, im using the same email as the email account Commented Jan 25, 2022 at 14:52
  • Either the credentials are wrong, the FROM is not matching the account, or you are working inside the company firewall. Sometimes errors occur if the send (not sent) mail box is full. Commented Jan 25, 2022 at 15:08

1 Answer 1

1

Regarding the exception that you are getting, you have to enable Less Secure Sign-In (or Less secure app access) in your google account.

After sign into google account, go to:

https://www.google.com/settings/security/lesssecureapps or https://myaccount.google.com/lesssecureapps

and enable the option. Once you do this step, you should be able to send e-mails.

Sign up to request clarification or add additional context in comments.

4 Comments

My god... i really though this was already allowed since i've allowed it before but it wasnt. Thank you very much, i've been stuck here for days.
Also, i think that with this implementation, i can only send emails from gmail to gmail. Is there any way i can send emails from gmail, to another domains such as hotmail for example ?
No, you can send emails to any domain using this. Also don't forget to up-vote if the solution was helpful for you.
ok, i tried and yes, it sends to different domains aswell. Thank you so much

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.