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
Expand Up @@ -32,26 +32,26 @@
}
}

public async Task SendTextEmailAsync(string toAddress, string subject, string body, IEnumerable<IEmailAttachment> attachments = null)

Check warning on line 35 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...

Cannot convert null literal to non-nullable reference type.

Check warning on line 35 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...

Nullability of reference types in type of parameter 'attachments' of 'Task MailkitEmailSender.SendTextEmailAsync(string toAddress, string subject, string body, IEnumerable<IEmailAttachment> attachments = null)' doesn't match implicitly implemented member 'Task IEmailSender.SendTextEmailAsync(string toAddress, string subject, string body, IEnumerable<IEmailAttachment>? attachments = null)' (possibly because of nullability attributes).
{
attachments ??= new List<IEmailAttachment>();
await SendTextEmailAsync(new List<string>() { toAddress }, subject, body, attachments);
}

public async Task SendTextEmailAsync(IEnumerable<string> toAddress, string subject, string body, IEnumerable<IEmailAttachment> attachments = null)

Check warning on line 41 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...

Cannot convert null literal to non-nullable reference type.

Check warning on line 41 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...

Nullability of reference types in type of parameter 'attachments' of 'Task MailkitEmailSender.SendTextEmailAsync(IEnumerable<string> toAddress, string subject, string body, IEnumerable<IEmailAttachment> attachments = null)' doesn't match implicitly implemented member 'Task IEmailSender.SendTextEmailAsync(IEnumerable<string> toAddress, string subject, string body, IEnumerable<IEmailAttachment>? attachments = null)' (possibly because of nullability attributes).
{
attachments ??= new List<IEmailAttachment>();
var message = ConstructTextMimeMessage(toAddress, subject, body, attachments);
await SendMimeMessageAsync(message);
}

public async Task SendHtmlEmailAsync(string toAddress, string subject, string body, IEnumerable<IEmailAttachment> attachments = null)

Check warning on line 48 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...

Cannot convert null literal to non-nullable reference type.

Check warning on line 48 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...

Nullability of reference types in type of parameter 'attachments' of 'Task MailkitEmailSender.SendHtmlEmailAsync(string toAddress, string subject, string body, IEnumerable<IEmailAttachment> attachments = null)' doesn't match implicitly implemented member 'Task IEmailSender.SendHtmlEmailAsync(string toAddress, string subject, string body, IEnumerable<IEmailAttachment>? attachments = null)' (possibly because of nullability attributes).
{
attachments ??= new List<IEmailAttachment>();
await SendHtmlEmailAsync(new List<string>() { toAddress }, subject, body, attachments);
}

public async Task SendHtmlEmailAsync(IEnumerable<string> toAddress, string subject, string body, IEnumerable<IEmailAttachment> attachments = null)

Check warning on line 54 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...

Cannot convert null literal to non-nullable reference type.

Check warning on line 54 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...

