summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShawn A. Luce <shawn@saluce.com>2014-05-15 16:59:50 -0500
committerShawn A. Luce <shawn@saluce.com>2014-05-15 16:59:50 -0500
commitf6a3e659081c641b45f94978fd59df5244d5c4d3 (patch)
tree763d16295c96a1259a8e8a2d8144a963ee81a221
parent64bf5a7ea806cf8974c239e9faab50020d8e8079 (diff)
downloadsendgrid-csharp-f6a3e659081c641b45f94978fd59df5244d5c4d3.zip
sendgrid-csharp-f6a3e659081c641b45f94978fd59df5244d5c4d3.tar.gz
sendgrid-csharp-f6a3e659081c641b45f94978fd59df5244d5c4d3.tar.bz2
Added support for inline images and minor name changes in the MailBuilder
-rw-r--r--SendGrid/SendGridMail/ISendGrid.cs6
-rw-r--r--SendGrid/SendGridMail/MailBuilder.cs50
-rw-r--r--SendGrid/SendGridMail/SendGrid.cs11
-rw-r--r--SendGrid/SendGridMail/Transport/Web.cs4
4 files changed, 46 insertions, 25 deletions
diff --git a/SendGrid/SendGridMail/ISendGrid.cs b/SendGrid/SendGridMail/ISendGrid.cs
index 1e61610..534e644 100644
--- a/SendGrid/SendGridMail/ISendGrid.cs
+++ b/SendGrid/SendGridMail/ISendGrid.cs
@@ -164,6 +164,12 @@ namespace SendGridMail
/// <param name="headers">key substitutionValues pairs</param>
void AddHeaders(IDictionary<String, String> headers);
+ /// <summary>
+ /// Gets the list of embedded images
+ /// </summary>
+ /// <returns></returns>
+ IDictionary<string, string> GetEmbeddedImages();
+
#endregion
#region SMTP API Functions
diff --git a/SendGrid/SendGridMail/MailBuilder.cs b/SendGrid/SendGridMail/MailBuilder.cs
index 1d3d611..2457d2e 100644
--- a/SendGrid/SendGridMail/MailBuilder.cs
+++ b/SendGrid/SendGridMail/MailBuilder.cs
@@ -1,10 +1,10 @@
-using Smtpapi;
-using System;
+using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;
+using Smtpapi;
namespace SendGridMail {
public sealed class MailBuilder {
@@ -124,7 +124,7 @@ namespace SendGridMail {
public MailBuilder Html(AlternateView view) {
return this
.Html(GetAlternateViewAsString(view))
- .WithLinkedResources(view.LinkedResources);
+ .EmbedImages(view.LinkedResources);
}
public MailBuilder Html(string html) {
sendgrid.Html = html;
@@ -140,62 +140,64 @@ namespace SendGridMail {
return this;
}
- public MailBuilder WithAttachment(Stream stream, string name) {
+ public MailBuilder AttachFile(Stream stream, string name) {
sendgrid.AddAttachment(stream, name);
return this;
}
- public MailBuilder WithAttachment(string filePath) {
+ public MailBuilder AttachFile(string filePath) {
sendgrid.AddAttachment(filePath);
return this;
}
- public MailBuilder WithAttachment(Attachment attachment) {
- return this.WithAttachment(attachment.ContentStream, attachment.Name);
+ public MailBuilder AttachFile(Attachment attachment) {
+ return this.AttachFile(attachment.ContentStream, attachment.Name);
}
- public MailBuilder WithAttachments(IEnumerable<Attachment> attachments) {
- foreach (var attachment in attachments) { this.WithAttachment(attachment); }
+ public MailBuilder AttachFiles(IEnumerable<Attachment> attachments) {
+ foreach (var attachment in attachments) { this.AttachFile(attachment); }
return this;
}
- public MailBuilder WithAttachments(AttachmentCollection attachments) {
- return this.WithAttachments(attachments.ToList());
+ public MailBuilder AttachFiles(AttachmentCollection attachments) {
+ return this.AttachFiles(attachments.ToList());
}
- public MailBuilder WithLinkedResource(Stream stream, string name) {
+ public MailBuilder EmbedImage(Stream stream, string name, string cid) {
sendgrid.AddAttachment(stream, name);
+ sendgrid.EmbedImage(name, cid);
return this;
}
- public MailBuilder WithLinkedResource(string filePath) {
+ public MailBuilder EmbedImage(string filePath, string cid) {
sendgrid.AddAttachment(filePath);
+ sendgrid.EmbedImage(new FileInfo(filePath).Name, cid);
return this;
}
- public MailBuilder WithLinkedResource(LinkedResource resource) {
- return this.WithAttachment(resource.ContentStream, resource.ContentId);
+ public MailBuilder EmbedImage(LinkedResource resource) {
+ return this.EmbedImage(resource.ContentStream, resource.ContentId, resource.ContentId);
}
- public MailBuilder WithLinkedResources(IEnumerable<LinkedResource> resources) {
- foreach (var resource in resources) { this.WithLinkedResource(resource); }
+ public MailBuilder EmbedImages(IEnumerable<LinkedResource> resources) {
+ foreach (var resource in resources) { this.EmbedImage(resource); }
return this;
}
- public MailBuilder WithLinkedResources(LinkedResourceCollection resources) {
- return this.WithLinkedResources(resources.ToList());
+ public MailBuilder EmbedImages(LinkedResourceCollection resources) {
+ return this.EmbedImages(resources.ToList());
}
- public MailBuilder WithHeader(string key, string value) {
+ public MailBuilder AddHeader(string key, string value) {
sendgrid.AddHeaders(new Dictionary<string, string> { { key, value } });
return this;
}
- public MailBuilder WithHeaders(IDictionary<string, string> headers) {
+ public MailBuilder AddHeaders(IDictionary<string, string> headers) {
sendgrid.AddHeaders(headers);
return this;
}
- public MailBuilder WithSubstitution(string replacementTag, IEnumerable<string> substitutionValues) {
+ public MailBuilder Substitute(string replacementTag, IEnumerable<string> substitutionValues) {
sendgrid.AddSubstitution(replacementTag, substitutionValues.ToList());
return this;
}
- public MailBuilder WithUniqueArg(string key, string value) {
+ public MailBuilder IncludeUniqueArg(string key, string value) {
sendgrid.AddUniqueArgs(new Dictionary<string, string> { { key, value } });
return this;
}
- public MailBuilder WithUniqueArgs(IDictionary<string, string> identifiers) {
+ public MailBuilder IncludeUniqueArgs(IDictionary<string, string> identifiers) {
sendgrid.AddUniqueArgs(identifiers);
return this;
}
diff --git a/SendGrid/SendGridMail/SendGrid.cs b/SendGrid/SendGridMail/SendGrid.cs
index cc87ffd..54aaa96 100644
--- a/SendGrid/SendGridMail/SendGrid.cs
+++ b/SendGrid/SendGridMail/SendGrid.cs
@@ -163,13 +163,14 @@ namespace SendGridMail
public IHeader Header { get; set; }
public String Html { get; set; }
public String Text { get; set; }
-
+
#endregion
#region Methods for setting data
private List<String> _attachments = new List<String>();
private Dictionary<String, MemoryStream> _streamedAttachments = new Dictionary<string, MemoryStream>();
+ private Dictionary<String, String> _contentImages = new Dictionary<string, string>();
public void AddTo(String address)
{
@@ -251,6 +252,14 @@ namespace SendGridMail
set { _attachments = value.ToList(); }
}
+ public void EmbedImage(String filename, String cid) {
+ _contentImages[filename] = cid;
+ }
+
+ public IDictionary<string, string> GetEmbeddedImages() {
+ return new Dictionary<string, string>(_contentImages);
+ }
+
public void AddSubstitution(String replacementTag, List<String> substitutionValues)
{
//let the system complain if they do something bad, since the function returns null
diff --git a/SendGrid/SendGridMail/Transport/Web.cs b/SendGrid/SendGridMail/Transport/Web.cs
index f053575..a26e9e0 100644
--- a/SendGrid/SendGridMail/Transport/Web.cs
+++ b/SendGrid/SendGridMail/Transport/Web.cs
@@ -210,6 +210,10 @@ namespace SendGridMail
result = result.Concat(message.Cc.ToList().Select(a => new KeyValuePair<String, String>("cc[]", a.Address)))
.ToList();
}
+ if (message.GetEmbeddedImages().Count > 0) {
+ result = result.Concat(message.GetEmbeddedImages().ToList().Select(x => new KeyValuePair<String, String>(string.Format("content[{0}]", x.Key), x.Value)))
+ .ToList();
+ }
return result.Where(r => !String.IsNullOrEmpty(r.Value)).ToList();
}