How to save MailMessage object to disk as *.eml or *.msg file

Viewed 97305

How do I save MailMessage object to the disk? The MailMessage object does not expose any Save() methods.

I dont have a problem if it saves in any format, *.eml or *.msg. Any idea how to do this?

6 Answers

For simplicity, I'll just quote an explanation from a Connect item:

You can actually configure the SmtpClient to send emails to the file system instead of the network. You can do this programmatically using the following code:

SmtpClient client = new SmtpClient("mysmtphost");
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = @"C:\somedirectory";
client.Send(message);

You can also set this up in your application configuration file like this:

 <configuration>
     <system.net>
         <mailSettings>
             <smtp deliveryMethod="SpecifiedPickupDirectory">
                 <specifiedPickupDirectory pickupDirectoryLocation="C:\somedirectory" />
             </smtp>
         </mailSettings>
     </system.net>
 </configuration>

After sending the email, you should see email files get added to the directory you specified. You can then have a separate process send out the email messages in batch mode.

You should be able to use the empty constructor instead of the one listed, as it won't be sending it anyway.

If you are using Mailkit. Just write below code

string fileName = "your filename full path";
MimeKit.MimeMessage message = CreateMyMessage ();
message.WriteTo(fileName);

With the help of community I came up with an solution for .NET 5. I have combined this old solution with suggestions in this post and got inspired by Mailkit which resulted in nice extension method without unnecessary dependencies

public static class MailMessageHelper
{
    public static void WriteTo(this MailMessage mail, Stream stream)
    {
        Assembly assembly = typeof(SmtpClient).Assembly;
        Type _mailWriterType = assembly.GetType("System.Net.Mail.MailWriter");

        // Get reflection info for MailWriter contructor
        ConstructorInfo _mailWriterConstructor =
            _mailWriterType.GetConstructor(
                BindingFlags.Instance | BindingFlags.NonPublic,
                null,
                new Type[] { typeof(Stream), typeof(bool) },
                null);

        // Construct MailWriter object with our FileStream
        object _mailWriter =
          _mailWriterConstructor.Invoke(new object[] { stream, true });

        // Get reflection info for Send() method on MailMessage
        MethodInfo _sendMethod =
            typeof(MailMessage).GetMethod(
                "Send",
                BindingFlags.Instance | BindingFlags.NonPublic);

        // Call method passing in MailWriter
        _sendMethod.Invoke(
            mail,
            BindingFlags.Instance | BindingFlags.NonPublic,
            null,
            new object[] { _mailWriter, true, true },
            null);

        // Finally get reflection info for Close() method on our MailWriter
        MethodInfo _closeMethod =
            _mailWriter.GetType().GetMethod(
                "Close",
                BindingFlags.Instance | BindingFlags.NonPublic);

        // Call close method
        _closeMethod.Invoke(
            _mailWriter,
            BindingFlags.Instance | BindingFlags.NonPublic,
            null,
            Array.Empty<object>(),
            null);
    }
}

Usage

MailMessage mail = new(mailFrom, mailTo, mailSubject, mailContent);
mail.WriteTo(new FileStream(@"path_to_file\new_mail.eml", FileMode.Create));

Also if you are using MemoryStream and want to get result in string, just change the return type of the extension method and at the end write

return Encoding.ASCII.GetString(stream.ToArray());

Enjoy

try this

please use these 2 reference ( using MailBee;) ( using MailBee.Mime;)

    public static string load(string to,string from,string cc,string bcc,string subject,string body, List<string> reportList,string path, bool HtmlbodyType)
    {
        try
        {
            MailBee.Mime.MailMessage msg = new MailBee.Mime.MailMessage();
            msg.From.AsString = from;
            msg.Subject = subject;
            if (HtmlbodyType == true)
            {
                msg.BodyHtmlText = body;
            }
            else
            {
                msg.BodyPlainText = body;
            }
            
            string[] receptionEmail = to.Split(new string[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries);
            string[] ccEmail = cc.Split(new string[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries);
            string[] bccEmail = bcc.Split(new string[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries);
            string message = "";
            foreach (string To in receptionEmail)
            {
                msg.To.Add(To);
            }
            foreach (string CC in ccEmail)
            {
                    msg.Cc.Add(CC);
            }
            foreach (string Bcc in bccEmail)
            {
                    msg.Bcc.Add(Bcc);

            }
                for (int x = 0; x < reportList.Count; x++)
                {
                    string fileName = reportList[x];
                    msg.Attachments.Add(fileName);
                }

                msg.SaveMessage(path);
                return "Success";
            
        }
        catch (Exception ex)
        {
            return ex.Message;
        }

    }
Related