gpt4 book ai didi

C# 将 System.Drawing.Image 附加到电子邮件

转载 作者:太空狗 更新时间:2023-10-29 18:07:22 25 4
gpt4 key购买 nike

有没有办法将 System.Drawing.Image 附加到电子邮件而不保存它,然后从保存的路径中抓取它。

现在我正在创建图像并保存它。然后我发送电子邮件:

MailMessage mail = new MailMessage();
string _body = "body"

mail.Body = _body;
string _attacmentPath;
if (iP.Contains(":"))
_attacmentPath = (@"path1");//, System.Net.Mime.MediaTypeNames.Application.Octet));
else
_attacmentPath = @"path2");
mail.Attachments.Add(new Attachment(_attacmentPath, System.Net.Mime.MediaTypeNames.Application.Octet));
mail.To.Add(_imageInfo.VendorEmail);
mail.Subject = "Rouses' PO # " + _imageInfo.PONumber.Trim();
mail.From = _imageInfo.VendorNum == 691 ? new MailAddress("email", "") : new MailAddress("email", "");
SmtpClient server = null;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.Normal;
server = new SmtpClient("server");
try
{

server.Send(mail);
}
catch
{

}

有没有办法直接将 System.Drawing.Image 传递给 mail.Attachments.Add()?

最佳答案

您不能将 Image 直接传递给附件,但您可以跳过文件系统,只需将图像保存到 MemoryStream,然后提供 MemoryStream 到附件构造函数:

var stream = new MemoryStream();
image.Save(stream, ImageFormat.Jpeg);
stream.Position = 0;

mail.Attachments.Add(new Attachment(stream, "image/jpg"));

关于C# 将 System.Drawing.Image 附加到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21688300/

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