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
1 change: 1 addition & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
- src/Madev.Utils.Infrastructure.Services.Mailing/Madev.Utils.Infrastructure.Services.Mailing.csproj
- src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/Madev.Utils.Infrastructure.Services.Mailing.Mailkit.csproj
- src/Madev.Utils.Infrastructure.Services.Mailing.MsGraph/Madev.Utils.Infrastructure.Services.Mailing.MsGraph.csproj
- src/Madev.Utils.Infrastructure.Services.Mailing.SendGrid/Madev.Utils.Infrastructure.Services.Mailing.SendGrid.csproj
- src/Madev.Utils.Infrastructure.Hangfire/Madev.Utils.Infrastructure.Hangfire.csproj


Expand Down
2 changes: 2 additions & 0 deletions GitVersion.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ branches:
is-release-branch: true
is-main-branch: true
increment: Patch
pull-request:
is-main-branch: false
commit-message-incrementing: Enabled
ignore:
sha: []
61 changes: 0 additions & 61 deletions Madev.Utils.sln

This file was deleted.

10 changes: 10 additions & 0 deletions Madev.Utils.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Solution>
<Project Path="src/Madev.Utils.Infrastructure.ApplicationInsights.AspNetCore/Madev.Utils.Infrastructure.ApplicationInsights.AspNetCore.csproj" />
<Project Path="src/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService/Madev.Utils.Infrastructure.ApplicationInsights.WorkerService.csproj" />
<Project Path="src/Madev.Utils.Infrastructure.Hangfire/Madev.Utils.Infrastructure.Hangfire.csproj" />
<Project Path="src/Madev.Utils.Infrastructure.Http/Madev.Utils.Infrastructure.Http.csproj" />
<Project Path="src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/Madev.Utils.Infrastructure.Services.Mailing.Mailkit.csproj" />
<Project Path="src/Madev.Utils.Infrastructure.Services.Mailing.MsGraph/Madev.Utils.Infrastructure.Services.Mailing.MsGraph.csproj" />
<Project Path="src/Madev.Utils.Infrastructure.Services.Mailing.SendGrid/Madev.Utils.Infrastructure.Services.Mailing.SendGrid.csproj" />
<Project Path="src/Madev.Utils.Infrastructure.Services.Mailing/Madev.Utils.Infrastructure.Services.Mailing.csproj" />
</Solution>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using MailKit;
using MailKit.Net.Smtp;
Expand Down Expand Up @@ -31,106 +32,95 @@
}
}

public async Task SendTextEmailAsync(string toAddress, string subject, string body, IEnumerable<IEmailAttachment> attachments = null)
public async Task SendHtmlEmailAsync(EmailMessage message, CancellationToken cancellationToken = default)
{
attachments ??= new List<IEmailAttachment>();
await SendTextEmailAsync(new List<string>() { toAddress }, subject, body, attachments);
var mimeMessage = ConstructHtmlMimeMessage(message);
await SendMimeMessageAsync(mimeMessage, cancellationToken);
}

public async Task SendTextEmailAsync(IEnumerable<string> toAddress, string subject, string body, IEnumerable<IEmailAttachment> attachments = null)
public async Task SendTextEmailAsync(EmailMessage message, CancellationToken cancellationToken = default)
{
attachments ??= new List<IEmailAttachment>();
var message = ConstructTextMimeMessage(toAddress, subject, body, attachments);
await SendMimeMessageAsync(message);
var mimeMessage = ConstructTextMimeMessage(message);
await SendMimeMessageAsync(mimeMessage, cancellationToken);
}

public async Task SendHtmlEmailAsync(string toAddress, string subject, string body, IEnumerable<IEmailAttachment> attachments = null)
private MimeMessage ConstructHtmlMimeMessage(EmailMessage message)
{
attachments ??= new List<IEmailAttachment>();
await SendHtmlEmailAsync(new List<string>() { toAddress }, subject, body, attachments);
var mimeMessage = new MimeMessage();
mimeMessage = ConstructMessageHeaders(mimeMessage, message);
mimeMessage = ConstructMessageBody(mimeMessage, message.Body, null, message.Attachments);
return mimeMessage;
}

