Go to file
Jonas Hinterdorfer 45e86fd063 made async
2025-03-11 21:42:48 +01:00
.idea/.idea.ConsoleApp1/.idea init 2025-03-11 20:55:37 +01:00
ConsoleApp1 made async 2025-03-11 21:42:48 +01:00
.gitignore added gitignore 2025-03-11 20:58:39 +01:00
ConsoleApp1.sln init 2025-03-11 20:55:37 +01:00
ReadMe.md added Readme 2025-03-11 21:42:35 +01:00

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 from appsettings.json, initializes the MailService, 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

  1. Clone the repository and navigate to the project directory.
  2. Ensure the appsettings.json file is correctly configured with your SMTP settings.
  3. Run the application using your preferred IDE or the .NET CLI.

Sending Emails

The application sends two types of emails:

  1. Plain Text Email:

    • Sent using the SendEmailText method in MailService.
    • Example: await mailService.SendEmailText("user@user.com", "Hello World", "Hello World from C#!");
  2. HTML Email:

    • Sent using the SendEmailHtmlFile method in MailService.
    • 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);