| .idea/.idea.ConsoleApp1/.idea | ||
| ConsoleApp1 | ||
| .gitignore | ||
| ConsoleApp1.sln | ||
| ReadMe.md | ||
MailService
Overview
MailService is a simple C# console application that demonstrates how to send plain text and HTML emails using SMTP settings defined in a configuration file. The application reads SMTP settings from a JSON file (appsettings.json), and uses these settings to send emails through an SmtpClient.
Features
- Send plain text emails.
- Send HTML emails using an HTML template file.
- Replace placeholders in the HTML template with actual values.
Project Structure
Program.cs: The main entry point of the application. It reads the SMTP settings fromappsettings.json, initializes theMailService, and sends both plain text and HTML emails.SmtpSettings.cs: A class that holds the SMTP configuration settings.MailService.cs: A class that handles the logic for sending emails.appsettings.json: A JSON file that contains the SMTP configuration settings.email.html: An HTML template file used for sending HTML emails.
Configuration
The SMTP settings are stored in the appsettings.json file. Here is an example configuration:
{
"smtpServer": "smtp.office365.com",
"smtpPort": 587,
"smtpUser": "user",
"smtpPassword": "smtpPassword",
"smtpFrom": "smpt@service.com"
}
Usage
- Clone the repository and navigate to the project directory.
- Ensure the
appsettings.jsonfile is correctly configured with your SMTP settings. - Run the application using your preferred IDE or the .NET CLI.
Sending Emails
The application sends two types of emails:
-
Plain Text Email:
- Sent using the
SendEmailTextmethod inMailService. - Example:
await mailService.SendEmailText("user@user.com", "Hello World", "Hello World from C#!");
- Sent using the
-
HTML Email:
- Sent using the
SendEmailHtmlFilemethod inMailService. - The method searches for placeholders in the format
{keyValue}within the HTML template and replaces them with the corresponding values from the provided dictionary. - Example:
Dictionary<string, string> replacements = new Dictionary<string, string> { { "name", "Jonas" } }; await mailService.SendEmailHtmlFile("user@user.com", "Hello World", "email.html", replacements);
- Sent using the