public async Task SendHtmlEmailAsync(IEnumerable<string> toAddress, string subject, string body, IEnumerable<IEmailAttachment> attachments = null)
private MimeMessage ConstructTextMimeMessage(EmailMessage message)
{
attachments ??= new List<IEmailAttachment>();
var message = ConstructHtmlMimeMessage(toAddress, subject, body, attachments);
await SendMimeMessageAsync(message);
var mimeMessage = new MimeMessage();
mimeMessage = ConstructMessageHeaders(mimeMessage, message);
mimeMessage = ConstructMessageBody(mimeMessage, null, message.Body, message.Attachments);
return mimeMessage;
}

private MimeMessage ConstructTextMimeMessage(IEnumerable<string> toAddress,
string subject, string body, IEnumerable<IEmailAttachment> attachments)
private MimeMessage ConstructMessageHeaders(MimeMessage mimeMessage, EmailMessage message)
{
var message = new MimeMessage();
message = ConstructMessageHeaders(message, toAddress, subject);
message = ConstructMessageBody(message, null, body, attachments);
return message;
}

private MimeMessage ConstructHtmlMimeMessage(IEnumerable<string> toAddress,
string subject, string body, IEnumerable<IEmailAttachment> attachments)
{
var message = new MimeMessage();
message = ConstructMessageHeaders(message, toAddress, subject);
message = ConstructMessageBody(message, body, null, attachments);
return message;
}

private MimeMessage ConstructMessageHeaders(MimeMessage message, IEnumerable<string> toAddress, string subject)
{
message.From.Add(MailboxAddress.Parse(_options.Sender ?? _options.Username));
foreach (var address in toAddress)
mimeMessage.From.Add(MailboxAddress.Parse(message.From ?? _options.Sender ?? _options.Username));
foreach (var address in message.To)
{
message.To.Add(MailboxAddress.Parse(address));
mimeMessage.To.Add(MailboxAddress.Parse(address));
}
message.Subject = subject;
return message;
foreach (var address in message.Cc)
{
mimeMessage.Cc.Add(MailboxAddress.Parse(address));
}
foreach (var address in message.Bcc)
{
mimeMessage.Bcc.Add(MailboxAddress.Parse(address));
}
mimeMessage.Subject = message.Subject;
return mimeMessage;
}

