jstedfast/MimeKit

How to load a MimeMessage from an IFormFile ?

ersnverl opened this issue · 2 comments

Hi,

I'm trying to build an REST endpoint which would allow clients to send an email by giving an .eml file.
The request is a multipart/form-data in which I get the .eml file from an IFormFile.
https://learn.microsoft.com/fr-fr/dotnet/api/microsoft.aspnetcore.http.iformfile?view=aspnetcore-7.0

I made a first Mimekit try from a FileStream which works perfectly.

Here is my code but I never get the Console.WriteLine since Load doesn't end.
I've read some talks here about BoundStream or MemoryBlockStream but I'm not good enough at streams to understand what it implies.

What would you advise me ?

if (request != null && request.Files!= null && request.Files.Length == 1)
{
IFormFile file = request.Files[0];
if (file != null)
{
using (MemoryStream ms = new MemoryStream())
{
file.CopyTo(ms);
MimeMessage msg = MimeMessage.Load(ms);
Console.WriteLine("message from " + msg.From);
}
}
}

After file.CopyTo(ms), add ms.Position = 0; to reset the memory stream back to the beginning.

Thank you. Works perfect.
I told you: streams are not my speciality 👍