60 lines
2.2 KiB
Markdown
60 lines
2.2 KiB
Markdown
# 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:
|
|
|
|
```json
|
|
{
|
|
"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:
|
|
```csharp
|
|
Dictionary<string, string> replacements = new Dictionary<string, string>
|
|
{
|
|
{ "name", "Jonas" }
|
|
};
|
|
await mailService.SendEmailHtmlFile("user@user.com", "Hello World", "email.html", replacements);
|
|
```
|