邮件C#正文中的图像

时间:2015-08-04 07:35:21

标签: c# asp.net image email byte

我想将图像发送到邮件正文中。在这里,我正在加载基于数据库记录的图表。我通过以下代码将图表转换为字节:

string base64 = Request.Form[hfImageData.UniqueID].Split(',')[1];
byte[] bytes = Convert.FromBase64String(base64);

现在,我想将这个bytes数组转换为Image并将图像添加到邮件正文中。

这是我的发送邮件代码:

 MailMessage message = new MailMessage();
 string fromEmail = "domain.com";
 string fromPW = "12345";
 string toEmail = "abcde@gmail.com";     
 message.From = new MailAddress(fromEmail);
 message.To.Add(toEmail);
 message.Subject = "Chart Mail";
 message.Body = "Hi Team";
 message.IsBodyHtml = true;
 message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
 SmtpClient smtpClient = new SmtpClient("domain.com", 1234);
 smtpClient.EnableSsl = false;
 smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
 smtpClient.UseDefaultCredentials = false;
 smtpClient.Credentials = new NetworkCredential(fromEmail, fromPW);
 smtpClient.Send(message.From.ToString(), message.To.ToString(), message.Subject, message.Body);

在上面的代码中,我想将字节转换为图像,并且需要在邮件正文中发送。请建议。

1 个答案:

答案 0 :(得分:1)

无需将图像转换为字节。 你要做的就是:

string attachmentPath = Environment.CurrentDirectory + @"\test.png";
Attachment inline = new Attachment(attachmentPath);
inline.ContentDisposition.Inline = true;
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
inline.ContentId = contentID;
inline.ContentType.MediaType = "image/png";
inline.ContentType.Name = Path.GetFileName(attachmentPath);

message.Attachments.Add(inline);

这将在'内联'中显示消息。布局而不仅仅是附加图像