1

我将 MVCMailer 与 asp.net MVC 3 一起使用。这是一个很棒的库,但我有一个问题。

我看到可以像这样在电子邮件中嵌入图像:

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

因此,看起来“资源”期望文件系统中的图像路径,但是,我的图像在内存流中。

是否可以直接将图像嵌入为 base64,而无需实际将文件写入磁盘然后传递路径?

谢谢你的帮助!

4

1 回答 1

1

Baucause 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 版本的一部分。

于 2012-01-10T09:17:30.410 回答