+1 vote

We're using attachments for files that are already in memory or streamed and not on disk. Those files do have a file name. We're using 'IFluentMail.AddAttachment' with the byte array parameter. Afterwards, we set the filename with 'SetFileName'.

It would be nice to be able to set an (optional?) parameter when using SetFileName, so that the ContentType is also set automatically or updated, just like happens when you're using IFluentMail.AddAttachment with a file path.

Now we have to use a custom way to determine the content type and set it manually.

by (450 points)

1 Answer

0 votes

SetFileName method is a fluent equivalent of MimeData.FileName property, the idea is that there are no side effects in using this method.

We'll add the overload on IFluentAttachment (SetFileName(string fileName, bool guessContentType)) and similar method to MimeData class.

Fluent API:

byte[] data = File.ReadAllBytes("c:\\tmp.zip");

IMail email = Fluent.Mail
    .Text("email body")
    .AddAttachment(data).SetFileName("report.zip", true)
    .Create();

Assert.AreEqual("author.zip", email.NonVisuals[0].FileName);
Assert.AreEqual(ContentType.ApplicationZip, email.NonVisuals[0].ContentType);

MailBuilder API:

byte[] data = File.ReadAllBytes(TestConstants.FileInTest("me.zip"));

MailBuilder builder = new MailBuilder();
builder.Text = "email body";
MimeData mime = builder.AddAttachment(data);
mime.SetFileName("author.zip", true);
IMail email = builder.Create();

Assert.AreEqual("author.zip", email.NonVisuals[0].FileName);
Assert.AreEqual(ContentType.ApplicationZip, email.NonVisuals[0].ContentType);
by (297k points)
...