C# ASP.NET邮件附件内联
我有一行创建附件的代码C# ASP.NET邮件附件内联,c#,asp.net,C#,Asp.net,我有一行创建附件的代码 email.Attachments.Add(new Attachment(new MemoryStream(filebytes), "QRCode.png")); 现在,我正在尝试将ContentDisposition.Inline属性应用于它……当以我的方式创建附件时,我将如何做到这一点?我想您需要一行解决方案,这是一种方法: email.Attachments.Add((new Func<Stream, Attachment>(stream =>
email.Attachments.Add(new Attachment(new MemoryStream(filebytes), "QRCode.png"));
现在,我正在尝试将ContentDisposition.Inline属性应用于它……当以我的方式创建附件时,我将如何做到这一点?我想您需要一行解决方案,这是一种方法:
email.Attachments.Add((new Func<Stream, Attachment>(stream => { var attachment = new Attachment(stream, "QRCode.png"); attachment.ContentDisposition.Inline = true; return attachment; }).Invoke(new MemoryStream(fileBytes))));
email.Attachments.Add((new Func(stream=>{var attachment=new attachment(stream,“QRCode.png”);attachment.ContentDisposition.Inline=true;return attachment;}).Invoke(new MemoryStream(fileBytes));
然而,我强烈反对这种方法,因为它非常难阅读。@darjan bogdan的解决方案是可行的,如果出于某种原因,它绝对需要是一行 但是,如果目标是只使用一行逻辑语句来添加附件,那么我只需要使用一个helper方法,即la:
Attachment data = BuildAttachment(new MemoryStream(filebytes), "QRCode.png"));
...
private Attachment BuildAttachment(MemoryStream stream, string name) {
Attachment attachment = new Attachment(stream, name);
attachment.ContentDisposition.Inline = true;
return attachment;
}
这保留了我假设的for循环中的“单行”。您需要一行解决方案吗?我同意您的观点,代码应该是可读的。