gpt4 book ai didi

c# - 使用 C# 将图像添加到电子邮件正文

转载 作者:行者123 更新时间:2023-12-04 01:45:07 26 4
gpt4 key购买 nike

我想使用 C# 将图片框中的图像添加到我的电子邮件正文中。这就是我所做的。任何人都可以帮助我吗?

var fromAddress = new MailAddress("aaa@gmail.com", "aaa");
var toAddress = new MailAddress("bbb@gmail.com", "bbb");
const string fromPassword = "mypassword";
const string subject = "Tes Program";
const string body = "Bersama ini kami kirimkan QR Code sebagai sarana validasi pengiriman rekening koran Anda. Harap simpan dan tunjukkan QR Code ini saat kurir kami datang untuk mengantar rekening koran. Atas perhatiannya kami sampaikan terima kasih. Salam";

var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};

using (var message = new MailMessage(fromAddress, toAddress)
{

Subject = subject,
Body = body


}

)
{
smtp.Send(message);
}

我有picturebox1 并且想将picturebox1 中显示的图像添加到我的电子邮件正文中

最佳答案

您需要将其添加为电子邮件的嵌入式资源并为其创建 HTML View ,以下是帮助您入门的示例:

    private static void AddImageToEmail(MailMessage mail, Image image)
{
var imageStream = GetImageStream(image);

var imageResource = new LinkedResource(imageStream, "image/png") { ContentId = "added-image-id" };
var alternateView = AlternateView.CreateAlternateViewFromString(mail.Body, mail.BodyEncoding, MediaTypeNames.Text.Html);

alternateView.LinkedResources.Add(imageResource);
mail.AlternateViews.Add(alternateView);
}

private static Stream GetImageStream(Image image)
{
// Conver the image to a memory stream and return.
var imageConverter = new ImageConverter();
var imgaBytes = (byte[])imageConverter.ConvertTo(image, typeof(byte[]));
var memoryStream = new MemoryStream(imgaBytes);

return memoryStream;
}

关于c# - 使用 C# 将图像添加到电子邮件正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44170026/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com