Search code examples
c#imageoutlookclipboardcom-automation

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

Solution

  • 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.