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.
Create a View page like below; the html should like
Add the Post method in HomeController like below;
Add caption |
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
No comments:
Post a Comment