FluentEmail icon indicating copy to clipboard operation
FluentEmail copied to clipboard

FluentEmail.AttachFromFileName({fileName}, "content-type", "attachmentName") has "Content-Type" as optional but is ideally expected. Throws error

Open aravindk777 opened this issue 1 year ago • 1 comments

Currently, the method AttachFromFileName({fileName}, {Content-Type}, {attachmentName}) has only fileName as required and other two including "content-type" as optional. But in the implementation, it seems its expected/required. When i implemented the attachment option with just the fileName parameter only, it throws error

Value cannot be null. (Parameter name 'text')

When i dig deeper into the source code of this, I found like its been expected to parse further down, as ContentType.Parse(text).

Either, make the "content-type" parameter as required or dont expect it further down the line, may be, derive the content type implicitly or auto detect, whichever could solve not to throw such error.

Stack trace details:

at MimeKit.Utils.ParseUtils.ValidateArguments(ParserOptions options, String text) at MimeKit.ContentType.Parse(ParserOptions options, String text) at MimeKit.ContentType.Parse(String text) at FluentEmail.MailKitSmtp.MailKitSender.<>c__DisplayClass5_0.<CreateMailMessage>b__0(Attachment x) at FluentEmail.Core.ListExtensions.ForEach[T](IEnumerable1 enumerable, Action1 consumer) at FluentEmail.MailKitSmtp.MailKitSender.CreateMailMessage(IFluentEmail email) at FluentEmail.MailKitSmtp.MailKitSender.<SendAsync>d__3.MoveNext()

My code usage/implementation is,

if (File.Exists(file))
{                  
    mailer.AttachFromFilename(file, attachmentName: Path.GetFileName(file));
}

aravindk777 avatar Feb 29 '24 05:02 aravindk777

I think it's actually MimeKit which expects the content type, not this library. The classes in this library are designed to be generic and to work with multiple senders; for example, SmtpSender, which uses the built-in SmtpClient, does not need a content type.

asherber avatar Mar 30 '24 13:03 asherber