Compare commits
2 Commits
86c7b6bb8d
...
45e86fd063
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
45e86fd063 | ||
|
|
a42cba1ec8 |
@ -3,60 +3,32 @@ using System.Net.Mail;
|
|||||||
|
|
||||||
namespace ConsoleApp1;
|
namespace ConsoleApp1;
|
||||||
|
|
||||||
public class MailService
|
public class MailService(SmtpSettings smtpSettings)
|
||||||
{
|
{
|
||||||
private readonly SmtpSettings _smtpSettings;
|
/// <summary>
|
||||||
|
/// Sends a plain text email to the specified recipient.
|
||||||
public MailService(SmtpSettings smtpSettings)
|
/// </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>
|
||||||
|
public async Task SendEmailText(string to, string subject, string body)
|
||||||
{
|
{
|
||||||
_smtpSettings = smtpSettings;
|
await SendEmailInternal(to, subject, body, isHtml: false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SendEmail(string to, string subject, string body)
|
/// <summary>
|
||||||
|
/// 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)
|
||||||
{
|
{
|
||||||
using (var client = new SmtpClient(_smtpSettings.SmtpServer, _smtpSettings.SmtpPort))
|
string body = await File.ReadAllTextAsync(filePath);
|
||||||
{
|
|
||||||
client.Credentials = new NetworkCredential(_smtpSettings.SmtpUser, _smtpSettings.SmtpPassword);
|
|
||||||
client.EnableSsl = true;
|
|
||||||
|
|
||||||
var mailMessage = new MailMessage
|
if (replacements != null)
|
||||||
{
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
@ -64,7 +36,32 @@ public class MailService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SendEmailHtml(to, subject, body);
|
await SendEmailInternal(to, subject, body, isHtml: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -1,12 +1,10 @@
|
|||||||
using System;
|
using System.Text.Json;
|
||||||
using System.IO;
|
|
||||||
using System.Text.Json;
|
|
||||||
|
|
||||||
namespace ConsoleApp1
|
namespace ConsoleApp1
|
||||||
{
|
{
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
static void Main(string[] args)
|
static async Task Main()
|
||||||
{
|
{
|
||||||
string jsonFilePath = "appsettings.json";
|
string jsonFilePath = "appsettings.json";
|
||||||
string jsonString = File.ReadAllText(jsonFilePath);
|
string jsonString = File.ReadAllText(jsonFilePath);
|
||||||
@ -25,16 +23,15 @@ namespace ConsoleApp1
|
|||||||
}
|
}
|
||||||
|
|
||||||
MailService mailService = new MailService(smtpSettings);
|
MailService mailService = new MailService(smtpSettings);
|
||||||
mailService.SendEmail("user@user.com", "Hello World", "Hello World from C#!");
|
await mailService.SendEmailText("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" }
|
||||||
};
|
};
|
||||||
|
|
||||||
mailService.SendEmailHtmlFile("user@user.com" , "Hello World", "email.html", replacements);
|
await mailService.SendEmailHtmlFile("user@user.com" , "Hello World", "email.html", replacements);
|
||||||
Console.WriteLine("Email sent!");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
59
ReadMe.md
Normal file
59
ReadMe.md
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
# 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);
|
||||||
|
```
|
||||||
Loading…
Reference in New Issue
Block a user