Home > Software design >  Paste Image in Email Body
Paste Image in Email Body

Time:01-22

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"); after the New-Mail Window opend but it didnt work.

Is there maybe a way to put the image directly into the 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 = "[email protected]";
        oMailItem.CC = "[email protected]";

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

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

CodePudding user response:

The following code embeds an image into the message body:

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

If you still got an exception in the code I'd suggest using VBA to check whether the code is working correctly.

CodePudding user response:

When you paste an image into an email, Outlook attaches the image as a file attachment, then embeds the image into the body. 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 = "[email protected]";
oMailItem.CC = "[email protected]";

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 Outlook = Microsoft.Office.Interop.Outlook;
  •  Tags:  
  • Related