使用base64字符串的C#LinkedResource

时间:2014-03-26 04:17:15

标签: c# email base64

我怎么能把这个==> url('data:image/jpeg;base64,/9j/4AAQSkZJRgABAgEASABIAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB')进入new System.Net.Mail.LinkedResource()

使用带有base64字符串的背景css样式发送C#形式的邮件,而不是文件URL。

1 个答案:

答案 0 :(得分:27)

我自己想知道这件事并且到了这个帖子。我解决了它,并认为我会分享我的解决方案。

var imageData = Convert.FromBase64String("/9j/4AAQSkZJRgABAgEASABIAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB");

var contentId = Guid.NewGuid().ToString();
var linkedResource = new LinkedResource(new MemoryStream(imageData), "image/jpeg");
linkedResource.ContentId = contentId;
linkedResource.TransferEncoding = TransferEncoding.Base64;

var body = string.Format("<img src=\"cid:{0}\" />", contentId);
var htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
htmlView.LinkedResources.Add(linkedResource);