我如何从 c# 发送包含图形的电子邮件,就像 dygraph js 一样?
How can i send email from c# that contain graphics as dygraph js does?
我需要从 C# 代码发送电子邮件报告。此电子邮件包含图片等图形。有什么办法可以在 c# 中实现吗?
dygraph graphic must be included in the email
您可以使用MailMessage class to construct the email, and the SmtpClientclass发送消息。您需要适当地设置 SmtpClient*。
为了嵌入图像,您需要将电子邮件设置为 HTML,并包含嵌入图像的替代视图:
var mail = new MailMessage();
mail.IsBodyHtml = true;
var inline = new LinkedResource(@"C:\path\to\your\image.png");
inline.ContentId = Guid.NewGuid().ToString();
var htmlBody = @"<img src='cid:" + inline.ContentId + @"'/>"; // Include whatever other content in the html body here.
var alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
alternateView.LinkedResources.Add(inline);
mail.AlternateViews.Add(alternateView);
然后设置 to/from/subject 字段并使用 SmtpClient.Send Method(或 SendAsync)发送电子邮件。
*来自 MSDN 文档:
要使用 SmtpClient 构造和发送电子邮件,您必须指定以下信息:
您用来发送电子邮件的 SMTP 主机服务器。请参阅主机和端口属性。
身份验证凭据(如果 SMTP 服务器需要)。请参阅凭据 属性.
发件人的电子邮件地址。请参阅采用 from 参数的 Send 和 SendAsync 方法。另见 MailMessage.From 属性.
收件人的电子邮件地址。请参阅采用收件人参数的 Send 和 SendAsync 方法。另见 MailMessage.To 属性.
留言内容。请参阅采用正文参数的 Send 和 SendAsync 方法。另见 MailMessage.Body 属性.
Dotnet 包含一个 MailMessage class with an IsBodyHtml 属性。您可以使用文本字符串构建电子邮件消息。在文本字符串中,写入 HTML 和指向图形的 <img ..>
标记。
您可以使用 data: URI scheme 将图像的数据流直接插入到图像标签中,标签不引用外部图像。看起来像这样。
img src="data:image/png;base64,iVBORw0KG==" />
您可以按照此处的建议进行操作。 Converting image into data:image/png;base64 for web page disaplay
我需要从 C# 代码发送电子邮件报告。此电子邮件包含图片等图形。有什么办法可以在 c# 中实现吗? dygraph graphic must be included in the email
您可以使用MailMessage class to construct the email, and the SmtpClientclass发送消息。您需要适当地设置 SmtpClient*。
为了嵌入图像,您需要将电子邮件设置为 HTML,并包含嵌入图像的替代视图:
var mail = new MailMessage();
mail.IsBodyHtml = true;
var inline = new LinkedResource(@"C:\path\to\your\image.png");
inline.ContentId = Guid.NewGuid().ToString();
var htmlBody = @"<img src='cid:" + inline.ContentId + @"'/>"; // Include whatever other content in the html body here.
var alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
alternateView.LinkedResources.Add(inline);
mail.AlternateViews.Add(alternateView);
然后设置 to/from/subject 字段并使用 SmtpClient.Send Method(或 SendAsync)发送电子邮件。
*来自 MSDN 文档:
要使用 SmtpClient 构造和发送电子邮件,您必须指定以下信息:
您用来发送电子邮件的 SMTP 主机服务器。请参阅主机和端口属性。
身份验证凭据(如果 SMTP 服务器需要)。请参阅凭据 属性.
发件人的电子邮件地址。请参阅采用 from 参数的 Send 和 SendAsync 方法。另见 MailMessage.From 属性.
收件人的电子邮件地址。请参阅采用收件人参数的 Send 和 SendAsync 方法。另见 MailMessage.To 属性.
留言内容。请参阅采用正文参数的 Send 和 SendAsync 方法。另见 MailMessage.Body 属性.
Dotnet 包含一个 MailMessage class with an IsBodyHtml 属性。您可以使用文本字符串构建电子邮件消息。在文本字符串中,写入 HTML 和指向图形的 <img ..>
标记。
您可以使用 data: URI scheme 将图像的数据流直接插入到图像标签中,标签不引用外部图像。看起来像这样。
img src="data:image/png;base64,iVBORw0KG==" />
您可以按照此处的建议进行操作。 Converting image into data:image/png;base64 for web page disaplay