3

I'm working with C# on Windows servers for a web application stored on the IIS Server.

I would like to create an eml file from :

  • an html content (string)
  • some attachments that are loaded in memory
  • a string subject
  • string recipients
  • string sender

The main problem is that I am not allowed to store files on the host server (not even in a temporary directory or if I delete them after).

I saw many threads explaining how to create an eml file with the help of SmtpClient. But we always need to use a directory to save the file.

Do someone knows a way to do that ? Or to create a directory in memory (which seems undoable) ?

Thanks for everyone who will read me

[EDIT] Using jstedfast's answer below and the Mime documentation, I could figure a way. Here is a POC in case someone needs it later.

        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Joey", "joey@friends.com"));
        message.To.Add(new MailboxAddress("Alice", "alice@wonderland.com"));
        message.Subject = "How you doin?";

        var builder = new BodyBuilder();

        // Set the plain-text version of the message text
        builder.TextBody = @"Hey Alice,

What are you up to this weekend? Monica is throwing one of her parties on
Saturday and I was hoping you could make it.

Will you be my +1?

-- Joey
";

        // We may also want to attach a calendar event for Monica's party...
        builder.Attachments.Add("test.pdf", attachmentByteArray);

        // Now we just need to set the message body and we're done
        message.Body = builder.ToMessageBody();

        using (var memory = new MemoryStream())
        {
            message.WriteTo(memory);
        }
Kirjava
  • 226
  • 1
  • 11

1 Answers1

3

Look into using MimeKit.

You can write the MimeMessage objects to any type of stream that you want, including a MemoryStream.

jstedfast
  • 35,744
  • 5
  • 97
  • 110