[英]Embed an image in Outlook 2010 when send email with ASP.NET MVC C#
我有一個問題:是否可以在郵件中嵌入圖像而不用讓用戶單擊Outlook中的自動下載?
我寫了簡單的C#代碼:
string message = "<p><img src='test1.jpg' />";
SendEmail( "to@test.com", "from@test.com", "Subject", message );
在我的Outlook中,該圖像是隱藏的,我必須單擊“自動下載圖像”。
我也嘗試將圖像源編寫為base64代碼
string message = "<p><img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAR8AAAA8CA...' />";
SendEmail( "to@test.com", "from@test.com", "Subject", message );
但這不起作用。
可以做到的-您必須將圖像嵌入為鏈接資源。 以下代碼將img標簽替換為其等效標簽,在服務器端下載圖像並將其作為鏈接資源附加到電子郵件。
public void Email(string htmlBody, string emailSubject) {
var mailMessage = new MailMessage("from@company.com", "to@company.com");
mailMessage.IsBodyHtml = true;
mailMessage.SubjectEncoding = Encoding.UTF8;
mailMessage.BodyEncoding = Encoding.UTF8;
mailMessage.Body = htmlBody;
mailMessage.Subject = emailSubject;
// embedd images
var imageUrls = new List<EmailImage>();
var regexImg = new Regex(@"<img[^>]+>", RegexOptions.IgnoreCase);
var regexSrc = new Regex(@"src=[""](?<url>[^""]+)[""]", RegexOptions.IgnoreCase);
mailMessage.Body = regexImg.Replace(mailMessage.Body, (matchImg) => {
var value = regexSrc.Replace(matchImg.Value, (matchSrc) => {
string url = matchSrc.Groups["url"].Value;
var image = imageUrls.Where(i => string.Compare(i.Url, url, StringComparison.OrdinalIgnoreCase) == 0).FirstOrDefault();
if (image == null) {
image = new EmailImage { Url = url, MailID = Convert.ToString(imageUrls.Count) };
imageUrls.Add(image);
}
return string.Format(@"src=""cid:{0}""", image.MailID);
});
return value;
});
if (imageUrls.Count > 0) {
var htmlView = AlternateView.CreateAlternateViewFromString(mailMessage.Body, null, "text/html");
for (int i = 0; i < imageUrls.Count; i++) {
var request = WebRequest.Create(imageUrls[i].Url);
var response = (HttpWebResponse)request.GetResponse();
var stream = response.GetResponseStream();
var memoryStream = new MemoryStream((int)response.ContentLength);
CopyStream(stream, memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
var imageLink = new LinkedResource(memoryStream, GetContentType(response.ContentType, imageUrls[i].Url));
imageLink.ContentId = imageUrls[i].MailID;
imageLink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
htmlView.LinkedResources.Add(imageLink);
}
mailMessage.AlternateViews.Add(htmlView);
}
SmtpClient.Send(mailMessage);
}
支持方法/類:
private void CopyStream(Stream input, Stream output) {
byte[] buffer = new byte[32768];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0) {
output.Write(buffer, 0, read);
}
}
private string GetContentType(string serverContentType, string imageUrl) {
if (string.IsNullOrEmpty(imageUrl))
throw new ArgumentNullException("imageUrl");
ImageMimeType mimeType;
if (!string.IsNullOrEmpty(serverContentType)) {
serverContentType = serverContentType.ToLowerInvariant();
mimeType = ImageMimeType.MimeTypes.Where(m => m.MimeType == serverContentType).FirstOrDefault();
if (mimeType != null) {
return mimeType.MimeType;
}
}
string extension = Path.GetExtension(imageUrl).ToLowerInvariant();
mimeType = ImageMimeType.MimeTypes.Where(m => m.FileExtension == extension).FirstOrDefault();
if (mimeType != null) {
return mimeType.MimeType;
}
return "application/octet-stream";
}
private class EmailImage {
public string Url { get; set; }
public string MailID { get; set; }
}
private class ImageMimeType {
public string FileExtension { get; private set; }
public string MimeType { get; private set; }
public static List<ImageMimeType> MimeTypes = new List<ImageMimeType> {
new ImageMimeType { FileExtension = ".png", MimeType = "image/png"},
new ImageMimeType { FileExtension = ".jpe", MimeType = "image/jpeg" },
new ImageMimeType { FileExtension = ".jpeg", MimeType = "image/jpeg" },
new ImageMimeType { FileExtension = ".jpg", MimeType = "image/jpeg" },
new ImageMimeType { FileExtension = ".gif", MimeType = "image/gif" },
new ImageMimeType { FileExtension = ".bmp", MimeType = "image/bmp" }
};
}
如果圖像很簡單(即顏色不多,幾何形狀不多),則可以將其轉換為HTML表,然后將其嵌入HTML電子郵件中。 您可以使用此開源工具進行轉換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.