gpt4 book ai didi

c# - 如何显示图像并将其添加为电子邮件的附件

转载 作者:太空狗 更新时间:2023-10-29 23:16:30 24 4
gpt4 key购买 nike

我正在创建一个带有 html 正文的 System.Net.Mail 消息。我将客户签名的 jpeg 图像(来自字节数组)附加到电子邮件并发送到服务中心。服务中心希望在不打开附件的情况下看到签名,所以我更改了 html 并再次将图像添加为 LinkedResource。问题是,现在附件比以前大了,试图查看附件时会显示错误。如果我保存附件并在十六进制编辑器中打开文件,整个文件是空的。我可以评论添加链接资源的代码,附件再次起作用。如何包含内联图像和附件?

这是我嵌入图像的标签:<p><img src=cid:CustomerSignature /></p>

这是我的代码:

System.IO.MemoryStream ms = new System.IO.MemoryStream(insuranceClaim.Signature, 0, insuranceClaim.Signature.Length);
ms.Position = 0;
System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
contentType.MediaType = System.Net.Mime.MediaTypeNames.Image.Jpeg;
contentType.Name = "signature.jpg";
System.Net.Mail.Attachment imageAttachment = new System.Net.Mail.Attachment(ms, contentType);
System.Net.Mime.ContentDisposition disposition = imageAttachment.ContentDisposition;
mailMessage.Attachments.Add(imageAttachment);
System.Net.Mail.LinkedResource signature = new System.Net.Mail.LinkedResource(ms, new System.Net.Mime.ContentType("image/jpeg"));
signature.ContentId = "CustomerSignature";
System.Net.Mail.AlternateView aView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(mailMessage.Body, new System.Net.Mime.ContentType("text/html"));
aView.LinkedResources.Add(signature);
mailMessage.AlternateViews.Add(aView);

最佳答案

我不喜欢自己回答问题,但如果其他人犯了和我一样的错误,这里是解决方案。我用图像数据创建了一个新的 MemoryStream,并尝试在附件和 LinkedResource 之间共享流。一旦我创建了 2 个 MemoryStreams,它就起作用了。

System.IO.MemoryStream ms = new System.IO.MemoryStream(insuranceClaim.Signature, 0, insuranceClaim.Signature.Length);
System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
contentType.MediaType = System.Net.Mime.MediaTypeNames.Image.Jpeg;
contentType.Name = "signature.jpg";
System.Net.Mail.Attachment imageAttachment = new System.Net.Mail.Attachment(ms, contentType);
mailMessage.Attachments.Add(imageAttachment);

System.IO.MemoryStream embeddedMs = new System.IO.MemoryStream(insuranceClaim.Signature, 0, insuranceClaim.Signature.Length);
System.Net.Mail.LinkedResource signature = new System.Net.Mail.LinkedResource(embeddedMs, new System.Net.Mime.ContentType("image/jpeg"));
signature.ContentId = "CustomerSignature";
System.Net.Mail.AlternateView aView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(mailMessage.Body, new System.Net.Mime.ContentType("text/html"));
aView.LinkedResources.Add(signature);
mailMessage.AlternateViews.Add(aView);

关于c# - 如何显示图像并将其添加为电子邮件的附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12787852/

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