Nullability of reference types in type of parameter 'attachments' of 'Task MailkitEmailSender.SendHtmlEmailAsync(IEnumerable<string> toAddress, string subject, string body, IEnumerable<IEmailAttachment> attachments = null)' doesn't match implicitly implemented member 'Task IEmailSender.SendHtmlEmailAsync(IEnumerable<string> toAddress, string subject, string body, IEnumerable<IEmailAttachment>? attachments = null)' (possibly because of nullability attributes).
{
attachments ??= new List<IEmailAttachment>();
var message = ConstructHtmlMimeMessage(toAddress, subject, body, attachments);
Expand All @@ -63,7 +63,7 @@
{
var message = new MimeMessage();
message = ConstructMessageHeaders(message, toAddress, subject);
message = ConstructMessageBody(message, null, body, attachments);

Check warning on line 66 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...

Cannot convert null literal to non-nullable reference type.
return message;
}

Expand All @@ -72,7 +72,7 @@
{
var message = new MimeMessage();
message = ConstructMessageHeaders(message, toAddress, subject);
message = ConstructMessageBody(message, body, null, attachments);

Check warning on line 75 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...

Cannot convert null literal to non-nullable reference type.
return message;
}

Expand All @@ -96,18 +96,30 @@
{
foreach (var attachment in attachments)
{
switch (attachment)
var convertedAttachment = attachment switch
{
case FilepathEmailAttachment att:
var filename = Path.GetFileName(att.Path);
var content = File.ReadAllBytes(att.Path);
builder.Attachments.Add(filename, content);
break;
case ByteEmailAttachment att:
builder.Attachments.Add(att.Filename, att.Content);
break;
default:
throw new InvalidOperationException("Unknown attachment type.");
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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,36 @@ public EmailBuilder Attachments(IEnumerable<IEmailAttachment> attachments)

private FileAttachment ConvertToFileAttachment(IEmailAttachment attachment)
{
return attachment switch
var convertedAttachment = attachment switch
{
ByteEmailAttachment att => new FileAttachment
{
Name = att.Filename, ContentBytes = att.Content, ContentType = "application/octet-stream"
Name = att.Filename,
ContentBytes = att.Content,
ContentType = attachment.ContentType
},
FilepathEmailAttachment att => new FileAttachment
{
Name = Path.GetFileName(att.Path),
ContentBytes = File.ReadAllBytes(att.Path),
ContentType = "application/octet-stream"
ContentType = attachment.ContentType
},
Base64EmailAttachment att => new FileAttachment
{
Name = att.FileName,
ContentBytes = Convert.FromBase64String(att.Content),
ContentType = attachment.ContentType
},
_ => throw new InvalidOperationException("Unknown attachment type.")
};

if (attachment.IsInline)
{
convertedAttachment.IsInline = true;
convertedAttachment.ContentId = attachment.ContentId;
}

return convertedAttachment;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;

namespace Madev.Utils.Infrastructure.Services.Mailing
{
public class Base64EmailAttachment : IEmailAttachment
{
public Base64EmailAttachment(string filename, string content, bool isInline = false, string? contentId = null, string? contentType = null)
{
FileName = filename;
Content = content;
IsInline = isInline;

if (IsInline && contentId == null)
{
throw new ArgumentException("If an attachment is inline, ContentId must be defined");
}
ContentId = contentId;

if(contentType != null)
{
ContentType = contentType;
}
}

public string FileName { get; }
public string Content { get; }
public string? ContentId { get; set; }
public bool IsInline { get; set; }
public string? ContentType { get; set; } = "application/octet-stream";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,28 @@ namespace Madev.Utils.Infrastructure.Services.Mailing
{
public class ByteEmailAttachment : IEmailAttachment
{
public ByteEmailAttachment(string filename, byte[] content)
public ByteEmailAttachment(string filename, byte[] content, bool isInline = false, string? contentId = null, string? contentType = null)
{
Filename = filename ?? throw new ArgumentException($"{nameof(filename)} cannot be null.");
Content = content ?? throw new ArgumentException($"{nameof(content)} cannot be empty.");
IsInline = isInline;

if(IsInline && contentId == null)
{
throw new ArgumentException("If an attachment is inline, ContentId must be defined");
}
ContentId = contentId;

if(contentType != null)
{
ContentType = contentType;
}
}

public string Filename { get; }
public byte[] Content { get; }
public string? ContentId { get; set; }
public bool IsInline { get; set;}
public string? ContentType { get; set; } = "application/octet-stream";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,26 @@ namespace Madev.Utils.Infrastructure.Services.Mailing
{
public class FilepathEmailAttachment : IEmailAttachment
{
public FilepathEmailAttachment(string path)
public FilepathEmailAttachment(string path, bool isInline = false, string? contentId = null, string? contentType = null)
{
Path = path ?? throw new ArgumentException($"{nameof(path)} cannot be empty.");
IsInline = isInline;

if (IsInline && contentId == null)
{
throw new ArgumentException("If an attachment is inline, ContentId must be defined");
}
ContentId = contentId;

if(contentType != null)
{
ContentType = contentType;
}
}

public string Path { get; }
public string? ContentId { get; set; }
public bool IsInline { get ; set ; }
public string? ContentType { get ; set ; } = "application/octet-stream";
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
namespace Madev.Utils.Infrastructure.Services.Mailing
{
public interface IEmailAttachment{}
public interface IEmailAttachment{
public bool IsInline { get; set; }
public string? ContentId { get; set; }
public string? ContentType { get; set; }
}
}
Loading