Compare commits

..

No commits in common. "45e86fd063dd80dd40336c75d0b0cfdd3a1519d3" and "86c7b6bb8d0f463e9cdd2c7ae82e3425ab0364c0" have entirely different histories.

3 changed files with 58 additions and 111 deletions

View File

@ -3,32 +3,60 @@ using System.Net.Mail;
namespace ConsoleApp1; namespace ConsoleApp1;
public class MailService(SmtpSettings smtpSettings) public class MailService
{ {
/// <summary> private readonly SmtpSettings _smtpSettings;
/// Sends a plain text email to the specified recipient.
/// </summary> public MailService(SmtpSettings smtpSettings)
/// <param name="to">The recipient's email address.</param>
/// <param name="subject">The subject of the email.</param>
/// <param name="body">The body of the email.</param>
public async Task SendEmailText(string to, string subject, string body)
{ {
await SendEmailInternal(to, subject, body, isHtml: false); _smtpSettings = smtpSettings;
} }
/// <summary> public void SendEmail(string to, string subject, string body)
/// Sends an HTML email to the specified recipient using a file as the body.
/// </summary>
/// <param name="to">The recipient's email address.</param>
/// <param name="subject">The subject of the email.</param>
/// <param name="filePath">The file path of the HTML file to be used as the body of the email.</param>
/// <param name="replacements">Optional dictionary of placeholder replacements to be applied to the HTML body.</param>
public async Task SendEmailHtmlFile(string to, string subject, string filePath,
Dictionary<string, string>? replacements = null)
{ {
string body = await File.ReadAllTextAsync(filePath); using (var client = new SmtpClient(_smtpSettings.SmtpServer, _smtpSettings.SmtpPort))
{
client.Credentials = new NetworkCredential(_smtpSettings.SmtpUser, _smtpSettings.SmtpPassword);
client.EnableSsl = true;
if (replacements != null) var mailMessage = new MailMessage
{
From = new MailAddress(_smtpSettings.SmtpFrom),
Subject = subject,
Body = body,
IsBodyHtml = true,
};
mailMessage.To.Add(to);
client.Send(mailMessage);
}
}
private void SendEmailHtml(string to, string subject, string body)
{
using var client = new SmtpClient(_smtpSettings.SmtpServer, _smtpSettings.SmtpPort);
client.Credentials = new NetworkCredential(_smtpSettings.SmtpUser, _smtpSettings.SmtpPassword);
client.EnableSsl = true;
var mailMessage = new MailMessage
{
From = new MailAddress(_smtpSettings.SmtpFrom),
Subject = subject,
Body = body,
IsBodyHtml = true,
};
mailMessage.To.Add(to);
client.Send(mailMessage);
}
public void SendEmailHtmlFile(string to, string subject, string filePath, Dictionary<string, string>? replacements = null)
{
string body = System.IO.File.ReadAllText(filePath);
if (replacements is not null)
{ {
foreach (var replacement in replacements) foreach (var replacement in replacements)
{ {
@ -36,32 +64,7 @@ public class MailService(SmtpSettings smtpSettings)
} }
} }
await SendEmailInternal(to, subject, body, isHtml: true); SendEmailHtml(to, subject, body);
}
/// <summary>
/// Sends an email to the specified recipient.
/// </summary>
/// <param name="to">The recipient's email address.</param>
/// <param name="subject">The subject of the email.</param>
/// <param name="body">The body of the email.</param>
/// <param name="isHtml">Indicates whether the body of the email is HTML.</param>
private async Task SendEmailInternal(string to, string subject, string body, bool isHtml)
{
using var client = new SmtpClient(smtpSettings.SmtpServer, smtpSettings.SmtpPort);
client.Credentials = new NetworkCredential(smtpSettings.SmtpUser, smtpSettings.SmtpPassword);
client.EnableSsl = true;
var mailMessage = new MailMessage
{
From = new MailAddress(smtpSettings.SmtpFrom),
Subject = subject,
Body = body,
IsBodyHtml = isHtml
};
mailMessage.To.Add(to);
await client.SendMailAsync(mailMessage);
} }
} }

View File

@ -1,10 +1,12 @@
using System.Text.Json; using System;
using System.IO;
using System.Text.Json;
namespace ConsoleApp1 namespace ConsoleApp1
{ {
class Program class Program
{ {
static async Task Main() static void Main(string[] args)
{ {
string jsonFilePath = "appsettings.json"; string jsonFilePath = "appsettings.json";
string jsonString = File.ReadAllText(jsonFilePath); string jsonString = File.ReadAllText(jsonFilePath);
@ -23,15 +25,16 @@ namespace ConsoleApp1
} }
MailService mailService = new MailService(smtpSettings); MailService mailService = new MailService(smtpSettings);
await mailService.SendEmailText("user@user.com", "Hello World", "Hello World from C#!"); mailService.SendEmail("user@user.com", "Hello World", "Hello World from C#!");
Console.WriteLine("Email sent!");
Dictionary<string, string> replacements = new Dictionary<string, string> Dictionary<string, string> replacements = new Dictionary<string, string>
{ {
{ "name", "Jonas" } { "name", "Jonas" }
}; };
await mailService.SendEmailHtmlFile("user@user.com" , "Hello World", "email.html", replacements); mailService.SendEmailHtmlFile("user@user.com" , "Hello World", "email.html", replacements);
Console.WriteLine("Email sent!");
} }
} }

View File

@ -1,59 +0,0 @@
# 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);
```