【问题标题】:Including Pictures in an Outlook Email在 Outlook 电子邮件中包含图片
【发布时间】:2022-04-24 02:37:04
【问题描述】:

我正在尝试使用 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 并保持其原始格式? 提前致谢

【问题讨论】:

    标签: c# .net ms-word outlook


    【解决方案1】:

    首先,您要设置纯文本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 按钮)。

    编辑:示例脚本(我想不到):

    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>"
    

    【讨论】:

    • 您的网页正在执行某些操作,“OutlookSpy 将帮助您准确找到您正在寻找的内容!!!”然后有大量的空白空间然后将页面上的其余信息压碎在底部的一条条中
    • 你用的是什么浏览器?这不是一个链接——它是属性 DASL 名称;您需要将其传递给 GetProperty。
    • 我对您告诉我如何处理 Attachment.PropertyAccessor 感到有些困惑。你能给我一个示例代码来说明它是如何工作的吗?
    猜你喜欢
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 2020-02-08
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    相关资源
    最近更新 更多