Align Outlook htmlbody right to left - c#

How do I align a HTMLBody in Outlook to be right to left?
Here's my code for sending the message (the body is in a textbox)
private void sendmail()
{
outlook.Application outApp;
outApp = new outlook.Application();
outlook.MailItem mail = (outlook.MailItem)(outApp.CreateItem
(outlook.OlItemType.olMailItem));
mail.BodyFormat = outlook.OlBodyFormat.olFormatHTML;
mail.To = textTo.Text;
mail.CC = textCC.Text;
mail.Subject = textSubject.Text;
mail.HTMLBody = textBody.Text;
}

if by right to left align you mean RTL scripts use this
mail.HTMLBody = "<p DIR=\"RTL\">" + textBody.Text + "</p>";
if you just want the text to be aligned either left or right use this snippet:
mail.HTMLBody = "<p style=\"text-align:left;\">" + textBody.Text + "</p>";//aligned left

Try wrapping your textBody.Text string in the following HTML
<table width='100%'><tr><td align="right">[YOUR TEXTBODY.TEXT VAR GOES HERE]</td></tr></table>
e.g.
mail.HTMLBody = "<table width='100%'><tr><td align="right">"+textBody.Text+"</td></tr></table>";
or if you wanted your body content to be a certain width
mail.HTMLBody = "<table width='600'><tr><td align="right">"+textBody.Text+"</td></tr></table>";
Hope this helps

Related

Couldn't find image with CID in windows service

