. excel, csv, pdf, , .. png. png GetEmbeddedImage. .
SmtpClient smtpServer = new SmtpClient(smtpServerName);
smtpServer.Port = 25;
smtpServer.Credentials = new System.Net.NetworkCredential(userName, password);
MailMessage smtpEmail = new MailMessage();
string messageBodyImage = @"<img width=1200 id=""MyContent"" src=""cid:{0}"">";
toAddressList = toAddress.Split(';');
foreach (string toEmail in toAddressList)
smtpEmail.To.Add(toEmail);
smtpEmail.From = new MailAddress(fromAddress);
smtpEmail.Body = messageBody;
smtpEmail.Subject = subject;
foreach (string format in fileExtension)
{
switch (format)
{
case "PNG":
smtpEmail.IsBodyHtml = true;
smtpEmail.AlternateViews.Add(GetEmbeddedImage(reportByteStream, messageBodyImage, format));
break;
case "CSV":
smtpEmail.Attachments.Add(new System.Net.Mail.Attachment(new MemoryStream(myStream[format][0]), "MyReport." + format, "text/csv"));
break;
case "XLS":
smtpEmail.Attachments.Add(new System.Net.Mail.Attachment(new MemoryStream(myStream[format][0]), "MyReport." + format, "application/vnd.ms-excel"));
break;
default:
smtpEmail.Attachments.Add(new System.Net.Mail.Attachment(new MemoryStream(myStream[format][0]), "MyReport." + format, MediaTypeNames.Application.Pdf));
break;
}
}
.
private AlternateView GetEmbeddedImage(Dictionary<string, Byte[][]> streamAttachment, string msgTemplate, string fileFormat)
{
LinkedResource imageFile = null;
AlternateView alternateView = null;
string msgBody = string.Empty;
try
{
List<LinkedResource> imageFiles = new List<LinkedResource>();
for (int page = 0; page < streamAttachment[fileFormat].Length; page++)
{
imageFile = new LinkedResource(new MemoryStream(streamAttachment[fileFormat][page]));
imageFile.ContentId = Guid.NewGuid().ToString();
msgBody = msgBody + "<BR/>" + string.Format(msgTemplate, imageFile.ContentId);
imageFiles.Add(imageFile);
}
alternateView = AlternateView.CreateAlternateViewFromString(msgBody, null, MediaTypeNames.Text.Html);
imageFiles.ForEach(img => alternateView.LinkedResources.Add(img));
}
catch (Exception Ex)
{
}
return alternateView;
}