在电子邮件正文中粘贴图像



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

新邮件窗口打开后,我试用了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
}

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

Attachment attachment = newMail.Attachments.Add(
@"E:Picturesimage001.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检查代码是否正常工作。

将图像粘贴到电子邮件中时,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;

相关内容

  • 没有找到相关文章

最新更新