none
Microsoft.Exchange.Data.Transport.Email.EmailMessage RRS feed

  • Question

  • Hello,

    I am using the following code to create a new Email Message.  This creates a blank Mime Document that looks just like this:

    MIME-Version: 1.0
    Content-Type: text/plain; charset="iso-8859-1"

    I just want to add a plain text message body to this Mime Document that is sourced from a String.  Can someone point me in the right direction.  I would like to use the built in classes in the Microsoft.Exchange APIs.

    Thanks

    using Microsoft.Exchange.Data.Mime;
    using Microsoft.Exchange.Data.Transport.Email;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    
    
    namespace Sandbox
    {
        class Sandbox
        {
            static void Main(string[] args)
            {
    
                EmailMessage MyMessage = EmailMessage.Create();
    
                //=====
                // How to add body part to the message.
    
    
    
    
                String MessageFile = "message.txt";
                FileStream MessageFile = new FileStream(MessageFile, FileMode.Create);
                MyMessage.MimeDocument.WriteTo(MessageFile);
                ForwardedMessageFile.Close();
    
    
    
            }
        }
    }

    Friday, May 6, 2016 5:57 PM

Answers

  • The Body is just a Stream so you just write to it eg

                String MessageBody = "One\r\nTwo\r\n";
                EmailMessage Message = EmailMessage.Create(BodyFormat.Text);
                using (var stream = Message.Body.GetContentWriteStream())
                {
                    using (StreamWriter sw = new StreamWriter(stream))
                    {
                        sw.Write(MessageBody);
                    }
                }
                Message.To.Add(new EmailRecipient("Blah","Blah@blah.com"));
                Stream fsFileStream = new FileStream(@"C:\temp\test2.eml", FileMode.OpenOrCreate);
                Message.MimeDocument.WriteTo(fsFileStream);
                fsFileStream.Close();

    EmailMessage also has an overload that allows you to load and entire message from the RFC (EML) stream

    Cheers
    Glen

    • Marked as answer by williamtholmes Wednesday, September 21, 2016 12:05 PM
    Monday, May 9, 2016 7:13 AM