0

我需要将我的图像(我之前存储在剪贴板中)粘贴到电子邮件正文中。我该怎么做?

SendKeys.Send("^v");在新邮件窗口打开后尝试过,但没有用。

有没有办法将图像直接放入oMailItem.Body = "";

private void mailsenden() // Versendet die E-Mail
    {

        Bitmap bmp = new Bitmap(PanelErstmeldung.Width, PanelErstmeldung.Height);
        PanelErstmeldung.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
        Clipboard.SetDataObject(bmp);
        

        Outlook.Application oApp = new Outlook.Application();
        _MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

        oMailItem.Subject = "Betriebsstörung im Bereich  "+ comboBox1.SelectedItem;
        oMailItem.To = "test@test.com";
        oMailItem.CC = "test2@test2.com";

        oMailItem.Body = "";   // PASTE THE BITMAP bmp HERE in the Body

        oMailItem.Display(true); // Or CTRL+V it here in the opend Window
}
4

2 回答 2

0

以下代码将图像嵌入到消息正文中:

Attachment attachment = newMail.Attachments.Add(
     @"E:\Pictures\image001.jpg", OlAttachmentType.olEmbeddeditem
    , null, "Some image display name");

   string imageCid = "image001.jpg@123";

   attachment.PropertyAccessor.SetProperty(
     "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
    , imageCid
    );

   newMail.HTMLBody = String.Format("<body><img src=\"cid:{0}\"></body>", imageCid);

如果您仍然在代码中遇到异常,我建议您使用 VBA 来检查代码是否正常工作。

于 2022-01-21T18:20:18.647 回答
0

当您将图像粘贴到电子邮件中时,Outlook 会将图像作为文件附件附加,然后将图像嵌入到正文中。您可以在代码中执行相同的操作。但是,附加文件的代码仅适用于文件系统上的文件,因此您需要在附加之前将图像保存到文件系统,然后才能将其删除。您根本不需要使用剪贴板。

它看起来像这样:

Bitmap bmp = new Bitmap(PanelErstmeldung.Width, PanelErstmeldung.Height);
PanelErstmeldung.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));

// Save image to temp file
var tempFileName = Path.GetTempFileName();
bmp.Save(tempFileName, ImageFormat.Jpeg);

Outlook.Application oApp = new Outlook.Application();
Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

oMailItem.Subject = "Betriebsstörung im Bereich  "+ comboBox1.SelectedItem;
oMailItem.To = "test@test.com";
oMailItem.CC = "test2@test2.com";

var attachment = oMailItem.Attachments.Add(tempFileName);

// Set the Content ID (CID) of the attachment, which we'll use
// in the body of the email
var imageCid = "image001.jpg@123";
attachment.PropertyAccessor.SetProperty(
             "http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid);

// Set HTML body with an <img> using the CID we gave it
oMailItem.HTMLBody = $"<body><img src=\"cid:{imageCid}\"></body>";

oMailItem.Display(true);

File.Delete(tempFileName);

我假设您using在文件顶部有一个指令,如下所示:

using Outlook = Microsoft.Office.Interop.Outlook;
于 2022-01-21T17:35:48.373 回答