在Outlook电子邮件中包含图片

本文关键字:包含图 电子邮件 Outlook | 更新日期: 2023-09-27 18:19:07

我正在尝试使用Microsoft Word文档作为Microsoft Outlook电子邮件的正文。到目前为止,我已经能够将Word .docx中的文本包含到电子邮件的正文中,代码为:

            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 = "example@xyz.com";
                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;
        }

但是此代码将不包括同样在电子邮件中的Word文档中的图像。是否有任何方法可以将。docx图像也发送到Outlook并保持其原始格式?提前感谢

在Outlook电子邮件中包含图片

首先,您要设置纯文本MailItem.Body属性。其次,使用Attachment.PropertyAccessor.SetProperty创建一个附件并设置PR_ATTACH_CONTENT_ID属性(DASL名称"http://schemas.microsoft.com/mapi/proptag/0x3712001F")。

你的HTML主体(MailItem.HTMLBody属性)需要通过cid属性引用该图像附件:

<img src="cid:xyz">

其中xyz为PR_ATTACH_CONTENT_ID属性的值。

查看现有的消息与OutlookSpy(我是它的作者-点击IMessage按钮)。

编辑:示例脚本(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>"