C# 在Outlook电子邮件中包含图片

C# 在Outlook电子邮件中包含图片,c#,.net,ms-word,outlook,C#,.net,Ms Word,Outlook,我正在尝试使用Microsoft Word文档作为Microsoft Outlook电子邮件的正文。到目前为止,我已经能够将Word.docx中的文本包含在电子邮件正文中,代码如下: if (File.Exists(fileName.ToString())) { DateTime today = DateTime.Now; object readOnly = false; objec

我正在尝试使用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;
        }
if(File.Exists(fileName.ToString()))
{
DateTime today=DateTime.Now;
对象只读=false;
对象isVisible=false;
//将单词设置为不可见
可见=false;
//打开word文档
aDoc=wordApp.Documents.Open(引用文件名、引用缺失、引用只读、引用缺失、,
参考缺失,参考缺失,参考缺失,参考缺失,参考缺失,参考缺失,参考缺失,
参考缺失,参考可见,参考缺失,参考缺失,参考缺失,参考缺失,参考缺失);
尝试
{
//激活文档
aDoc.Activate();
//找到占位符并用值替换它们
this.FindAndReplace(wordApp,“,NameAddressed);
this.FindAndReplace(wordApp,“,SessionInfo);
this.FindAndReplace(wordApp,“,GuestNumber);
this.findandplace(wordApp,“,Balance);
//邮政的
this.findandplace(wordApp,“,全名);
this.FindAndReplace(wordApp,“,Address1);
如果(地址2!=”和地址2!=“”和地址2!=“”)
this.FindAndReplace(wordApp,“,Address1+”\n\r“+Address2);
其他的
this.FindAndReplace(wordApp,“,Address1);
this.findandplace(wordApp,“,City);
this.findandplace(wordApp,“,State);
this.findandplace(wordApp,“,Zip);
}
捕获(例外情况除外)
{
aDoc.关闭(参考缺失、参考缺失、参考缺失);
RegisterStartupScript(this.GetType(),“error”,“javascript:;alert(““+ex.Message+”)”);
返回false;
}
aDoc.SaveAs(参考SaveAs);
//将文件另存为正确的文件名
如果(DataType.Text==“电子邮件”)
{
Outlook.Application oApp=新建Outlook.Application();
//创建一个新的邮件项目。
Outlook.MailItem eMail=(Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
单词范围r=aDoc.Content;
r、 选择();
字符串s=r.Text;
eMail.Subject=“确认电子邮件”;
eMail.To=”example@xyz.com";
eMail.Body=s;
((Outlook._MailItem)电子邮件).Send();
//关闭文档-您必须这样做
aDoc.关闭(参考缺失、参考缺失、参考缺失);
}
Text=“文件已创建。”;
返回true;
}
其他的
{
litError.Visible=true;
litError.Text=“文件不存在”;
返回false;
}
但此代码将不包括电子邮件中Word文档中的图像。是否有任何方法可以使.docx也可以将其图像发送到Outlook并保持其原始格式?
提前感谢

首先,您正在设置纯文本
MailItem.Body
属性。 其次,创建一个附件并设置
PR\u ATTACH\u CONTENT\u ID
属性(DASL name
)http://schemas.microsoft.com/mapi/proptag/0x3712001F“
)使用
附件.PropertyAccessor.SetProperty

然后,您的HTML正文(
MailItem.HTMLBody
property)需要通过cid属性引用该图像附件:

<img src="cid:xyz">

你的网页正在做一些事情,比如“OutlookSpy将帮助你准确地找到你要找的!!!”然后有大量的空白,然后将页面上的其余信息压碎在底部的一条细条中。你的链接也不起作用。你用的是什么浏览器?这不是一个链接-它是属性DASL名称;您需要将它传递给GetProperty。您告诉我如何处理Attachment.PropertyAccessor,我有点困惑。你能给我一个示例代码来说明它是如何工作的吗?
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>"