简体   繁体   English

将图像粘贴到 Email 主体中

[英]Paste Image in Email Body

I need to get my Image (which I have stored in the clipbord before) paste into the E-Mail Body.我需要将我的图像(我之前存储在剪贴板中)粘贴到电子邮件正文中。 How can i do it?我该怎么做?

Ive tryed SendKeys.Send("^v");我试过SendKeys.Send("^v"); after the New-Mail Window opend but it didnt work.新邮件 Window 打开后,它没有工作。

Is there maybe a way to put the image directly into the oMailItem.Body = "";有没有办法将图像直接放入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
}

When you paste an image into an email, Outlook attaches the image as a file attachment, then embeds the image into the body.当您将图像粘贴到 email 时,Outlook 将图像作为文件附件附加,然后将图像嵌入到正文中。 You can do the same in your code.您可以在代码中执行相同的操作。 However, the code to attach a file only works with files on the file system, so you need to save your image to the file system before you attach it, then you can delete it.但是, 附加文件的代码仅适用于文件系统上的文件,因此您需要在附加之前将图像保存到文件系统,然后才能将其删除。 You don't need to use the clipboard at all.您根本不需要使用剪贴板。

It would look something like this:它看起来像这样:

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);

I'm assuming you have a using directive at the top of your file like this:我假设您在文件顶部有一个using指令,如下所示:

using Outlook = Microsoft.Office.Interop.Outlook;
**Current  Code:** 

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));

    // 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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM