2012-01-09 71 views
1

我使用MVCMailer和asp.net MVC 3. 這是一個很棒的庫,但我遇到了問題。使用MVCMailer從電子郵件中嵌入MemoryStream中的圖像

只見它可以嵌入圖像的電子郵件,像這樣:

var resources = new Dictionary<string, string>(); 
resources["image"] = imagePath; 
PopulateBody(mailMessage, "WelcomeMessage", resources); 

因此它看起來像「資源」從文件系統期待的圖像的路徑,但是,我的形象是MemoryStream的。

是否有可能將圖像作爲base64直接嵌入,而不必實際將文件寫入磁盤然後傳遞路徑?

感謝您的幫助!

回答

1

由於MVCMailer基於System.Net.Mail,因此很容易將LinkedResource添加爲流。

這裏是修復:

在ILinkedResourceProvider.cs補充:

List<LinkedResource> GetAll(Dictionary<string, MemoryStream> resources); 
LinkedResource Get(string contentId, MemoryStream stream); 

在LinkedResourceProvider補充:

public virtual List<LinkedResource> GetAll(Dictionary<string, MemoryStream> resources) 
{ 
    var linkedResources = new List<LinkedResource>(); 
    foreach (var resource in resources) 
    { 
     linkedResources.Add(Get(resource.Key, resource.Value)); 
    } 
    return linkedResources; 
} 
public virtual LinkedResource Get(string contentId, MemoryStream stream) 
{ 
    LinkedResource resource = new LinkedResource(stream); 
    resource.ContentId = contentId; 
    return resource; 
} 

在MailerBase.cs補充:

public virtual void PopulateBody(MailMessage mailMessage, string viewName, Dictionary<string, MemoryStream> linkedResources) 
{ 
    PopulateBody(mailMessage, viewName, null, linkedResources); 
} 
public virtual void PopulateBody(MailMessage mailMessage, string viewName, string masterName = null, Dictionary<string, MemoryStream> linkedResources = null) 
{ 
    if (mailMessage == null) 
    { 
     throw new ArgumentNullException("mailMessage", "mailMessage cannot be null"); 
    } 

    masterName = masterName ?? MasterName; 

    var linkedResourcesPresent = linkedResources != null && linkedResources.Count > 0; 
    var textExists = TextViewExists(viewName, masterName); 

    //if Text exists, it always goes to the body 
    if (textExists) 
    { 
     PopulateTextBody(mailMessage, viewName, masterName); 
    } 

    // if html exists 
    if (HtmlViewExists(viewName, masterName)) 
    { 
     if (textExists || linkedResourcesPresent) 
     { 
      PopulateHtmlPart(mailMessage, viewName, masterName, linkedResources); 
     } 
     else 
     { 
      PopulateHtmlBody(mailMessage, viewName, masterName); 
     } 
    } 
} 
public virtual AlternateView PopulateHtmlPart(MailMessage mailMessage, string viewName, string masterName, Dictionary<string, MemoryStream> linkedResources) 
{ 
    var htmlPart = PopulatePart(mailMessage, viewName, "text/html", masterName); 
    if (htmlPart != null) 
    { 
     PopulateLinkedResources(htmlPart, linkedResources); 
    } 
    return htmlPart; 
} 
public virtual List<LinkedResource> PopulateLinkedResources(AlternateView mailPart, Dictionary<string, MemoryStream> resources) 
{ 
    if (resources == null || resources.Count == 0) 
     return new List<LinkedResource>(); 

    var linkedResources = LinkedResourceProvider.GetAll(resources); 
    linkedResources.ForEach(resource => mailPart.LinkedResources.Add(resource)); 
    return linkedResources; 
} 

希望它成爲下一個MVCMailer發佈的一部分。

+0

@作者我在github上發佈了請求:https://github.com/smsohan/MvcMailer/issues/36 – Dragouf 2012-01-16 10:17:58

相關問題