How can I send a Word 2007 document using a SMTP RRS feed

  • Question

  • I have created a form using Word 2007.  I am going to add a button to the ribbon to allow the completed form to be emailed out in the body of the message (not as an attachment).  Unfortunately my company does not use Outlook, so I’m stuck sending this through my SMTP server.  Now I have been successful emailing from C# applications, but I was generating html code and then inserting that as a string with AlternativeView.

    Thank you for any help you can give me.

    Here is the code I’m using to send out the email:

                Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument as Word.Document;
                string server = "smtp server name";
                SmtpClient client = new SmtpClient(server);
                MailAddress from = new MailAddress("", "VSTO Reporting");
                MailMessage message = new MailMessage();
                message.From = from;
                message.To.Add(new MailAddress(""));
                message.Subject = "Report from Word";
                AlternateView avHtml = AlternateView.CreateAlternateViewFromString(doc.ToString(), null, MediaTypeNames.Text.Html);

    The email I receive only has :


    Monday, November 21, 2011 10:08 PM


  • Hi DChiShaggy,

    Thanks for your post.

    To send the content of a document also without losing format, we need to convert word document to html document. And then export the html document to html string which we need to pass to body property of  message object. In addition, we need to specify the IsBodyHtml property to true to make the code work. Check the code below:

           private void button1_Click(object sender, RibbonControlEventArgs e)
                Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
                //check if there is a document open within the currenct application
                if (doc == null)
                //create a new document which need to be saved as html document lateer
                Word.Document docHTML = Globals.ThisAddIn.Application.Documents.Add();
                Word.Range rngHTML = docHTML.Content;          
                string fullName = doc.Path + @"\Temp.htm";
                docHTML.SaveAs2(fullName, Word.WdSaveFormat.wdFormatHTML);
                docHTML = null;
                //configure the message and client objects
                string server = "";
                SmtpClient client = new SmtpClient(server,587);
                string fromAddress = "";
                MailAddress from = new MailAddress(fromAddress);
                MailMessage message = new MailMessage();
                message.From = from;
                message.To.Add(new MailAddress(""));
                message.Subject = "Report from Word";
                //specify this property to true
                message.IsBodyHtml = true;
                message.Body = RetrieveStream(fullName);
                client.EnableSsl = true;
                client.Credentials = new System.Net.NetworkCredential(fromAddress, "password of gmail account");
                //delete the temp file after all the procedures has been done
            //the method to read html document
            public string RetrieveStream(string FullName)
                string stream = null;
                using (StreamReader sr = new StreamReader(FullName))
                    string line =null;
                    while ((line = sr.ReadLine()) != null)
                        stream += line;
                return stream;

    I hope this helps. 

    Calvin Gao[MSFT]
    MSDN Community Support | Feedback to us
    Wednesday, November 23, 2011 7:40 AM