I am testing the sending of an email with images using CID tag, I make a desktop application that only runs the mail, the program asked me to also place them in the debug folder of the project apart from the folder where I put them, Now I send it to a windows service but it tells me that
Could not find file 'C:\WINDOWS\system32\ img.png
I already put the image in that folder but it still gives me the same error, the image is type .png, this is my email code
private void SendMAil()
{
string htmlBody = "<!DOCTYPE html>" +
"<html xmlns = 'http://www.w3.org/1999/xhtml'>" +
"<head>" +
"<meta http - equiv = 'Content-Type' content = 'text/html; charset=UTF-8'/>" +
"<title> Demystifying Email Design</title>" +
"<meta name = 'viewport' content = 'width=device-width, initial-scale=1.0'/>" +
"</head>" +
"<body style = 'margin: 0; padding: 0;'>" +
"<table align = 'center' border = '0' cellpadding = '0' cellspacing = '0' width = '900' > " +
"<tr>" +
"<td align='left' bgcolor='#F8F8F8' style='padding: 15px 0 15px 0;border-bottom-width:6px;border-bottom-color:#225100;border-bottom-style:solid;'>" +
" <img src=\"cid:img\"' width='90' height='40'>" +
"</td>" +
"</tr>" +
"<tr>" +
"<td bgcolor = '#ffffff' style='padding:30px 30px 65px 30px'>" +
"HELLO!!!"+
"</td>" +
"</tr>" +
"<tr>" +
"<td bgcolor = '#FFFFFF' align='center' style='padding: 15px 0 15px 0;border-top-width:1px;border-top-color:#FA5300;border-top-style:solid;'>" +
"</td>" +
"</tr>" +
"</table>" +
"</body>" +
"</html>";
AlternateView avHtml = AlternateView.CreateAlternateViewFromString
(htmlBody, null, MediaTypeNames.Text.Html);
LinkedResource inline = new LinkedResource("img.png", MediaTypeNames.Image.Jpeg);
inline.ContentId = "img";
avHtml.LinkedResources.Add(inline);
Attachment att1 = new Attachment(#"img\img.png");
att1.ContentDisposition.Inline = true;
mail.From = new MailAddress("xx#xx.com");
mail.To.Add("xx#xx.com");
mail.Subject = "Alerta Estado Tags";
mail.Body = inline.ContentId;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient
{
Credentials =
new NetworkCredential("xx#xx.com", "****"),
Host = "smtp.gmail.com",
Port = 000,
EnableSsl = true
};
smtp.Send(mail);
mail.Dispose();
}
Where do I put the images? and What would be the path?
Please review your code, you are using 2 paths for the img.png file, once under without specifying any additional path, which will result in the program trying to search the file under the current working directory (system32 in your case) and once searching inside the img folder (which should also be in your current working directory).
There are other issues in your code example,
you are not associating the avHtml to the email message anywhere.
mail.AlternateViews.Add
where do you initializing the mail object?
But the most important, is how do you execute your program?
I assume you are using the windows command line and dragging the program with its full path, which causes your current working directory to stay under System32 (which is the default directory of CMD when running as Administrator.
Therefore, it looks for img.png and img/img.png under System32
The cid tag is used for resources embedded in the email itself. So you have to add the image as an attachment with a certain code (don't have it at the ready at the moment).
That said: don't use it. It's obsolete and blocked by many email clients. Dump your pictures on a webserver and just link to them.

Sending email with calender invite and html doesn't show correctly

I'm trying to send a mail containing html with inline images as well as a calender invitation (.ics). I have tested that both elements at least work seperately.
If I use Gmail.com to view the mail it displays all elements correctly. Showing information about the event at the top of the mail and showing the text with images. The issue is that depending on the order that I create the alternateViews, it either works with Outlook or Iphone, it always works with gmail.com/gmailclient but not all three at the same time.
SmtpClient sc = new System.Net.Mail.SmtpClient(IP);
MailMessage msg = new MailMessage();
msg.From = new System.Net.Mail.MailAddress("demomail#hotmail.com", "bob");
string mailTo = "client#gmail.com";
msg.To.Add(new MailAddress(mailTo, "Client Name"));
msg.Subject = "Send Calendar Appointment Email";
string mailBody = $"<br><div class=\"dagsordenMødeDato\"> Her er dagsordenen for mødet 10-10-2018 14:45:00 </div><br> <br><ul class=\"dagsordenListe\"><li>Leasing til dig</li><li>Pension</li><li>Garantkunde</li><li>Dine fordele</li></ul><br> <div class=\"bekrivelseAfVedhæftning\">Jeg har vedhæftet et dokument.</div>";
string icsContent = System.IO.File.ReadAllText(#"H:\hjemmelavetICS.ics");
If the alternateView for the calender is placed before the alternateView for Html in the code then it works on 'Mail' for iphone
ContentType contype = new ContentType("text/calendar");
contype.Parameters.Add("method", "REQUEST");
contype.Parameters.Add("name", "Invitation.ics");
contype.CharSet = "utf-8";
AlternateView avCal =
AlternateView.CreateAlternateViewFromString(icsContent, contype);
msg.AlternateViews.Add(avCal);
LinkedResource inlineRådgiver = new LinkedResource(
#"K:\MEDARBEJDERBILLEDER\Signatur Notes\" + "fn" + ".jpg",
MediaTypeNames.Image.Jpeg);
inlineRådgiver.ContentId = "raadgiverID";
inlineRådgiver.ContentType.Name = "Raadgiver.jpg";
LinkedResource inlineKampagne = new LinkedResource(
#"K:\MEDARBEJDERBILLEDER\Signatur Notes\" + "Kampagne" + ".jpg",
MediaTypeNames.Image.Jpeg);
inlineKampagne.ContentId = "kampagneID";
inlineKampagne.ContentType.Name = "Kampagne.jpg";
var htmlContentType =
new System.Net.Mime.ContentType("text/html");
htmlContentType.CharSet = "utf-8";
string embededHtml =
"<img src=cid:raadgiverID alt=\"medarbejder\"></p><img style=\"border: 0;\" src=cid:kampagneID alt=\"kampagne\">";
string fullHtml = mailBody + embededHtml;
var avHtmlBody = AlternateView.CreateAlternateViewFromString(fullHtml, htmlContentType);
avHtmlBody.LinkedResources.Add(inlineRådgiver);
avHtmlBody.LinkedResources.Add(inlineKampagne);
msg.AlternateViews.Add(avHtmlBody);
sc.Send(msg);
However if I add the alternateview for html before the calender then it works for Outlook.
I have been scratching my head over this for a week now, any help would be appreciated.

Change text to Bold in Outlook Message Body

please help me with following.
I'm working on creating and modifying outlook messages from template. I need to change some text to Bold.
foreach (XmlNode node in nodeList)
{
string CustomerName = node.SelectSingleNode("CustomerName").InnerText;
string ReportName = node.SelectSingleNode("ReportName").InnerText + ".pdf";
Outlook.Application mailApplication = new Outlook.Application();
Outlook.MailItem mail = mailApplication.CreateItemFromTemplate(#"d:\Friday Report\#TEMPLATES\template.oft") as Outlook.MailItem;
mail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
mail.Attachments.Add(#"d:\Friday Report\" + ReportName);
mail.Subject = "Application Packaging – Weekly Summary";
CustomerName = "<b>" + CustomerName + "</b> ";
string body = mail.Body;
string new_body = body.Replace("CustomerName", CustomerName );
mail.Body = new_body;
mail.Display(true);
mail.Close(Outlook.OlInspectorClose.olDiscard);
}
If you want to use HTML in your email, you need to set the HTMLBody property instead of Body:
foreach (XmlNode node in nodeList)
{
string CustomerName = node.SelectSingleNode("CustomerName").InnerText;
string ReportName = node.SelectSingleNode("ReportName").InnerText + ".pdf";
Outlook.Application mailApplication = new Outlook.Application();
Outlook.MailItem mail = mailApplication.CreateItemFromTemplate(#"d:\Friday Report\#TEMPLATES\template.oft") as Outlook.MailItem;
mail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
mail.Attachments.Add(#"d:\Friday Report\" + ReportName);
mail.Subject = "Application Packaging – Weekly Summary";
CustomerName = "<b>" + CustomerName + "</b> ";
string body = mail.Body;
string new_body = body.Replace("CustomerName", CustomerName );
mail.HTMLBody = new_body;
mail.Display(true);
mail.Close(Outlook.OlInspectorClose.olDiscard);
}
You should use valid HTML, though, by surrounding your mail with <html><body>{your message}</body></html>
This seems to work (see screen shot below the code)
using Microsoft.Office.Interop.Outlook;
using outlookApp = Microsoft.Office.Interop.Outlook;
namespace z_Console_Scratch
{
class Program
{
static void Main(string[] args)
{
Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem mailItem = (Microsoft.Office.Interop.Outlook.MailItem)outlookApp.CreateItem(OlItemType.olMailItem);
mailItem.Subject = "test subject";
mailItem.HTMLBody = "<html><body>This is the <strong>funky</strong> message body</body></html>";
mailItem.Display(false);
}
}
}
Note: This works as well: mailItem.HTMLBody = "<html><body>This is the <b>funky</b> message body</body></html>";
Screen Shot

Adding Image to System.Net.Mail Message

I have some images stored in the Resources.resx file in my solution. I would like to use these images in my email. I have loaded the image into a variable:
Bitmap myImage = new Bitmap(Resources.Image);
and now I want to put it in the HTML in the AlternateView string I am using to create the HTML email. Just need some help.
Here is the HTML string(partial):
body += "</HEAD><BODY><DIV style='height:100%; width:700px;'><div style='height:70px; width:700px; background-color:red;'><img src='" + myImage + "' width='104' height='27' alt='img' style='margin: 20px 0px 0px 20px;'/></div>
Any help would be greatly appreciated Thanks!
EDIT: Here is the entire Code block. I think I am close to getting it, just inexperience getting in the way here :) I tried converting it into a Byte like suggested which got me farther. Still not rendering the image. There is something here I am not doing right. Thank you so much for all of you help everyone! Here is the code (the HTML I need for the image is in the 3 line of the string body = code):
if (emailTo != null)
{
Bitmap myImage = new Bitmap(Resources.comcastHeader);
ImageConverter ic = new ImageConverter();
Byte[] ba = (Byte[])ic.ConvertTo(myImage, typeof(Byte[]));
MemoryStream image1 = new MemoryStream(ba);
LinkedResource headerImage = new LinkedResource(image1, "image/jpeg");
headerImage.ContentId = "companyLogo";
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("" + emailTo + "");
message.Subject = "" + customer + " Your order is being processed...";
message.From = new System.Net.Mail.MailAddress("noreply#stormcopper.com");
string body = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
body += "<HTML><HEAD><META http-equiv=Content-Type content=\"text/html; charset=iso-8859-1\">";
body += "</HEAD><BODY><DIV style='height:100%; width:700px;'><div style='height:70px; width:700px; background-color:red;'><img src=\"cid:companyLogo\" width='104' height='27' alt='img' style='margin: 20px 0px 0px 20px;'/></div><P>Hello " + customer + ",</P><P>Thank you for shopping at <a href='" + store + "'>" + store + "</A>. Your order is being processed and will be shipped to you soon. We would like to take this time to thank you for choosing Storm Copper Components, and we hope you are completely satisfied with your purchase. Please review your information and make sure all the information is correct. If there are any errors in your order, please contact us immediately <A href='mailto:busbar#stormcopper.com'>here.</A></P>";
body += "<P><B>Here is your order information:</B></P>";
body += "<H3>Contact Information</H3><TABLE><TR><TD><B>Name:</B> " + customer + "</TR></TD><TR><TD><B>Address:</B> " + street + " " + city + ", " + state + " " + zip + "</TR></TD><TR><TD><B>Email:</B> " + emailTo + "</TR></TD><TR><TD><B>Phone:</B> " + phone + "</TR></TD><TR><TD></TD></TR></TABLE>";
body += "<H3>Products Ordered</H3><TABLE>" + productInformation + "</TABLE><BR /><BR />";
body += "<H3>Pricing Information</H3><TABLE><TR><TD>Subtotal: $" + subTotal + "</TD></TR><TR><TD>Shipping: $" + shippingInfo + " </TD></TR><TR><TD>Tax: $" + taxInfo + "</TD></TR><TR><TD><B>Total:</B> $" + total + "</TD></TR><BR /></TABLE>";
body += "<P>Thank you for shopping with us!</P><A href='stormcopper.com'>Storm Copper Components</A>";
body += "<P><I>This is an Auto-Generated email sent by store copper. Your email will not be sent to Storm Copper Components if you reply to this message. If you need to change any information, or have any questions about your order, please contact us using the information provided in this email.</I></P></DIV></BODY></HTML>";
ContentType mimeType = new System.Net.Mime.ContentType("text/html");
AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, mimeType);
message.AlternateViews.Add(alternate);
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("########");
smtp.Send(message);
}
you need to add them in the email message as CID's/linked resources.
here is some code I have used before which works nicely. I hope this gives you some guidance:
Create an AlternateView:
AlternateView av = AlternateView.CreateAlternateViewFromString(body, null, isHTML ? System.Net.Mime.MediaTypeNames.Text.Html : System.Net.Mime.MediaTypeNames.Text.Plain)
Create a Linked Resource:
LinkedResource logo = new LinkedResource("SomeRandomValue", System.Net.Mime.MediaTypeNames.Image.Jpeg);
logo.ContentId = currentLinkedResource.Key;
logo.ContentType = new System.Net.Mime.ContentType("image/jpg");
// add it to the alternative view
av.LinkedResources.Add(logo);
// finally, add the alternative view to the message:
msg.AlternateView.Add(av);
here is some documentation to help you with what the AlternativeView and LinkedResources are and how it works:
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.alternateviews(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.net.mail.linkedresource(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/ms144669(v=vs.110).aspx
in the HTML itself, you need to do something like the following:
"<img style=\"width: 157px; height: 60px;\" alt=\"blah blah\" title=\"my title here\" src=\"cid:{0}\" />";
notice the CID followed by a string format {0} - I then use this to replace it with a random value.
UPDATE
To go back and comment on the posters comments... here is the working solution for the poster:
string body = "blah blah blah... body goes here with the image tag: <img src=\"cid:companyLogo\" width="104" height="27" />";
byte[] reader = File.ReadAllBytes("E:\\TestImage.jpg");
MemoryStream image1 = new MemoryStream(reader);
AlternateView av = AlternateView.CreateAlternateViewFromString(body, null, System.Net.Mime.MediaTypeNames.Text.Html);
LinkedResource headerImage = new LinkedResource(image1, System.Net.Mime.MediaTypeNames.Image.Jpeg);
headerImage.ContentId = "companyLogo";
headerImage.ContentType = new ContentType("image/jpg");
av.LinkedResources.Add(headerImage);
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.AlternateViews.Add(av);
message.To.Add(emailTo);
message.Subject = " Your order is being processed...";
message.From = new System.Net.Mail.MailAddress("xxx#example.com");
ContentType mimeType = new System.Net.Mime.ContentType("text/html");
AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, mimeType);
message.AlternateViews.Add(alternate);
// then send message!
One option is put the image in one host, and in the src put the url, like this "src='http://www.host/myImage.jpg'"
This way you do not have to load the image in each mail and would be more agile.
If it helps anyone, here is a version based on Ahmed ilyas' very useful answer, which passes the actual Bitmap to the memory stream, and encloses the various objects which implement IDisposable in using blocks -
public void SendMailExample(string emailAddressTo, string hexColour)
{
// Give the LinkedResource an ID which should be passed into the 'cid' of the <img> tag -
var linkedResourceId = "mylogo";
var sb = new StringBuilder("");
sb.Append("<body><p>This is the HTML email body with img tag...<br /><br />");
sb.Append($"<img src=\"cid:{linkedResourceId}\" width=\"100\" height=\"115.5\" alt=\"Logo\"/>");
sb.Append("<p></body>");
var emailBodyHtml = sb.ToString();
var emailBodyPlain = "This is the plain text email body";
using (var message = new MailMessage())
using (var logoMemStream = new MemoryStream())
using (var altViewHtml = AlternateView.CreateAlternateViewFromString(emailBodyHtml, null, System.Net.Mime.MediaTypeNames.Text.Html))
using (var altViewPlainText = AlternateView.CreateAlternateViewFromString(emailBodyPlain, null, System.Net.Mime.MediaTypeNames.Text.Plain))
using (var client = new System.Net.Mail.SmtpClient(_smtpServer)
{
Port = 25,
DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
EnableSsl = false
})
{
message.To.Add(emailAddressTo);
message.From = new MailAddress(_emailAddressFrom);
message.Subject = "This is the email subject";
// Assume that GetLogo() just returns a Bitmap (for my particular problem I had to return a logo in a specified colour, hence the hexColour parameter!)
Bitmap logoBitmap = GetLogo(hexColour);
logoBitmap.Save(logoMemStream, System.Drawing.Imaging.ImageFormat.Png);
logoMemStream.Position = 0;
using (LinkedResource logoLinkedResource = new LinkedResource(logoMemStream))
{
logoLinkedResource.ContentId = linkedResourceId;
logoLinkedResource.ContentType = new ContentType("image/png");
altViewHtml.LinkedResources.Add(logoLinkedResource);
message.AlternateViews.Add(altViewHtml);
message.AlternateViews.Add(altViewPlainText);
client.Send(message);
}
}
}
I know this is an old post, but if someone still hits this looking for an answer (like I did), then I have a simpler answer.
You can use a simple overload of LinkedResource object that directly takes the file path as the parameter. So there is no need to explicitly load image into memory stream. Of course, this example assumes you have access to the image on the disk -
Here is the complete function code which worked for me -
public System.Net.Mail.AlternateView GetAlternateView(string MessageText, string LogoPath, bool bSilent)
{
try
{
MessageText += "<br><img title='' alt='' src='cid:SystemEmail_HTMLLogoPath' />";
System.Net.Mail.AlternateView view = System.Net.Mail.AlternateView.CreateAlternateViewFromString(MessageText, null, "text/html");
System.Net.Mail.LinkedResource linked = new System.Net.Mail.LinkedResource(HttpContext.Current.Request.PhysicalApplicationPath + LogoPath);
linked.ContentId = "SystemEmail_HTMLLogoPath";
view.LinkedResources.Add(linked);
return view;
}
catch (Exception ex)
{
if (bSilent)
return null;
else
throw ex;
}
}
you should not assign the Bitmap object to <img> tag as it expects the image path.
Replace this:
body += "</HEAD><BODY><DIV style='height:100%; width:700px;'><div style='height:70px; width:700px; background-color:red;'><img src='" + myImage + "' width='104' height='27' alt='img' style='margin: 20px 0px 0px 20px;'/></div>
With following :
body += "</HEAD><BODY><DIV style='height:100%; width:700px;'><div style='height:70px; width:700px; background-color:red;'><img src='" + Resources.Image+ "' width='104' height='27' alt='img' style='margin: 20px 0px 0px 20px;'/></div>

Embedding picture to outlook email body

I have the following code:
string imageSrc = "C:\\Documents and Settings\\menonsu\\Desktop\\screenScrapper\\Bitmap1.bmp";
oMsg.HTMLBody = "<HTML><BODY><img src = \" cid:Bitmap1.bmp#embed \"/><br><font size=\"2\" face=\"Courier New\">" + introText + "</font>" + body + "<font size=\"2\" face=\"Courier New\">" + conclText + "</font>" + " </BODY></HTML>";
Microsoft.Office.Interop.Outlook.Attachment attc = oMsg.Attachments.Add(imageSrc, Microsoft.Office.Interop.Outlook.OlAttachmentType.olEmbeddeditem, null, "");
attc.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", "Bitmap1.bmp#EMBED");
//Send the message.
oMsg.Save();
For some reason the email is just showing an x when i try to run this code...anyone know why?
The following is working code with two ways of achieving this:
using System;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Method1();
Method2();
}
public static void Method1()
{
Outlook.Application outlookApp = new Outlook.Application();
Outlook.MailItem mailItem = outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.Subject = "This is the subject";
mailItem.To = "john#example.com";
string imageSrc = "D:\\Temp\\test.jpg"; // Change path as needed
var attachments = mailItem.Attachments;
var attachment = attachments.Add(imageSrc);
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x370E001F", "image/jpeg");
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "myident"); // Image identifier found in the HTML code right after cid. Can be anything.
mailItem.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B", true);
// Set body format to HTML
mailItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
string msgHTMLBody = "<html><head></head><body>Hello,<br><br>This is a working example of embedding an image unsing C#:<br><br><img align=\"baseline\" border=\"1\" hspace=\"0\" src=\"cid:myident\" width=\"\" 600=\"\" hold=\" /> \"></img><br><br>Regards,<br>Tarik Hoshan</body></html>";
mailItem.HTMLBody = msgHTMLBody;
mailItem.Send();
}
public static void Method2()
{
// Create the Outlook application.
Outlook.Application outlookApp = new Outlook.Application();
Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
//Add an attachment.
String attachmentDisplayName = "MyAttachment";
// Attach the file to be embedded
string imageSrc = "D:\\Temp\\test.jpg"; // Change path as needed
Outlook.Attachment oAttach = mailItem.Attachments.Add(imageSrc, Outlook.OlAttachmentType.olByValue, null, attachmentDisplayName);
mailItem.Subject = "Sending an embedded image";
string imageContentid = "someimage.jpg"; // Content ID can be anything. It is referenced in the HTML body
oAttach.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageContentid);
mailItem.HTMLBody = String.Format(
"<body>Hello,<br><br>This is an example of an embedded image:<br><br><img src=\"cid:{0}\"><br><br>Regards,<br>Tarik</body>",
imageContentid);
// Add recipient
Outlook.Recipient recipient = mailItem.Recipients.Add("john#example.com");
recipient.Resolve();
// Send.
mailItem.Send();
}
}
}
From what I can tell, you are not setting the content id properly. Try to change the code to the following:
attc.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x370E001F", "image/bmp");
attc.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "Bitmap1.bmp#EMBED");
I have done this before a little differently. I embedded images in some emails that I had to send from my web app using an 'alternate view' in the system.net.mail
System.Net.Mail.LinkedResource theContent = new System.Net.Mail.LinkedResource({path to image});
theContent.ContentID = "TheContent";
String altViewString = anEmail.Body.replace("{original imageSource i.e. '../Images/someimage.gif'}","cid:TheContent");
System.Net.Mail.AlternateView altView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(altViewString, Nothing, System.Net.Mime.MediaTypeNames.Text.Html);
altView.LinkedResources.add(theContent);
anEmail.Message.AlternateViews.Add(altView);
Here's a simple solution:
private static void insertPictureAsLink(Outlook.MailItem mail, String imagePath, String URI)
{
mail.BodyFormat = OlBodyFormat.olFormatHTML;
mail.HTMLBody += String.Format("<body></body>", imagePath, URI);
mail.Display(false);
}

Categories