Click here to Skip to main content
15,881,826 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

Am trying to send an outlook email in a standalone application. As of now it is working fine in sending the text. I want to insert an image into this..pls help
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
using Outlook = Microsoft.Office.Interop.Outlook;

public void sendEMailThroughOUTLOOK()
{
    try
    {
        // Create the Outlook application.
        Outlook.Application oApp = new Outlook.Application();
        // Create a new mail item.
        Outlook.MailItem oMsg =       (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
        // Set HTMLBody. 
        
        //add the body of the email
        oMsg.HTMLBody = "Hello, This is test mail!!";
        //Add an attachment.
        String sDisplayName = "MyAttachment";
        int iPosition = (int)oMsg.Body.Length + 1;
        int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
        //now attached the file
        Outlook.Attachment oAttach = oMsg.Attachments.Add(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg",iAttachType,null, sDisplayName);
        //Subject line
        oMsg.Subject = "Your Subject will go here.";
       
        // Add a recipient.
        Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
        // Change the recipient in the next line if necessary.
        Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("sam@abconsultants.com");
        oRecip.Resolve();
        // Send.
        oMsg.Send();
        
        // Clean up.
        oRecip = null;
        oRecips = null;
        oMsg = null;
        oApp = null;
    }
    //end of try block
    catch (Exception ex)
    {
    }
    //end of catch
}
//end of Email Method


thanks in advance
Posted
Updated 26-Nov-13 22:47pm
v4
Comments
JoCodes 27-Nov-13 4:49am    
Which version of outlook interop are you using?
[no name] 27-Nov-13 4:55am    
2007
JoCodes 27-Nov-13 5:25am    
I have added a solution . can you check and revert?
Richard MacCutchan 27-Nov-13 5:24am    
You need to add it as an attachment to the message.
Pikoh 2-Feb-15 3:44am    
May I ask why are you using an Outlook interop instead of SmtpCLient?

Try this

string imageContentid = "someimage.jpg";

   attachment.PropertyAccessor.SetProperty(
     "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
    , imageContentid 
    );

   oMsg.HTMLBody = String.Format(
     "<body><img src=\"cid:{0}\"></body>"
    , imageContentid 
    );
 
Share this answer
 
Comments
[no name] 27-Nov-13 5:29am    
I have tried to do this...but unfortunately the first line of code
in may case

olAttach doesnt give me the property accessor option
JoCodes 27-Nov-13 5:41am    
Are you sure you are programming against 2007 or above version of outlook?Because below 2007 it wont have propertyaccessor
[no name] 27-Nov-13 5:45am    
Yes am using Office professional 2010.
VB
This is a vb code snippet by Dmitry Streblechenko (MVP).It worked fine for me.
Set objOutlook = CreateObject("Outlook.Application")
      Set Ns = objOutlook.GetNamespace("MAPI")
      Ns.Logon
      Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
      Set objOutlookRecip = objOutlookMsg.Recipients.Add("test@dimastr.com")
      objOutlookRecip.Type = olTo
      objOutlookMsg.Subject = "test"
      ' add graphic as attachment to Outlook message
      Set colAttach = objOutlookMsg.Attachments
      Set l_Attach = colAttach.Add("z:\Temp\8\1.jpg ")
      l_Attach.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x370E001F", "image/jpeg"  'Change From 0x370eE001E
      l_Attach.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x3712001F", "myident" 'Changed from 0x3712001E
      objOutlookMsg.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B", True
       'Set body format to HTML
       objOutlookMsg.BodyFormat = olFormatHTML
       msgHTMLBody = "<html>" & _
                  "<head>" & _
                  "</head>" & _
                  "<body>" & _
                  "        <img align="baseline" border="1" hspace="0" src="cid:myident" width="" 600="" hold=" />                  "></img></body></html>"
         objOutlookMsg.HTMLBody = msgHTMLBody
      objOutlookMsg.Save
      objOutlookMsg.Send

Dmitry Streblechenko (MVP)
http://www.dimastr.com/redemption
Redemption - what the Outlook
Object Model should have been
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900