Answered by:
how to embed image in html body in c# into outlook mail

Question
-
hello
I have a problem and It getting me crazy .
I want to embed image into html body in C# application and send throw outlook application
this my code
// Create a new MailItem and set the To, Subject, and Body properties.
Outlook.MailItem newMail = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);
newMail.To = to;
newMail.Subject = subject;
newMail.HTMLBody = body ;
newMail.CC = cc;
newMail.BCC = bcc;
// Retrieve the account that has the specific SMTP address.
Outlook.Account account = GetAccountForEmailAddress(application, smtpAddress);
// Use this account to send the e-mail.
newMail.SendUsingAccount = account;
// I have parameter called source_img , string of path of image like C:\simple.jpg
// how can I embed here the picture , not attach, in same position when I added to html body
newMail.Send();
}Help Me Please !!
Answers
-
Hi,
If you make add-in for Outlook 2007 or higher try to add the following code before newMail.Send():
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 );
Of course you can insert the tag "img" in the existing html body instead of override it like in my example.
- Marked as answer by 许阳(无锡)Moderator Wednesday, September 7, 2011 7:59 AM
-
The basic principle of adding an embedded image is to attach the image to the item and then using the HTMLBody to write HTML to add the attachment cid as a reference in the HTML.The easiest way to see exactly what to add and where is to actually embed an image in an otherwise blank HTML email and then to read out the HTMLBody property to see what HTML was written to reference the attachment.Certain things you'd need to do can be done using PropertyAccessor in Outlook 2007 or 2010, but cannot be done at all in 2003 unless you use Extended MAPI (C++ or Delphi/Pascal only, no managed code) or Redemption (www.dimastr.com/redemption) or some other way of accessing extended attachment properties. CDO 1.21 can't be used as it's also not supported for managed code at all."kamalalbatarny" <=?utf-8?B?a2FtYWxhbGJhdGFybnk=?=> wrote in message news:6c063b27-7e8a-4963-ad5f-ce7e5ffb2c64...
hello
I have a problem and It getting me crazy .
I want to embed image into html body in C# application and send throw outlook application
this my code
// Create a new MailItem and set the To, Subject, and Body properties.
Outlook.MailItem newMail = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);
newMail.To = to;
newMail.Subject = subject;
newMail.HTMLBody = body ;
newMail.CC = cc;
newMail.BCC = bcc;
// Retrieve the account that has the specific SMTP address.
Outlook.Account account = GetAccountForEmailAddress(application, smtpAddress);
// Use this account to send the e-mail.
newMail.SendUsingAccount = account;
// I have parameter called source_img , string of path of image like C:\simple.jpg
// how can I embed here the picture , not attach, in same position when I added to html body
newMail.Send();
}Help Me Please !!
Ken Slovak MVP - Outlook- Marked as answer by 许阳(无锡)Moderator Saturday, August 20, 2011 11:53 AM
-
For the Redemption example, go to http://www.dimastr.com/redemption/objects.htm#examples and scroll down to "Create an HTML message with an embedded image"
Dmitry Streblechenko (MVP)
http://www.dimastr.com/redemption
Redemption - what the Outlook
Object Model should have been- Marked as answer by 许阳(无锡)Moderator Saturday, August 20, 2011 11:53 AM
All replies
-
The basic principle of adding an embedded image is to attach the image to the item and then using the HTMLBody to write HTML to add the attachment cid as a reference in the HTML.The easiest way to see exactly what to add and where is to actually embed an image in an otherwise blank HTML email and then to read out the HTMLBody property to see what HTML was written to reference the attachment.Certain things you'd need to do can be done using PropertyAccessor in Outlook 2007 or 2010, but cannot be done at all in 2003 unless you use Extended MAPI (C++ or Delphi/Pascal only, no managed code) or Redemption (www.dimastr.com/redemption) or some other way of accessing extended attachment properties. CDO 1.21 can't be used as it's also not supported for managed code at all."kamalalbatarny" <=?utf-8?B?a2FtYWxhbGJhdGFybnk=?=> wrote in message news:6c063b27-7e8a-4963-ad5f-ce7e5ffb2c64...
hello
I have a problem and It getting me crazy .
I want to embed image into html body in C# application and send throw outlook application
this my code
// Create a new MailItem and set the To, Subject, and Body properties.
Outlook.MailItem newMail = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);
newMail.To = to;
newMail.Subject = subject;
newMail.HTMLBody = body ;
newMail.CC = cc;
newMail.BCC = bcc;
// Retrieve the account that has the specific SMTP address.
Outlook.Account account = GetAccountForEmailAddress(application, smtpAddress);
// Use this account to send the e-mail.
newMail.SendUsingAccount = account;
// I have parameter called source_img , string of path of image like C:\simple.jpg
// how can I embed here the picture , not attach, in same position when I added to html body
newMail.Send();
}Help Me Please !!
Ken Slovak MVP - Outlook- Marked as answer by 许阳(无锡)Moderator Saturday, August 20, 2011 11:53 AM
-
For the Redemption example, go to http://www.dimastr.com/redemption/objects.htm#examples and scroll down to "Create an HTML message with an embedded image"
Dmitry Streblechenko (MVP)
http://www.dimastr.com/redemption
Redemption - what the Outlook
Object Model should have been- Marked as answer by 许阳(无锡)Moderator Saturday, August 20, 2011 11:53 AM
-
-
What exactly does not work? What is the relevant snippet of your code?
Dmitry Streblechenko (MVP)
http://www.dimastr.com/redemption
Redemption - what the Outlook
Object Model should have been -
Hi,
If you make add-in for Outlook 2007 or higher try to add the following code before newMail.Send():
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 );
Of course you can insert the tag "img" in the existing html body instead of override it like in my example.
- Marked as answer by 许阳(无锡)Moderator Wednesday, September 7, 2011 7:59 AM
-
thank you very very much
It works
- Proposed as answer by sam.sam442000 Monday, September 5, 2011 1:26 PM
-
hi
i am also facing problem wile trying to send email which contain an image inline attachment through Outlook. I am send a mail with the html body, in that html i am trying to set a image as background of table. my C# code is sending the the html mail but attached image is not appearing in the background.
i m using using the following code:
try
{
string mailboby = "<table background=\"ecrad2.jpg\" height=\"677px\" width=\"712px\" style=\" background-repeat: no-repeat;\">";
mailboby += "<tr style=\"height:73px\"><td style=\"width:290px\"></td><td colspan=\"2\"></td><td style=\" width:170px\"></td><td style=\"width:80px\"></td></tr>";
mailboby += "</table>";
// 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 = mailboby;
//Add an attachment.
String sDisplayName = "Ecard";
int iPosition = (int)oMsg.Body.Length + 1;
int iAttachType = (int)Outlook.OlAttachmentType.olEmbeddeditem;
//now attached the file
string relativePath = "~/" + Request.QueryString["Ecard"];
Outlook.Attachment oAttach = oMsg.Attachments.Add(Server.MapPath(relativePath), iAttachType, iPosition, 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("hemant.sharma25oct@gmail.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
i am attaching a image with same name with which i am referring in my html code background=\"ecrad2.jpg\"
plz guide me for the solution
- Edited by sam.sam442000 Tuesday, September 6, 2011 6:59 AM
-
Hi,
Is it important for you to set a background image for a table?
You can easely set it for a body:
<body background="cid:{0}"...
But I think it will work correctly only if open it with Outlook.
Here is an interesting articles about background images:
http://blog.mailermailer.com/2011/04/background-images-in-html-email-the-naked-truth/
- Edited by bon_ Monday, September 5, 2011 10:13 PM
-
thanks bon to to reply me.
have done this with System.Net.Mail
but i am not getting that how to add Content ID property while i am creating an Attachment for outlook mail.
Outlook.Attachment oAttach = oMsg.Attachments.Add(Server.MapPath(relativePath), iAttachType, iPosition, sDisplayName);
in the above code: oAttach do not have ConentID property
so i am getting that how will I refer my attached image in HTML body
-
Hi Sam,
You should set Content ID for an attachment manually as I have wrote above
string imageCid = "image001.jpg@123"; attachment.PropertyAccessor.SetProperty( "http://schemas.microsoft.com/mapi/proptag/0x3712001E" , imageCid );
After that you can refer on this attachment by this id ("cid:image001.jpg@123") in html body. -
Hi Bon
i have try as you suggested, but it is giving some compilation error:
Error 22 The name 'OlAttachmentType' does not exist in the current context C:\Users\RSPL48\Desktop\layout_sample\Program\E-cardAutomail\E-cardAutomail\PreviewEcard.aspx.cs 224 135 E-cardAutomail
Error 23 'System.Net.Mail.Attachment' does not contain a definition for 'PropertyAccessor' and no extension method 'PropertyAccessor' accepting a first argument of type 'System.Net.Mail.Attachment' could be found (are you missing a using directive or an assembly reference?) C:\Users\RSPL48\Desktop\layout_sample\Program\E-cardAutomail\E-cardAutomail\PreviewEcard.aspx.cs 228 24 E-cardAutomail
i am using the following code and highlighting the code where visual studio is showing the error--
using Outlook = Microsoft.Office.Interop.Outlook;
----
-------
------------
-------------------
Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem newMail = (Microsoft.Office.Interop.Outlook.MailItem)application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
newMail.To = "hemant.sharma25oct@gmail.com";
newMail.Subject = "ecard";
// Retrieve the account that has the specific SMTP address.
//Microsoft.Office.Interop.Outlook.Account account = GetAccountForEmailAddress(application, smtpAddress);
// Use this account to send the e-mail.
string relativePath = "~/" + Request.QueryString["Ecard"];
//Attachment imgAtt = new Attachment(Server.MapPath(relativePath));
//newMail.SendUsingAccount = account;
System.Net.Mail.Attachment attachment = (System.Net.Mail.Attachment)newMail.Attachments.Add(Server.MapPath(relativePath), OlAttachmentType.olEmbeddeditem, null, "Some image display name");
attachment.ContentId = "sample_card_layout2.jpg";
string imageCid = "sample_card_layout2.jpg";
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid);
newMail.HTMLBody = mailboby;
newMail.Send();
-
Hi Sam,the method "MailItem.Attachments.Add" returns an object of the type "Microsoft.Office.Interop.Outlook.Attachment" and you can't convert it to "System.Net.Mail.Attachment".PropertyAccsessor is highlighted because it is a property of Microsoft.Office.Interop.Outlook.Attachment class. There is no such a property in System.Net.Mail.Attachment.And you use the alias for Microsoft.Office.Interop.Outlook so you should write "Outlook.OlAttachmentType.olEmbeddeditem".
- Edited by bon_ Friday, September 9, 2011 12:36 PM
-
-
hi bon,
Hope you are doing good!
In some other program, I am sending a mail with Inline Image attachment. I am doing this with the help of Mail class and Attachment class.
My code is:
mail.Body = message; // html body code
mail.IsBodyHtml = true;
Attachment imgAtt = new Attachment(Server.MapPath(relativePath));
imgAtt.ContentId = "sample_card_layout2.jpg";
mail.Attachments.Add(imgAtt);
SmtpClient client = new SmtpClient(SMTPserver, SMTPportNO);
NetworkCredential credentials = new NetworkCredential(MYEmailAccount, password);
//add credentials to our smtp client
client.Credentials = credentials;
//try to send the mail message
client.Send(mail);
with this code i am able to send Image as Inline attachment, when I am browsing my mail account through and browser, attached image appears in the body of the mail. But when I fetch my account through OUTLOOK that image is not appearing in the body of the mail, Outlook shows that image as an attached file.
Please help me to resolve this problem.
-
Hi Sam.
Please, make sure that ContentId that you have set in the message body and ContentId set for the attachment are the same. I have tried to send an email and it has appeared in Outlook properly.
Attachment attach = new Attachment(@"C:\Temp\12.jpg"); attach.ContentId = "some_content_id"; MailMessage message = new MailMessage("...", "..."); message.Subject = "Test"; message.Body = "<img src=\"cid:some_content_id\">"; message.IsBodyHtml = true; message.Attachments.Add(attach); SmtpClient client = new SmtpClient("...", port); client.Credentials = new NetworkCredential("...", "..."); try { client.Send(message); } catch (Exception ex) { Trace.Write(ex.Message); }
-
-
The property is documented at
http://msdn.microsoft.com/en-us/library/office/cc765868.aspx and
http://msdn.microsoft.com/en-us/library/ee218702(v=exchg.80).aspx
Take a look at an existing message with embedded images using OutlookSpy (click IMessage button, go to the GetAttachmentTable tab, double click on an attachment).
Dmitry Streblechenko (MVP) http://www.dimastr.com/redemption
Redemption - what the Outlook
Object Model should have been
Version 5.5 is now available!