How to send email using C# without any nugget Package
Since you are reading this post, you want to send an email using C#.
Now let’s start coding.
Create new Project — Console Application
Create a new .NET Console Application project.
You can name your project anything! I would prefer to name it EmailSender.
Create a new class in your project and add a new method with 5parameters.
Now let's understand the code written above.
There are 5 parameters in the method SendEmail.
senderEmail: email of the sender.
senderPassword: password of the sender.
receiverEmail: email of the person whom to send the mail.
body: This parameter will have the body of your mail, it can also have html string.
subject: Subject of the mail.
We using MailMessage class which is in directory System.Net.Mail.
IsEmailValid() will check if the given emails are valid or not, this will ensure better exception handling.
Call the method, and you should see a new mail in your inbox in a few seconds.
System.Net.Mail.SmtpException: ‘The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required.
The above error can occur because of two reasons:
- Your credentials are incorrect.
To solve this error, just check your email and password once again.
2. Your Gmail account is not allowed to be access by less secure apps.
To solve this error, go to your Google account settings ⇾Security ⇾ Turn on Less secure app access.
Apart from this, if you have any other errors or exception Let me know in the comment section, I’ll be glad to help you out :)
You can also access the full project in the GitHub Repository.