In previous post, I have explained how to send email using Mailkit in ASP.NET Core. In this post, I will explain about how to send email using Gmail in ASP.NET.

The .NET framework has built-in namespace for handling email settings, which is System.Net.Mail namespace. In the following example, I’ll use two classes from the System.Net.Mail namespace:

  • For email settings, we will use the MailMessage class and
  • For smtp settings and sending email, we will use the SmtpClient class

I guess you are aware that mails are sent via an SMTP server, so we need to integrate smtp settings such as SMTP Host, Port, Credentials etc. If you don’t know about such things don’t worry, I’ll cover all these things in this tutorial.

What is SMTP?

SMTP stands for Simple Mail Transfer Protocol. It provides set of communication settings that allows our software or web applications to transmit or send emails over the internet with configured smtp settings.

Required Gmail SMTP Settings

Following are the list of SMTP settings that are mainly required to send emails:

  • SMTP Server/Host: It’s a server name. For example, smtp.gmail.com
  • SMTP Username: Your full email address. For example, [email protected]
  • SMTP Password: Your account password
  • SMTP port (TLS): 587
  • SMTP port (SSL): 465
  • SMTP TLS/SSL required: yes

Note: If you have your own email provider and you want to use that details then you can use that too.

Now we have all required details available, so we can go ahead and implement code to test mail using Gmail smtp.

HTML Markup Code To Send Mail – [.aspx]

Following is the complete HTML markup code for .aspx page shown as below:

Following is the code that we need to use for sending emails, choose your language from below.

Send Email Using Gmail SMTP Settings Sample Code – [C#]

Add the following namespace that is required for sending emails:

Now following is the code that we can use to send emails using MailMessage class:

Send Email Using Gmail SMTP Settings Sample Code – [Vb.net]

Add the following namespace that is required for sending emails:

Now following is the code that we can use to send emails using MailMessage class:

Conclusion

Done! You have learned about how to send email using Gmail in Asp.net Core. Now, you can implement it to your website and hope above article is useful. Please feel free to share this article if it is useful.

Leave a comment

Your email address will not be published.