在电子邮件正文中粘贴图像
Paste Image in Email Body
我需要将我的图像(我之前存储在剪贴板中)粘贴到电子邮件正文中。我该怎么做?
我在新邮件 Window 打开后尝试了 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
}
当您将图像粘贴到电子邮件中时,Outlook 会将图像附加为文件附件,然后将图像嵌入到正文中。您可以在代码中执行相同的操作。但是,the code to attach a file 仅适用于文件系统中的文件,因此您需要先将图像保存到文件系统中,然后才能将其删除。您根本不需要使用剪贴板。
看起来像这样:
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;
以下代码将图像嵌入到邮件正文中:
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 检查代码是否正常工作。
我需要将我的图像(我之前存储在剪贴板中)粘贴到电子邮件正文中。我该怎么做?
我在新邮件 Window 打开后尝试了 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
}
当您将图像粘贴到电子邮件中时,Outlook 会将图像附加为文件附件,然后将图像嵌入到正文中。您可以在代码中执行相同的操作。但是,the code to attach a file 仅适用于文件系统中的文件,因此您需要先将图像保存到文件系统中,然后才能将其删除。您根本不需要使用剪贴板。
看起来像这样:
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;
以下代码将图像嵌入到邮件正文中:
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 检查代码是否正常工作。