Create an EML file the easy way

Print Content | More

Many email readers (like Windows Mail, Outlook, Thunderbird...) use this format to store emails in files; I needed an easy way to create those files to send emails using the default email program the client has configured on his system.

Looking the the framework class documentation I’ve found that the SmtpClient class, specifying its DeliveryMethod to SmtpDeliveryMethod.SpecifiedPickupDirectory and PickupDirectoryLocation to the destination folder you want the files to be placed, can generate those files for you, without the need of any external library.

The code was quite straightforward:

/// <summary>
/// Creates an eml file to be delivered with the default mail client
/// </summary>
/// <param name="from">address that sends the email</param>
/// <param name="to">address to which send the email</param>
/// <param name="subject">subject</param>
/// <param name="body">body</param>
/// <param name="attachments">list of attachments</param>
/// <param name="destFolder">destination folder to store the file</param>
/// <returns>The complete path of the newely generated EML file</returns>
public static string CreateEml(string from,
                               string to,
                               string subject,
                               string body,
                               IEnumerable<Attachment> attachments,
                               string destFolder)
   using (MailMessage mail = new MailMessage())
      mail.From = new MailAddress(from);
      mail.Subject = subject;
      mail.Body = body;
      if (attachments != null)
         foreach (var attachment in attachments)
      SmtpClient smtpClient = new SmtpClient
                                    PickupDirectoryLocation = destFolder,
                                    DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory
   DirectoryInfo dir = new DirectoryInfo(destFolder);
   FileInfo file = dir.GetFiles("*.eml", SearchOption.TopDirectoryOnly).OrderByDescending(m => m.CreationTimeUtc).First();
   return file.FullName;

The function returns the full path of the newly created eml file, you can use it in a System.Diagnostics.Process.Start() call to open the default email client of the user.

Create, Eml, Programmatically


  1. #1 da Stefano - Saturday March 2010 alle 01:15

    Good tip, Ale! Do you know if there is also a method in the framework to create a MailMessage from an existing eml?

  2. #2 da Guardian - Saturday March 2010 alle 01:15

    I don't think so, I couldn't find anything on the MSDN documentation and I ended up manually parsing the eml file with some regular expressions to get the fields I needed.

  3. #3 da scorrexx - Saturday March 2010 alle 01:15

    OrderByDescending(m => m.CreationTimeUtc).First(); What`s "m" ?

  4. #4 da camping - Saturday March 2010 alle 01:15

    so, eml is created and what's next? how to send it out?

All fields are required and you must provide valid data in order to be able to comment on this post.

(will not be published)