Header add


In this article we will discuss how to send emails with attachment in ASP .Net Core using Gmail SMTP.

Default SMTP Settings for Gmail
  • Gmail SMTP server address: smtp.gmail.com.
  • Gmail SMTP username: Your Gmail address (for example, example@gmail.com)
  • Gmail SMTP password: Your Gmail password.
  • Gmail SMTP port (TLS): 587.
  • Gmail SMTP port (SSL): 465.
  • Gmail SMTP TLS/SSL required: Yes.
MailMessage Class Properties (Mailmessage are reference from System.Net.Mail)
  • From – Sender’s email address.
  • To – Recipient(s) Email Address.
  • CC – Carbon Copies (if any).
  • BCC – Blind Carbon Copies (if any).
  • Subject – Subject of the Email.
  • Body – Body of the Email.
  • IsBodyHtml – Specify whether body contains text or HTML mark up.
  • Attachments – Attachments (if any).
  • ReplyTo – ReplyTo Email address.
Let's create a .NET Core web application and see how we integrate the Gmail SMTP. We use Visual Studio 2019 with .NET Core Sdk 3.1

Model
Model class named EmailConfig with the following properties.


  • We take EmailConfig class to hold the information of Email. mailConfig class are used to hold the Gmail Credentials in appsettings.json 
  • IFormFile is the new Class for Files in .Net Core. It is a replacement of HttpPostedFileBase class.
UI
Create a View page like below; the html should like

appsettings.json. To access the value in appsettings.json, follow the link how-to-read-values-from-appsettingsjson We need to access the Gmail credentials as through dependency injection like below;
Controller
Add the Post method in HomeController like below;
Add caption
Note: If you face the secure connection error for sending email the follow the link smtp-server-requires-secure-connection.html

Finally hit the submit button and the email was sent as expected into the provided gmail account.

</> Find the Source Code in Github.com/CoreProgramm/

   Summary
 In this tutorial we discussed how to Send Email with Attachment in .NET Core using Gmail SMTP. If have any question related to this topic then give your feedback.

Post a Comment

Previous Post Next Post