0

I try to send email with embedded images. Images are going from List<Bitmap> and I am sure they are there 100%.

Somehow when I get email I don't see any images at all and HTML looks like

<img alt="" hspace="0" src="http://image0" align="baseline" border="0">
<br />

<img alt="" hspace="0" src="http://image1" align="baseline" border="0">
<br />

Any clue?

C# is like

var smtp = new SmtpClient();

var msg = new MailMessage(new MailAddress("support@mysite.com", "mysite.com Support"),
                                new MailAddress(email, email));
msg.Subject = "No worries, man";
msg.IsBodyHtml = true;
var bodyBuilder = new StringBuilder();

for (int i = 0; i < pages.Count; i++)
   bodyBuilder.AppendLine(string.Format("<img alt=\"\" hspace='0' src='cid:image{0}' align='baseline' border='0'><br />",i));
var htmlView = AlternateView.CreateAlternateViewFromString(bodyBuilder.ToString(), null, "text/html");

var index = 0;
foreach (var page in pages) // where page is List<Bitmap>
{
   var memoryStream = new MemoryStream();
   page.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);

   var imagelink = new LinkedResource(memoryStream, "image/png");
   imagelink.ContentId = string.Format("image{0}", index); 
   imagelink.TransferEncoding = TransferEncoding.Base64;

   htmlView.LinkedResources.Add(imagelink);
   index++;
}

msg.AlternateViews.Add(htmlView);

try
{
    smtp.Send(msg);
    return Json("true", JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
   logger.Error(ex.Message, ex);

   return Json("false", JsonRequestBehavior.AllowGet);
}
Charles
  • 50,943
  • 13
  • 104
  • 142
NoWar
  • 36,338
  • 80
  • 323
  • 498
  • The html src tag on those `img` elements don't look right. – gwin003 May 09 '13 at 14:23
  • @gwin003 ... well... how it should be in case of embedded images then? Could u suggest pls? – NoWar May 09 '13 at 14:24
  • @MichaelTodd Sorry ... Everything is in code, could you take a look at that pls? – NoWar May 09 '13 at 14:27
  • mm...why you dont set encode type in you htmlview ? like AlternateView htmlView = AlternateView.CreateAlternateViewFromString(bodyBuilder.ToString(), Encoding.UTF8, MediaTypeNames.Text.Html); – danywalls May 09 '13 at 14:33
  • When you debug, at what point does the string in the `img src` change from `cid:` to `http:`? – dnord May 09 '13 at 14:35
  • @Metropolitan Have you seen this example about 1/4 down the page? http://www.codedigest.com/Articles/ASPNET/95_Sending_Email_using_C__and_ASPNet_20.aspx – gwin003 May 09 '13 at 14:36
  • @gwin003 Yes I did. I have used it but in fact I use MemoryStream class. – NoWar May 09 '13 at 14:54

1 Answers1

0

ok guys

I found the awesome solution here

How to embed an Image Stream to MailMessage

Here is correct code:

ImageConverter ic = new ImageConverter();
Byte [] ba = (Byte[]) ic.ConvertTo(page,typeof(Byte[]));
var memoryStream = new MemoryStream(ba);
page.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
Community
  • 1
  • 1
NoWar
  • 36,338
  • 80
  • 323
  • 498