Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 2.67 KB  |  hits: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Using MVCMailer to embed image from MemoryStream in email
  2. var resources = new Dictionary<string, string>();
  3. resources["image"] = imagePath;
  4. PopulateBody(mailMessage, "WelcomeMessage", resources);
  5.        
  6. List<LinkedResource> GetAll(Dictionary<string, MemoryStream> resources);
  7. LinkedResource Get(string contentId, MemoryStream stream);
  8.        
  9. public virtual List<LinkedResource> GetAll(Dictionary<string, MemoryStream> resources)
  10. {
  11.     var linkedResources = new List<LinkedResource>();
  12.     foreach (var resource in resources)
  13.     {
  14.         linkedResources.Add(Get(resource.Key, resource.Value));
  15.     }
  16.     return linkedResources;
  17. }
  18. public virtual LinkedResource Get(string contentId, MemoryStream stream)
  19. {
  20.     LinkedResource resource = new LinkedResource(stream);
  21.     resource.ContentId = contentId;
  22.     return resource;
  23. }
  24.        
  25. public virtual void PopulateBody(MailMessage mailMessage, string viewName, Dictionary<string, MemoryStream> linkedResources)
  26. {
  27.     PopulateBody(mailMessage, viewName, null, linkedResources);
  28. }
  29. public virtual void PopulateBody(MailMessage mailMessage, string viewName, string masterName = null, Dictionary<string, MemoryStream> linkedResources = null)
  30. {
  31.     if (mailMessage == null)
  32.     {
  33.         throw new ArgumentNullException("mailMessage", "mailMessage cannot be null");
  34.     }
  35.  
  36.     masterName = masterName ?? MasterName;
  37.  
  38.     var linkedResourcesPresent = linkedResources != null && linkedResources.Count > 0;
  39.     var textExists = TextViewExists(viewName, masterName);
  40.  
  41.     //if Text exists, it always goes to the body
  42.     if (textExists)
  43.     {
  44.         PopulateTextBody(mailMessage, viewName, masterName);
  45.     }
  46.  
  47.     // if html exists
  48.     if (HtmlViewExists(viewName, masterName))
  49.     {
  50.         if (textExists || linkedResourcesPresent)
  51.         {
  52.             PopulateHtmlPart(mailMessage, viewName, masterName, linkedResources);
  53.         }
  54.         else
  55.         {
  56.             PopulateHtmlBody(mailMessage, viewName, masterName);
  57.         }
  58.     }
  59. }
  60. public virtual AlternateView PopulateHtmlPart(MailMessage mailMessage, string viewName, string masterName, Dictionary<string, MemoryStream> linkedResources)
  61. {
  62.     var htmlPart = PopulatePart(mailMessage, viewName, "text/html", masterName);
  63.     if (htmlPart != null)
  64.     {
  65.         PopulateLinkedResources(htmlPart, linkedResources);
  66.     }
  67.     return htmlPart;
  68. }
  69. public virtual List<LinkedResource> PopulateLinkedResources(AlternateView mailPart, Dictionary<string, MemoryStream> resources)
  70. {
  71.     if (resources == null || resources.Count == 0)
  72.         return new List<LinkedResource>();
  73.  
  74.     var linkedResources = LinkedResourceProvider.GetAll(resources);
  75.     linkedResources.ForEach(resource => mailPart.LinkedResources.Add(resource));
  76.     return linkedResources;
  77. }