由於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發佈的一部分。
@作者我在github上發佈了請求:https://github.com/smsohan/MvcMailer/issues/36 – Dragouf 2012-01-16 10:17:58