Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using EmailService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace EmailApp.Controllers;

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries =
[
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
];

private readonly IEmailSender _emailSender;

public WeatherForecastController(IEmailSender emailSender)
{
_emailSender = emailSender;
}

[HttpGet(Name = "GetWeatherForecast")]
public async Task<IEnumerable<WeatherForecast>> Get()
{
var message = new Message(["codemazetest@mailinator.com"],
"Test email async",
"This is the content from our async email.",
null);
await _emailSender.SendEmailAsync(message);

return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}

[HttpPost]
public async Task<IEnumerable<WeatherForecast>> Post()
{
var files = Request.Form.Files.Any() ? Request.Form.Files : new FormFileCollection();

var message = new Message(["codemazetest@mailinator.com"], "Test mail with Attachments", "This is the content from our mail with attachments.", files);
await _emailSender.SendEmailAsync(message);

return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>codemaze-send-email-mailkit</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.13" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\EmailService\EmailService.csproj" />
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions dotnet-client-libraries/SendEmailWithMailKit/EmailApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using EmailService;
using Microsoft.AspNetCore.Http.Features;

var builder = WebApplication.CreateBuilder(args);

var emailConfig = builder.Configuration
.GetSection("EmailConfiguration")
.Get<EmailConfiguration>()
?? throw new InvalidOperationException("The EmailConfiguration section is missing.");
builder.Services.AddSingleton(emailConfig);
builder.Services.AddScoped<IEmailSender, EmailSender>();

builder.Services.Configure<FormOptions>(o =>
{
o.ValueLengthLimit = int.MaxValue;
o.MultipartBodyLengthLimit = int.MaxValue;
o.MemoryBufferThreshold = int.MaxValue;
});

builder.Services.AddControllers();
builder.Services.AddOpenApi();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5115",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7076;http://localhost:5115",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace EmailApp;

public class WeatherForecast
{
public DateOnly Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

public string? Summary { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"EmailConfiguration": {
"From": "codemazetest@gmail.com",
"SmtpServer": "smtp.gmail.com",
"Port": 465,
"Username": "codemazetest@gmail.com"
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace EmailService;

public class EmailConfiguration
{
public string From { get; set; } = string.Empty;
public string SmtpServer { get; set; } = string.Empty;
public int Port { get; set; }
public string UserName { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using MailKit.Net.Smtp;
using MimeKit;

namespace EmailService;

public class EmailSender : IEmailSender
{
private readonly EmailConfiguration _emailConfig;

public EmailSender(EmailConfiguration emailConfig)
{
_emailConfig = emailConfig;
}

public void SendEmail(Message message)
{
var emailMessage = CreateEmailMessage(message);

Send(emailMessage);
}

public async Task SendEmailAsync(Message message)
{
var mailMessage = CreateEmailMessage(message);

await SendAsync(mailMessage);
}

private MimeMessage CreateEmailMessage(Message message)
{
var emailMessage = new MimeMessage();
emailMessage.From.Add(MailboxAddress.Parse(_emailConfig.From));
emailMessage.To.AddRange(message.To);
emailMessage.Subject = message.Subject;

var bodyBuilder = new BodyBuilder { HtmlBody = $"<h2 style='color:red;'>{message.Content}</h2>" };

if (message.Attachments != null && message.Attachments.Any())
{
byte[] fileBytes;
foreach (var attachment in message.Attachments)
{
using (var ms = new MemoryStream())
{
attachment.CopyTo(ms);
fileBytes = ms.ToArray();
}

bodyBuilder.Attachments.Add(attachment.FileName, fileBytes, ContentType.Parse(attachment.ContentType));
}
}

emailMessage.Body = bodyBuilder.ToMessageBody();
return emailMessage;
}

private void Send(MimeMessage mailMessage)
{
using var client = new SmtpClient();

client.Connect(_emailConfig.SmtpServer, _emailConfig.Port, true);
client.Authenticate(_emailConfig.UserName, _emailConfig.Password);

client.Send(mailMessage);
client.Disconnect(true);
}

private async Task SendAsync(MimeMessage mailMessage)
{
using var client = new SmtpClient();

await client.ConnectAsync(_emailConfig.SmtpServer, _emailConfig.Port, true);
await client.AuthenticateAsync(_emailConfig.UserName, _emailConfig.Password);

await client.SendAsync(mailMessage);
await client.DisconnectAsync(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="MailKit" Version="4.18.1" />
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="10.0.13" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EmailService;

public interface IEmailSender
{
void SendEmail(Message message);
Task SendEmailAsync(Message message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.AspNetCore.Http;
using MimeKit;

namespace EmailService;

public class Message
{
public List<MailboxAddress> To { get; set; }
public string Subject { get; set; }
public string Content { get; set; }

public IFormFileCollection? Attachments { get; set; }

public Message(IEnumerable<string> to, string subject, string content, IFormFileCollection? attachments)
{
To = new List<MailboxAddress>();

To.AddRange(to.Select(x => MailboxAddress.Parse(x)));
Subject = subject;
Content = content;
Attachments = attachments;
}
}
1 change: 1 addition & 0 deletions dotnet-client-libraries/SendEmailWithMailKit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Source code for [How to Send Email in ASP.NET Core Web API With Attachments](https://code-maze.com/aspnetcore-send-email/).
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EmailApp", "EmailApp\EmailApp.csproj", "{18F79C88-A3AF-4A1D-BA13-592A82E333A4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EmailService", "EmailService\EmailService.csproj", "{1987546B-DF9F-46DD-83ED-AE7BE1FF06AC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{18F79C88-A3AF-4A1D-BA13-592A82E333A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{18F79C88-A3AF-4A1D-BA13-592A82E333A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{18F79C88-A3AF-4A1D-BA13-592A82E333A4}.Debug|x64.ActiveCfg = Debug|Any CPU
{18F79C88-A3AF-4A1D-BA13-592A82E333A4}.Debug|x64.Build.0 = Debug|Any CPU
{18F79C88-A3AF-4A1D-BA13-592A82E333A4}.Debug|x86.ActiveCfg = Debug|Any CPU
{18F79C88-A3AF-4A1D-BA13-592A82E333A4}.Debug|x86.Build.0 = Debug|Any CPU
{18F79C88-A3AF-4A1D-BA13-592A82E333A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{18F79C88-A3AF-4A1D-BA13-592A82E333A4}.Release|Any CPU.Build.0 = Release|Any CPU
{18F79C88-A3AF-4A1D-BA13-592A82E333A4}.Release|x64.ActiveCfg = Release|Any CPU
{18F79C88-A3AF-4A1D-BA13-592A82E333A4}.Release|x64.Build.0 = Release|Any CPU
{18F79C88-A3AF-4A1D-BA13-592A82E333A4}.Release|x86.ActiveCfg = Release|Any CPU
{18F79C88-A3AF-4A1D-BA13-592A82E333A4}.Release|x86.Build.0 = Release|Any CPU
{1987546B-DF9F-46DD-83ED-AE7BE1FF06AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1987546B-DF9F-46DD-83ED-AE7BE1FF06AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1987546B-DF9F-46DD-83ED-AE7BE1FF06AC}.Debug|x64.ActiveCfg = Debug|Any CPU
{1987546B-DF9F-46DD-83ED-AE7BE1FF06AC}.Debug|x64.Build.0 = Debug|Any CPU
{1987546B-DF9F-46DD-83ED-AE7BE1FF06AC}.Debug|x86.ActiveCfg = Debug|Any CPU
{1987546B-DF9F-46DD-83ED-AE7BE1FF06AC}.Debug|x86.Build.0 = Debug|Any CPU
{1987546B-DF9F-46DD-83ED-AE7BE1FF06AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1987546B-DF9F-46DD-83ED-AE7BE1FF06AC}.Release|Any CPU.Build.0 = Release|Any CPU
{1987546B-DF9F-46DD-83ED-AE7BE1FF06AC}.Release|x64.ActiveCfg = Release|Any CPU
{1987546B-DF9F-46DD-83ED-AE7BE1FF06AC}.Release|x64.Build.0 = Release|Any CPU
{1987546B-DF9F-46DD-83ED-AE7BE1FF06AC}.Release|x86.ActiveCfg = Release|Any CPU
{1987546B-DF9F-46DD-83ED-AE7BE1FF06AC}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Loading