I am trying to add multiple png images as inline attachments to email body. My email body only has the last image. Looks like the memorysteam was overwritten new one. I tried to use AlternateView as this post suggested How to attach multi images in email body in windows application?. But it does not show any image. How to add multiple images attachments? Thanks.
struct Webpage
{
public string Id { get; set; }
public Byte[] Img { get; set; }
public string SiteName { get; set; }
public DateTime CollectTime { get; set; }
}
//
static void SendMultileImgsWEmail(List<Webpage> msg)
{
MailMessage mailMessage = new MailMessage();
SmtpClient client = new SmtpClient(mailserver);
mailMessage.IsBodyHtml = true;
mailMessage.From = new MailAddress(From);
mailMessage.To.Add(new MailAddress(To));
mailMessage.Subject = "xxx";
foreach (Webpage item in msg)
{
byte[] image = item.Img;
Attachment att = new Attachment(new MemoryStream(item.Img), item.SiteName);
att.ContentDisposition.Inline = true;
att.ContentId = item.Id;
att.ContentType.MediaType = "image/png";
mailMessage.Body += "Website Name: " + item.SiteName + Environment.NewLine + Environment.NewLine;
mailMessage.Body += "Screenshot Time: " + item.CollectTime + Environment.NewLine + Environment.NewLine;
mailMessage.Body = String.Format( @"<img src=""cid:{0}"" />", att.ContentId);
mailMessage.Attachments.Add(att);
}
//send message
try
{
client.Send(mailMessage);
}
catch (Exception ex)
{
throw;
}
}