2012-01-09 14 views
1

私はasp.net MVC 3でMVCMailerを使用しています。 これは素晴らしいライブラリですが問題があります。MVCMailerを使用してMemoryStreamからの画像を電子メールに埋め込む

私はそれはそうのような電子メールに画像を埋め込むことが可能です見た:

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

「資源」は、ファイルシステムからイメージへのパスを期待しているようので、それは見えます、しかし、私のイメージはMemoryStreamをです。

実際にディスクにファイルを書き込むことなく、画像をそのままbase64として埋め込み、パスを渡すことは可能でしょうか?

ありがとうございました!

答えて

1

Baucause MVCMailerはSystem.Net.Mailに基づいており、LinkedResourceをストリームとして簡単に追加できます。ここで

が修正です:ILinkedResourceProvider.csで

は追加:LinkedResourceProviderで

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

追加:MailerBase.csで

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; 
} 

を追加します。

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

@Author私はgithubにこのリクエストを投稿しました:https://github.com/smsohan/MvcMailer/issues/36 – Dragouf

関連する問題