private MimeMessage ConstructMessageBody(MimeMessage message, string htmlBody, string textBody, IEnumerable<IEmailAttachment> attachments)
private MimeMessage ConstructMessageBody(MimeMessage message, string? htmlBody, string? textBody, IEnumerable<IEmailAttachment> attachments)
{
var builder = new BodyBuilder();
builder.HtmlBody = htmlBody;
builder.TextBody = textBody;
if (attachments != null)
foreach (var attachment in attachments)
{
foreach (var attachment in attachments)
var convertedAttachment = attachment switch
{
FilepathEmailAttachment att => builder.Attachments.Add(
Path.GetFileName(att.Path),
File.ReadAllBytes(att.Path),
ContentType.Parse(att.ContentType)

Check warning on line 94 in src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/MailkitEmailSender.cs

View workflow job for this annotation

GitHub Actions / build (src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/Madev.Utils.Infrastructure.Service...

Possible null reference argument for parameter 'text' in 'ContentType ContentType.Parse(string text)'.

Check warning on line 94 in src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/MailkitEmailSender.cs

View workflow job for this annotation

GitHub Actions / build (src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/Madev.Utils.Infrastructure.Service...

Possible null reference argument for parameter 'text' in 'ContentType ContentType.Parse(string text)'.

Check warning on line 94 in src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/MailkitEmailSender.cs

View workflow job for this annotation

GitHub Actions / build (src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/Madev.Utils.Infrastructure.Service...

Possible null reference argument for parameter 'text' in 'ContentType ContentType.Parse(string text)'.

Check warning on line 94 in src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/MailkitEmailSender.cs

View workflow job for this annotation

GitHub Actions / build (src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/Madev.Utils.Infrastructure.Service...

Possible null reference argument for parameter 'text' in 'ContentType ContentType.Parse(string text)'.
),
ByteEmailAttachment att => builder.Attachments.Add(
att.Filename,
att.Content,
ContentType.Parse(att.ContentType)

Check warning on line 99 in src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/MailkitEmailSender.cs

View workflow job for this annotation

GitHub Actions / build (src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/Madev.Utils.Infrastructure.Service...

Possible null reference argument for parameter 'text' in 'ContentType ContentType.Parse(string text)'.

Check warning on line 99 in src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/MailkitEmailSender.cs

View workflow job for this annotation

GitHub Actions / build (src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/Madev.Utils.Infrastructure.Service...

Possible null reference argument for parameter 'text' in 'ContentType ContentType.Parse(string text)'.

Check warning on line 99 in src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/MailkitEmailSender.cs

View workflow job for this annotation

GitHub Actions / build (src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/Madev.Utils.Infrastructure.Service...

Possible null reference argument for parameter 'text' in 'ContentType ContentType.Parse(string text)'.
),
Base64EmailAttachment att => builder.Attachments.Add(
att.FileName,
Convert.FromBase64String(att.Content),
ContentType.Parse(att.ContentType)

Check warning on line 104 in src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/MailkitEmailSender.cs

View workflow job for this annotation

GitHub Actions / build (src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/Madev.Utils.Infrastructure.Service...

Possible null reference argument for parameter 'text' in 'ContentType ContentType.Parse(string text)'.

Check warning on line 104 in src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/MailkitEmailSender.cs

View workflow job for this annotation

GitHub Actions / build (src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/Madev.Utils.Infrastructure.Service...

Possible null reference argument for parameter 'text' in 'ContentType ContentType.Parse(string text)'.

Check warning on line 104 in src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/MailkitEmailSender.cs

View workflow job for this annotation

GitHub Actions / build (src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/Madev.Utils.Infrastructure.Service...

Possible null reference argument for parameter 'text' in 'ContentType ContentType.Parse(string text)'.
),
_ => throw new InvalidOperationException("Unknown attachment type.")
};

if (attachment.IsInline)
{
var convertedAttachment = attachment switch
{
FilepathEmailAttachment att => builder.Attachments.Add(
Path.GetFileName(att.Path),
File.ReadAllBytes(att.Path),
ContentType.Parse(att.ContentType)
),
ByteEmailAttachment att => builder.Attachments.Add(
att.Filename,
att.Content,
ContentType.Parse(att.ContentType)
),
Base64EmailAttachment att => builder.Attachments.Add(
att.FileName,
Convert.FromBase64String(att.Content),
ContentType.Parse(att.ContentType)
),
_ => throw new InvalidOperationException("Unknown attachment type.")
};

if (attachment.IsInline)
{
convertedAttachment.ContentId = attachment.ContentId;
convertedAttachment.ContentDisposition = new ContentDisposition(ContentDisposition.Inline);
}
convertedAttachment.ContentId = attachment.ContentId;
convertedAttachment.ContentDisposition = new ContentDisposition(ContentDisposition.Inline);
}
}
message.Body = builder.ToMessageBody();
return message;
}

private async Task SendMimeMessageAsync(MimeMessage message)
private async Task SendMimeMessageAsync(MimeMessage message, CancellationToken cancellationToken)
{
try
{
await _smtpClient.SendAsync(message);
await _smtpClient.SendAsync(message, cancellationToken);
return;
}
catch (ServiceNotConnectedException)
Expand All @@ -145,7 +135,7 @@
Connect();
}
}
_smtpClient.Send(message);
_smtpClient.Send(message, cancellationToken);
}

private void Connect()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,45 @@ public EmailBuilder To(IEnumerable<string> recipients)
return this;
}

public EmailBuilder Cc(IEnumerable<string> recipients)
{
_email.CcRecipients = recipients.Select(x => new Recipient
{
EmailAddress = new EmailAddress
{
Address = x
}
}).ToList();
return this;
}

public EmailBuilder Bcc(IEnumerable<string> recipients)
{
_email.BccRecipients = recipients.Select(x => new Recipient
{
EmailAddress = new EmailAddress
{
Address = x
}
}).ToList();
return this;
}

public EmailBuilder From(string? from)
{
if (from != null)
{
_email.From = new Recipient
{
EmailAddress = new EmailAddress
{
Address = from
}
};
}
return this;
}

public EmailBuilder Subject(string subject)
{
_email.Subject = subject;
Expand Down
Loading
Loading