'Including Pictures in an Outlook Email

I'm trying to use a Microsoft Word document as the body to an Microsoft Outlook email. So far I have been able to include the text from a Word .docx into the body of the email with the code:

            if (File.Exists(fileName.ToString()))
        {
            DateTime today = DateTime.Now;

            object readOnly = false;
            object isVisible = false;

            //Set Word to invisible
            wordApp.Visible = false;

            //Open the word document
            aDoc = wordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);

            try
            {
                //Activate Document
                aDoc.Activate();

                //Find Place Holders and replace them with values
                this.FindAndReplace(wordApp, "<NameAddressed>", NameAddressed);
                this.FindAndReplace(wordApp, "<SessionInfo>", SessionInfo);
                this.FindAndReplace(wordApp, "<NumberGuests>", GuestNumber);
                this.FindAndReplace(wordApp, "<Balance>", Balance);

                //Postal
                this.FindAndReplace(wordApp, "<FullName>", FullName);
                this.FindAndReplace(wordApp, "<Address1>", Address1);
                if (Address2 != "&nbsp" && Address2 != "" && Address2 != " ")
                    this.FindAndReplace(wordApp, "<Address1>", Address1 + "\n\r" + Address2);
                else
                    this.FindAndReplace(wordApp, "<Address1>", Address1);
                this.FindAndReplace(wordApp, "<City>", City);
                this.FindAndReplace(wordApp, "<State>", State);
                this.FindAndReplace(wordApp, "<Zip>", Zip);
            }
            catch (Exception ex)
            {
                aDoc.Close(ref missing, ref missing, ref missing);
                ClientScript.RegisterStartupScript(this.GetType(), "error", "javascript:;alert('" + ex.Message + "')");
                return false;
            }
            aDoc.SaveAs(ref saveAs);

            //Save the file as the correct file name

            if (DataType.Text == "Email")
            {
                Outlook.Application oApp = new Outlook.Application();
                // Create a new mail item.
                Outlook.MailItem eMail = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

                Word.Range r = aDoc.Content;
                r.Select();
                string s = r.Text;

                eMail.Subject = "Confirmation Email";
                eMail.To = "[email protected]";
                eMail.Body = s;
                ((Outlook._MailItem)eMail).Send();
                //Close the document - you have to do this
                aDoc.Close(ref missing, ref missing, ref missing);
            }
            litError.Text = "File Created. ";
            return true;
        }
        else
        {
            litError.Visible = true;
            litError.Text = "File Does Not Exist";
            return false;
        }

But this code will not include the images that are also in the Word doc in the email. Is there any way that the .docx can also send its images to Outlook and keep its original format? Thanks in advance



Solution 1:[1]

First of all, you are setting the plain text MailItem.Body property. Secondly, create an attachment and set the PR_ATTACH_CONTENT_ID property (DASL name "http://schemas.microsoft.com/mapi/proptag/0x3712001F") using Attachment.PropertyAccessor.SetProperty.

Your HTML body (MailItem.HTMLBody property) would then need to reference that image attachment through the cid attribute:

<img src="cid:xyz">

where xyz is the value of the PR_ATTACH_CONTENT_ID property.

Look at an existing message with OutlookSpy (I am its author - click IMessage button).

EDIT: sample script (off the top of my head):

attachment = MailItem.Attachments.Add("c:\temp\MyPicture.jpg")
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyId1")
MailItem.HTMLBody = "<html><body>Test image <img src=""cid:MyId1""></body></html>"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1