2

I'm trying to get an MS chart into an outlook email. I want to avoid saving it onto the desktop so I decided to use a MemoryStream. This is what I've put together so far:

MemoryStream s = new MemoryStream();
chart1.SaveImage(s, ChartImageFormat.Png);
s.Position = 0;

OutlookApp outlookApp = new OutlookApp();
MailItem mailItem = outlookApp.CreateItem(OlItemType.olMailItem);

mailItem.Subject = "This is the subject";
chart1.SaveImage(s, ChartImageFormat.Png);
chart1.SaveImage(s, ChartImageFormat.Png);
mailItem.HTMLBody = "<html><body>This is the <strong>funky</strong> message body</body></html>" ;


mailItem.Attachments.Add(s);

//Set a high priority to the message
mailItem.Importance = OlImportance.olImportanceHigh;
mailItem.To = "testemail@gmail.com";
mailItem.Display(false);
//mailItem.Send();

I just can't seem to find a way to get the MemoryStream into the email.

Sewder
  • 754
  • 3
  • 11
  • 37
  • Not really sure, but the Outlook Object Model does not seem to support adding a file attachment from memory. See [this](https://msdn.microsoft.com/en-us/library/office/ff869553.aspx). I would suggest you take a look at [Outlook Redemption](http://www.dimastr.com/redemption/home.htm). – Yacoub Massad Apr 28 '16 at 21:43
  • Are you trying to create an RTF email with an embedded OLE object (Chart) or an HTML email with an embedded image? – Dmitry Streblechenko Apr 28 '16 at 22:02

1 Answers1

3

Outlook supports sending email using SMTP. So you can use SmtpClient to send an email. Then you can send the image as attachment or put the image in the email body:

var body = @"Here is the chart: <br/> <img src=""$CONTENTID1$""/>";
var CONTENTID1 = Guid.NewGuid().ToString().Replace("-", "");
body = body.Replace("$CONTENTID1$", "cid:" + CONTENTID1);
var htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
var stream = new MemoryStream();
this.chart1.SaveImage(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Position = 0;
LinkedResource imagelink1 = new LinkedResource(stream, "image/png");
imagelink1.ContentId = CONTENTID1;
imagelink1.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
htmlView.LinkedResources.Add(imagelink1);

MailMessage mail = new MailMessage();
mail.From = new MailAddress("someone@outlook.com");
mail.To.Add(new MailAddress("someone@example.com"));
mail.Subject = "Subject";
mail.AlternateViews.Add(htmlView);
SmtpClient client = new SmtpClient();

client.Host = "smtp-mail.outlook.com";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("someone@outlook.com", "password");
client.Send(mail);

This way, the chart image will be shown in body of email:

enter image description here

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • I got this error The server response was: 5.7.3 Requested action aborted; user not authenticated – Sewder Apr 28 '16 at 22:49
  • Check your client settings like Host, Port, etc which are at the end of above code. These settings are very important and I used exactly these settings and it sends an email from my outlook.com account to another account. – Reza Aghaei Apr 28 '16 at 22:51
  • It seems the post answers your question. Also it's the answer to your [new question](http://stackoverflow.com/questions/36943115/save-image-on-my-computer-as-html-to-embed-in-email) which I had not seen yet. – Reza Aghaei May 11 '16 at 13:18