summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xSendGrid/Example/Program.cs5
-rwxr-xr-xSendGrid/Example/RESTAPI.cs17
-rwxr-xr-xSendGrid/SendGridMail/Header.cs6
-rwxr-xr-xSendGrid/SendGridMail/ISendGrid.cs8
-rwxr-xr-xSendGrid/SendGridMail/JsonUtils.cs23
-rwxr-xr-xSendGrid/SendGridMail/Mail.csproj2
-rwxr-xr-xSendGrid/SendGridMail/SendGrid.cs26
-rwxr-xr-xSendGrid/SendGridMail/Transport/REST.cs13
-rwxr-xr-xSendGrid/SendGridMail/Utils.cs52
-rwxr-xr-xSendGrid/Tests/TestJsonUtils.cs4
10 files changed, 121 insertions, 35 deletions
diff --git a/SendGrid/Example/Program.cs b/SendGrid/Example/Program.cs
index 4d16952..472d5b7 100755
--- a/SendGrid/Example/Program.cs
+++ b/SendGrid/Example/Program.cs
@@ -18,8 +18,7 @@ namespace Example
var from = "cj.buchmann@sendgrid.com";
var to = new List<String>
{
- "cj.buchmann@sendgrid.com",
- "tyler.bischel@sendgrid.com"
+ "cj.buchmann@sendgrid.com"
};
var bcc = new List<string>
@@ -34,7 +33,7 @@ namespace Example
//initialize the SMTPAPI example class
var smtpapi = new SMTPAPI(username, password, from, to);
- var restpapi = new RESTAPI(username, password, from, to, null, cc);
+ var restpapi = new RESTAPI(username, password, from, to, null, null);
//send a simple HTML encoded email.
//smtpapi.SimpleHTMLEmail();
diff --git a/SendGrid/Example/RESTAPI.cs b/SendGrid/Example/RESTAPI.cs
index ca4e507..a80c536 100755
--- a/SendGrid/Example/RESTAPI.cs
+++ b/SendGrid/Example/RESTAPI.cs
@@ -1,4 +1,5 @@
using System;
+using System.Linq;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
@@ -57,7 +58,19 @@ namespace Example
message.AddCc(cc);
}
}
-
+
+ var headers = new Dictionary<String, String>
+ {
+ {"key1", "value1"},
+ {"key2", "value2"},
+ {"icanhazcheeze", "kthnxbye"}
+ };
+
+ message.AddHeaders(headers);
+
+ //var replyTo = new List<MailAddress> { new MailAddress("tyler.bischel@sendgrid.com") };
+
+ //message.ReplyTo = replyTo.ToArray();
//set the sender
message.From = new MailAddress(_from);
@@ -68,6 +81,8 @@ namespace Example
//set the message subject
message.Subject = "Hello World Simple Test";
+ //Utils.PrepareAttachments();
+
//create an instance of the SMTP transport mechanism
var restInstance = new REST(new NetworkCredential(_username, _password));
diff --git a/SendGrid/SendGridMail/Header.cs b/SendGrid/SendGridMail/Header.cs
index 860ebed..f1328f5 100755
--- a/SendGrid/SendGridMail/Header.cs
+++ b/SendGrid/SendGridMail/Header.cs
@@ -155,11 +155,11 @@ namespace SendGridMail
public String ToJson()
{
if (_branches.Count > 0)
- return "{" + String.Join(",", _branches.Keys.Select(k => JsonUtils.Serialize(k) + " : " + _branches[k].ToJson())) + "}";
+ return "{" + String.Join(",", _branches.Keys.Select(k => Utils.Serialize(k) + " : " + _branches[k].ToJson())) + "}";
if (_leaf != null)
- return JsonUtils.Serialize(_leaf);
+ return Utils.Serialize(_leaf);
if (_array != null)
- return "[" + String.Join(", ", _array.Select(i => JsonUtils.Serialize(i))) + "]";
+ return "[" + String.Join(", ", _array.Select(i => Utils.Serialize(i))) + "]";
return "{}";
}
diff --git a/SendGrid/SendGridMail/ISendGrid.cs b/SendGrid/SendGridMail/ISendGrid.cs
index 2fa1553..c73cd99 100755
--- a/SendGrid/SendGridMail/ISendGrid.cs
+++ b/SendGrid/SendGridMail/ISendGrid.cs
@@ -29,8 +29,10 @@ namespace SendGridMail
MailAddress[] To { get; set; }
MailAddress[] Cc { get; }
MailAddress[] Bcc { get; }
+ MailAddress[] ReplyTo { get; set; }
Attachment[] Attachments { get; set; }
String Subject { get; set; }
+ Dictionary<String, String> Headers { get; set; }
IHeader Header { get; set; }
String Html { get; set; }
String Text { get; set; }
@@ -137,6 +139,12 @@ namespace SendGridMail
/// </summary>
/// <returns></returns>
IEnumerable<String> GetRecipients();
+
+ /// <summary>
+ /// Add custom headers to the message
+ /// </summary>
+ /// <param name="headers">key value pairs</param>
+ void AddHeaders(IDictionary<String, String> headers);
#endregion
#region SMTP API Functions
diff --git a/SendGrid/SendGridMail/JsonUtils.cs b/SendGrid/SendGridMail/JsonUtils.cs
deleted file mode 100755
index d92b9dd..0000000
--- a/SendGrid/SendGridMail/JsonUtils.cs
+++ /dev/null
@@ -1,23 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using System.Runtime.Serialization.Json;
-using System.Text;
-
-namespace SendGridMail
-{
- public class JsonUtils
- {
- public static string Serialize<T>(T obj)
- {
- var serializer = new DataContractJsonSerializer(obj.GetType());
- using (var stream = new MemoryStream())
- {
- serializer.WriteObject(stream, obj);
- var jsonData = Encoding.UTF8.GetString(stream.ToArray(), 0, (int)stream.Length);
- return jsonData;
- }
- }
- }
-}
diff --git a/SendGrid/SendGridMail/Mail.csproj b/SendGrid/SendGridMail/Mail.csproj
index 9272780..045e142 100755
--- a/SendGrid/SendGridMail/Mail.csproj
+++ b/SendGrid/SendGridMail/Mail.csproj
@@ -52,7 +52,7 @@
<Compile Include="Transport\ITransport.cs" />
<Compile Include="Transport\REST.cs" />
<Compile Include="Transport\SMTP.cs" />
- <Compile Include="JsonUtils.cs" />
+ <Compile Include="Utils.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
diff --git a/SendGrid/SendGridMail/SendGrid.cs b/SendGrid/SendGridMail/SendGrid.cs
index b473d5d..b1a38aa 100755
--- a/SendGrid/SendGridMail/SendGrid.cs
+++ b/SendGrid/SendGridMail/SendGrid.cs
@@ -49,6 +49,7 @@ namespace SendGridMail
{
message = new MailMessage();
Header = header;
+ Headers = new Dictionary<string, string>();
From = from;
To = to;
@@ -65,6 +66,7 @@ namespace SendGridMail
{
message = new MailMessage();
Header = header;
+ Headers = new Dictionary<string, string>();
//initialize the filters, for use within the library
this.InitializeFilters();
@@ -83,6 +85,22 @@ namespace SendGridMail
}
}
+ public MailAddress[] ReplyTo
+ {
+ get
+ {
+ return message.ReplyToList.ToArray();
+ }
+ set
+ {
+ message.ReplyToList.Clear();
+ foreach (var replyTo in value)
+ {
+ message.ReplyToList.Add(replyTo);
+ }
+ }
+ }
+
public MailAddress[] To
{
get
@@ -143,6 +161,7 @@ namespace SendGridMail
}
}
+ public Dictionary<String, String> Headers { get; set; }
public IHeader Header { get; set; }
public String Html { get; set; }
public String Text { get; set; }
@@ -273,6 +292,11 @@ namespace SendGridMail
var rcpts = tos.Union(ccs.Union(bccs)).Select(address => address.Address);
return rcpts;
}
+
+ public void AddHeaders(IDictionary<string, string> headers)
+ {
+ headers.Keys.ToList().ForEach(key => Headers[key] = headers[key]);
+ }
#endregion
#region SMTP API Functions
@@ -430,6 +454,8 @@ namespace SendGridMail
if (!String.IsNullOrEmpty(smtpapi))
message.Headers.Add("X-Smtpapi", smtpapi);
+ Headers.Keys.ToList().ForEach(k => message.Headers.Add(k, Headers[k]));
+
if(Attachments != null)
{
foreach (Attachment attachment in Attachments)
diff --git a/SendGrid/SendGridMail/Transport/REST.cs b/SendGrid/SendGridMail/Transport/REST.cs
index 85e3f5b..3a77c3f 100755
--- a/SendGrid/SendGridMail/Transport/REST.cs
+++ b/SendGrid/SendGridMail/Transport/REST.cs
@@ -46,8 +46,17 @@ namespace SendGridMail.Transport
message.To.ToList().ForEach(a => addQueryParam("to[]", a.Address));
message.Bcc.ToList().ForEach(a => addQueryParam("bcc[]", a.Address));
message.Cc.ToList().ForEach(a => addQueryParam("cc[]", a.Address));
- //_queryParameters["toname[]"] = String.Join(",", message.To.Select(a => a.DisplayName)); // message.To.First().ToString();
+
+ message.To.ToList().ForEach(a => addQueryParam("toname[]", a.DisplayName));
+
+ addQueryParam("headers", Utils.SerializeDictionary(message.Headers));
+
+ message.ReplyTo.ToList().ForEach(a => addQueryParam("replyto", a.Address));
+ //addQueryParam("", message.From.Address);
+
addQueryParam("from", message.From.Address);
+ addQueryParam("fromname", message.From.DisplayName);
+
addQueryParam("subject", message.Subject);
addQueryParam("text", message.Text);
addQueryParam("html", message.Html);
@@ -98,7 +107,7 @@ namespace SendGridMail.Transport
private string FetchQueryString()
{
- return String.Join("&", _query.Select(kvp => kvp.Key + "=" + kvp.Value));
+ return String.Join("&", _query.Where(kvp => !String.IsNullOrEmpty(kvp.Value)).Select(kvp => kvp.Key + "=" + kvp.Value));
}
}
}
diff --git a/SendGrid/SendGridMail/Utils.cs b/SendGrid/SendGridMail/Utils.cs
new file mode 100755
index 0000000..cb6413c
--- /dev/null
+++ b/SendGrid/SendGridMail/Utils.cs
@@ -0,0 +1,52 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.IO.Pipes;
+using System.Linq;
+using System.Net;
+using System.Net.Mail;
+using System.Runtime.Serialization.Json;
+using System.Text;
+
+namespace SendGridMail
+{
+ public class Utils
+ {
+ public static string Serialize<T>(T obj)
+ {
+ var serializer = new DataContractJsonSerializer(obj.GetType());
+ using (var stream = new MemoryStream())
+ {
+ serializer.WriteObject(stream, obj);
+ var jsonData = Encoding.UTF8.GetString(stream.ToArray(), 0, (int)stream.Length);
+ return jsonData;
+ }
+ }
+
+ public static string SerializeDictionary(IDictionary<String, String> dic)
+ {
+ return "{"+String.Join(",",dic.Select(kvp => Serialize(kvp.Key) + ":" + Serialize(kvp.Value)))+"}";
+ }
+
+ public static Dictionary<String, Stream> PrepareAttachments()
+ {
+ var attach = new Attachment("D:/att_proj/21.jpg");
+ Console.WriteLine("preparing message attachment");
+ var sr = new StreamReader(attach.ContentStream);
+
+ Console.WriteLine(sr.ReadToEnd());
+
+ Console.WriteLine(":D");
+ var request = (HttpWebRequest)WebRequest.Create("");
+
+
+ //attach.ContentStream.CopyTo(request.GetRequestStream());
+
+ Console.WriteLine("attachment: ");
+
+ Console.WriteLine("DONE");
+
+ return new Dictionary<string, Stream>();
+ }
+ }
+}
diff --git a/SendGrid/Tests/TestJsonUtils.cs b/SendGrid/Tests/TestJsonUtils.cs
index b8f4aae..fee990d 100755
--- a/SendGrid/Tests/TestJsonUtils.cs
+++ b/SendGrid/Tests/TestJsonUtils.cs
@@ -10,8 +10,8 @@ namespace Tests
[Test]
public void TestSerialize()
{
- Assert.AreEqual("1", JsonUtils.Serialize(1));
- Assert.AreEqual("\"\\\"foo\\\"\"", JsonUtils.Serialize("\"foo\""));
+ Assert.AreEqual("1", Utils.Serialize(1));
+ Assert.AreEqual("\"\\\"foo\\\"\"", Utils.Serialize("\"foo\""));
}
}
}