如何使用base64链接在电子邮件附件中发送base64图像



我有base64图像url,我想将此图像作为电子邮件附件发送,但它不起作用。抛出错误PathTooLongException

我的代码:

System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(Base64urlpath);
attachment.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
myMail.Attachments.Add(attachment);

请用system.web.Mail回答我。

谢谢,

您使用的构造函数不接受base64输入,但需要一个文件路径:

public Attachment (string fileName);

参数

fileName字符串

一个字符串,包含用于创建此附件的文件路径。

(引用自文档(。由于编码图像的长度超过260个字符,因此会出现路径过长的异常。

似乎其中一个接受Stream的构造函数可能就是您想要的。

将base64编码的图像转换为流的一种可能性是从中创建一个MemoryStream

var imageBytes = Convert.FromBase64String(Base64urlpath);
using var stream = new MemoryStream(imageBytes);
var attachment = new System.Net.Mail.Attachment(stream, null); // you may want to provide a name here

最新更新