2
votes

What is the best way of embedding an image into a new outlook mail item from a Stand Alone application. Not building an add-in for outlook.

Trying to embed or attach an existing picture to a new email item. I have read and looked at a lot of sources but most of these are tied into exchange or by using AddIn methods in Outlook.

User would see the image embedded into the new email and would just need to fill out the "To:" field. Would like to make the subject pre-populated in new email message as well from application.

Code Below: (I am trying to attach the picture that I capture below to an outlook email!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Mail;
using System.Net.Mime;



namespace While_You_Were_Out
{
    public partial class main : Form
    {
        public main()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Normal;
            Show();

            Rectangle l = Screen.PrimaryScreen.WorkingArea;
            //Sets Position Manual all other Dialogs are set within parent center area.
            this.StartPosition = FormStartPosition.Manual;
            this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Height - this.Height);
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new AboutDialog().ShowDialog(this);
        }

        private void trayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            WindowState = FormWindowState.Normal;
            Show();

            Rectangle l = Screen.PrimaryScreen.WorkingArea;
            //Sets Position Manual all other Dialogs are set within parent center area.
            this.StartPosition = FormStartPosition.Manual;
            this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Height - this.Height);
        }

        private void trayRightClickMenu2_Opening(object sender, CancelEventArgs e)
        {

        }

        private void toolStripMenuExit_Click(object sender, EventArgs e)
        {
            //Exit Application
            Application.Exit();
        }

        private void toolStripMenuOpen1_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Normal;
            Show();

            Rectangle l = Screen.PrimaryScreen.WorkingArea;
            //Sets Position Manual all other Dialogs are set within parent center area.
            this.StartPosition = FormStartPosition.Manual;
            this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Height - this.Height);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Hides to System Tray
            this.Hide();
            trayIcon.Visible = true;
            //Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {


        }

        private void sendNotificationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            /* Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
             panel1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
             bmp.Save(@"C:\Razor\wywo_notification.jpg");
             bmp.Dispose();*/

            SaveAsBitmap(panel1, @"C:\Users\Razor\wywo_notification.jpg");
        }



        private void clearFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtBox6.Text = string.Empty;
            txtBox8.Text = string.Empty;
            txtBox9.Text = string.Empty;

        }

        public void SaveAsBitmap(Control control, string fileName)
        {
            //get the instance of the graphics from the control
            Graphics g = control.CreateGraphics();

            //new bitmap object to save the image
            Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);

            //Drawing control to the bitmap
            panel1.DrawToBitmap(bmp, new Rectangle(0, 0, control.Width, control.Height));

            bmp.Save(fileName);
            bmp.Dispose();

        }



        private void sendToOutlook_Click(object sender, EventArgs e)
        {  


        }



    }

}

I was able to perform the function with VBS Script:

Set olApp = CreateObject("Outlook.Application")
Set olMsg = olApp.CreateItem(0)

With olMsg
  .To = "[email protected]"
  '.CC = "[email protected]"
  '.BCC = "[email protected]"
  .Subject = "Subject"

.HTMLBody = "<html><p>This is a picture.</p>" & _
                   "<img src='cid:wywo_notification.jpg'>"
  '.Body = "<IMG align=baseline border=0 hspace=0 src=cid:myident>"
  '.Attachments.Add "C:\users\doej\wywo_notification.jpg" 
   '.Attachments.Add "C:\users\doej\wywo_notification.jpg"


  .Display
End With
2

2 Answers

1
votes

I was able to solve my problem by relying on an VBS script called from within the application it was simple and solved my issue.

0
votes

What is your existing code? Create an attachment and set the PR_ATTACH_CONTENT_ID property (DASL name "http://schemas.microsoft.com/mapi/proptag/0x3712001F") using Attachment.PropertyAccessor.

Your HTML body would then need to reference that image attachment through the cid:

img src="cid:xyz"

where xyz is the value of the PR_ATTACH_CONTENT_ID property.

Look at an existing message with OutlookSpy (click IMessage button).