diff options
author | Eric Becking <eric.becking@sendgrid.com> | 2012-01-09 18:36:25 -0700 |
---|---|---|
committer | Eric Becking <eric.becking@sendgrid.com> | 2012-01-09 18:36:25 -0700 |
commit | 27f2cdecfb7c5c95607fb6ab75dfcca1c5ff609b (patch) | |
tree | 642451efcfb46e2cea6e1d3897d1aa4f037dbc76 /SendGrid | |
parent | 8282e3c24573cdabe29069d0018147ab324856b8 (diff) | |
download | sendgrid-csharp-27f2cdecfb7c5c95607fb6ab75dfcca1c5ff609b.zip sendgrid-csharp-27f2cdecfb7c5c95607fb6ab75dfcca1c5ff609b.tar.gz sendgrid-csharp-27f2cdecfb7c5c95607fb6ab75dfcca1c5ff609b.tar.bz2 |
flushed out the Sendgrid.cs file
Diffstat (limited to 'SendGrid')
-rwxr-xr-x | SendGrid/SendGrid.suo | bin | 41984 -> 41984 bytes | |||
-rwxr-xr-x | SendGrid/SendGrid/Header.cs | 2 | ||||
-rwxr-xr-x | SendGrid/SendGrid/IHeader.cs | 2 | ||||
-rwxr-xr-x | SendGrid/SendGrid/ISendGrid.cs | 42 | ||||
-rwxr-xr-x | SendGrid/SendGrid/SendGrid.cs | 274 |
5 files changed, 200 insertions, 120 deletions
diff --git a/SendGrid/SendGrid.suo b/SendGrid/SendGrid.suo Binary files differindex 6a1e6b8..949ddea 100755 --- a/SendGrid/SendGrid.suo +++ b/SendGrid/SendGrid.suo diff --git a/SendGrid/SendGrid/Header.cs b/SendGrid/SendGrid/Header.cs index 42a538f..13f4e90 100755 --- a/SendGrid/SendGrid/Header.cs +++ b/SendGrid/SendGrid/Header.cs @@ -48,7 +48,7 @@ namespace SendGrid throw new NotImplementedException();
}
- public void AsJson()
+ public String AsJson()
{
throw new NotImplementedException();
}
diff --git a/SendGrid/SendGrid/IHeader.cs b/SendGrid/SendGrid/IHeader.cs index 640d985..6756ed5 100755 --- a/SendGrid/SendGrid/IHeader.cs +++ b/SendGrid/SendGrid/IHeader.cs @@ -16,6 +16,6 @@ namespace SendGrid void Disable(String filter);
void AddFilterSetting(String filter, IEnumerable<String> settings, String value);
void AddHeader(MailMessage mime);
- void AsJson();
+ String AsJson();
}
}
diff --git a/SendGrid/SendGrid/ISendGrid.cs b/SendGrid/SendGrid/ISendGrid.cs index 20e7114..9a9d2c2 100755 --- a/SendGrid/SendGrid/ISendGrid.cs +++ b/SendGrid/SendGrid/ISendGrid.cs @@ -1,24 +1,31 @@ using System;
using System.Collections.Generic;
+using System.IO;
using System.Linq;
using System.Net.Mail;
+using System.Net.Mime;
using System.Text;
namespace SendGrid
{
+ public enum TransportType
+ {
+ SMTP,
+ REST
+ };
+
public interface ISendGrid
{
#region Properties
- String From { get; set; }
- String To { get; set; }
- String Cc { get; set; }
- String Bcc { get; set; }
+ MailAddress From { get; set; }
+ MailAddress[] To { get; set; }
+ MailAddress[] Cc { get; }
+ MailAddress[] Bcc { get; }
String Subject { get; set; }
IHeader Header { get; set; }
String Html { get; set; }
String Text { get; set; }
- String Transport { get; set; }
- String Date { get; set; }
+ TransportType Transport { get; set; }
#endregion
#region Interface for ITransport
@@ -28,34 +35,23 @@ namespace SendGrid #region Methods for setting data
void AddTo(String address);
void AddTo(IEnumerable<String> addresses);
- void AddTo(IDictionary<String, String> addresssInfo);
- void AddTo(IEnumerable<IDictionary<String, String>> addressesInfo);
+ void AddTo(IDictionary<String, IDictionary<String, String>> addresssInfo);
void AddCc(String address);
void AddCc(IEnumerable<String> addresses);
- void AddCc(IDictionary<String, String> addresssInfo);
- void AddCc(IEnumerable<IDictionary<String, String>> addressesInfo);
+ void AddCc(IDictionary<String, IDictionary<String, String>> addresssInfo);
void AddBcc(String address);
void AddBcc(IEnumerable<String> addresses);
- void AddBcc(IDictionary<String, String> addresssInfo);
- void AddBcc(IEnumerable<IDictionary<String, String>> addressesInfo);
-
- void AddRcpts(String address);
- void AddRcpts(IEnumerable<String> addresses);
- void AddRcpts(IDictionary<String, String> addresssInfo);
- void AddRcpts(IEnumerable<IDictionary<String, String>> addressesInfo);
+ void AddBcc(IDictionary<String, IDictionary<String, String>> addresssInfo);
void AddSubVal(String tag, String value);
void AddAttachment(String filePath);
void AddAttachment(Attachment attachment);
+ void AddAttachment(Stream attachment, ContentType type);
- String GetMailFrom();
IEnumerable<String> GetRecipients();
-
- String Get(String field);
- void Set(String field, String value);
#endregion
#region SMTP API Functions
@@ -68,7 +64,7 @@ namespace SendGrid void DisableGoogleAnalytics();
void DisableTemplate();
void DisableBcc();
- void DisableBipassListManaement();
+ void DisableBypassListManagement();
void EnableGravatar();
void EnableOpenTracking();
@@ -79,7 +75,7 @@ namespace SendGrid void EnableGoogleAnalytics(String source, String medium, String term, String content = null, String campaign = null);
void EnableTemplate(String html = null);
void EnableBcc(String email = null);
- void EnableBipassListManaement();
+ void EnableBypassListManagement();
#endregion
void Mail();
diff --git a/SendGrid/SendGrid/SendGrid.cs b/SendGrid/SendGrid/SendGrid.cs index 6f6e995..66d11db 100755 --- a/SendGrid/SendGrid/SendGrid.cs +++ b/SendGrid/SendGrid/SendGrid.cs @@ -1,7 +1,10 @@ using System;
using System.Collections.Generic;
+using System.IO;
using System.Linq;
using System.Net.Mail;
+using System.Net.Mime;
+using System.Runtime.InteropServices.ComTypes;
using System.Text;
namespace SendGrid
@@ -30,182 +33,253 @@ namespace SendGrid this.header = header;
}
- public string From
- {
- get { throw new NotImplementedException(); }
- set { throw new NotImplementedException(); }
- }
+ private MailMessage message;
- public string To
- {
- get { throw new NotImplementedException(); }
- set { throw new NotImplementedException(); }
- }
+ // TODO find appropriate types for these
+ const string encoding = "quoted-printable";
+ const string charset = "utf-8";
- public string Cc
- {
- get { throw new NotImplementedException(); }
- set { throw new NotImplementedException(); }
- }
+ /*
+ if (Html != null )
+ {
+ AlternateView htmlView = AlternateView.CreateAlternateViewFromString(html, null, "text/html");
+ message.AlternateViews.Add(htmlView);
+ }
- public string Bcc
- {
- get { throw new NotImplementedException(); }
- set { throw new NotImplementedException(); }
- }
+ if (Text != null )
+ {
+ AlternateView plainView = AlternateView.CreateAlternateViewFromString(Text, null, "text/plain");
+ message.AlternateViews.Add(plainView);
+ }
- public string Subject
- {
- get { throw new NotImplementedException(); }
- set { throw new NotImplementedException(); }
- }
+ message.BodyEncoding = Encoding.GetEncoding(charset);
+ */
- public IHeader Header
+ public SendGrid(MailAddress from, MailAddress[] to, MailAddress[] cc, MailAddress[] bcc,
+ String subject, String html, String text, TransportType transport, IHeader header = null )
{
- get { throw new NotImplementedException(); }
- set { throw new NotImplementedException(); }
- }
+ message = new MailMessage();
+ Header = header;
- public string Html
- {
- get { throw new NotImplementedException(); }
- set { throw new NotImplementedException(); }
- }
+ From = from;
+ To = to;
- public string Text
- {
- get { throw new NotImplementedException(); }
- set { throw new NotImplementedException(); }
- }
+ _subs = new Dictionary<string, string>();
- public string Transport
- {
- get { throw new NotImplementedException(); }
- set { throw new NotImplementedException(); }
- }
+ message.Subject = subject;
+ message.SubjectEncoding = Encoding.GetEncoding(charset);
- public string Date
- {
- get { throw new NotImplementedException(); }
- set { throw new NotImplementedException(); }
+ Text = text;
+ Html = html;
}
- public MailMessage CreateMimeMessage()
+ public MailAddress From
{
- throw new NotImplementedException();
+ get
+ {
+ return message.From;
+ }
+ set
+ {
+ if (value != null) message.From = value;
+ }
}
- public void AddTo(string address)
+ public MailAddress[] To
{
- throw new NotImplementedException();
+ get
+ {
+ return message.To.ToArray();
+ }
+ set
+ {
+ message.To.Clear();
+ foreach (var mailAddress in value)
+ {
+ message.To.Add(mailAddress);
+ }
+ }
}
- public void AddTo(IEnumerable<string> addresses)
+ public MailAddress[] Cc
{
- throw new NotImplementedException();
+ get
+ {
+ return message.CC.ToArray();
+ }
+ set
+ {
+ message.CC.Clear();
+ foreach (var mailAddress in value)
+ {
+ message.CC.Add(mailAddress);
+ }
+ }
}
- public void AddTo(IDictionary<string, string> addresssInfo)
+ public MailAddress[] Bcc
{
- throw new NotImplementedException();
+ get
+ {
+ return message.Bcc.ToArray();
+ }
+ set
+ {
+ message.Bcc.Clear();
+ foreach (var mailAddress in value)
+ {
+ message.Bcc.Add(mailAddress);
+ }
+ }
}
- public void AddTo(IEnumerable<IDictionary<string, string>> addressesInfo)
+ public String Subject
{
- throw new NotImplementedException();
+ get
+ {
+ return message.Subject;
+ }
+ set
+ {
+ if (value != null) message.Subject = value;
+ }
}
- public void AddCc(string address)
+ public IHeader Header
{
- throw new NotImplementedException();
+ get { throw new NotImplementedException(); }
+ set { throw new NotImplementedException(); }
}
- public void AddCc(IEnumerable<string> addresses)
- {
- throw new NotImplementedException();
- }
+ public String Html { get; set; }
+ public String Text { get; set; }
- public void AddCc(IDictionary<string, string> addresssInfo)
- {
- throw new NotImplementedException();
- }
+ public TransportType Transport { get; set; }
- public void AddCc(IEnumerable<IDictionary<string, string>> addressesInfo)
+ public void AddTo(String address)
{
- throw new NotImplementedException();
+ var mailAddress = new MailAddress(address);
+ message.To.Add(mailAddress);
}
- public void AddBcc(string address)
+ public void AddTo(IEnumerable<String> addresses)
{
- throw new NotImplementedException();
+ if (addresses != null)
+ {
+ foreach (var address in addresses)
+ {
+ if (address != null) AddTo(address);
+ }
+ }
}
- public void AddBcc(IEnumerable<string> addresses)
+ public void AddTo(IDictionary<String, IDictionary<String, String>> addresssInfo)
{
- throw new NotImplementedException();
+ foreach (var address in addresssInfo.Keys)
+ {
+ var table = addresssInfo[address];
+ //DisplayName is the only option that this implementation of MailAddress implements.
+ var mailAddress = new MailAddress(address, table.ContainsKey("DisplayName") ? table["DisplayName"] : null);
+ message.To.Add(mailAddress);
+ }
}
- public void AddBcc(IDictionary<string, string> addresssInfo)
+ public void AddCc(String address)
{
- throw new NotImplementedException();
+ var mailAddress = new MailAddress(address);
+ message.CC.Add(mailAddress);
}
- public void AddBcc(IEnumerable<IDictionary<string, string>> addressesInfo)
+ public void AddCc(IEnumerable<String> addresses)
{
- throw new NotImplementedException();
+ if (addresses != null)
+ {
+ foreach (var address in addresses)
+ {
+ if (address != null) AddCc(address);
+ }
+ }
}
- public void AddRcpts(string address)
+ public void AddCc(IDictionary<String, IDictionary<String, String>> addresssInfo)
{
- throw new NotImplementedException();
+ foreach (var address in addresssInfo.Keys)
+ {
+ var table = addresssInfo[address];
+ //DisplayName is the only option that this implementation of MailAddress implements.
+ var mailAddress = new MailAddress(address, table.ContainsKey("DisplayName") ? table["DisplayName"] : null);
+ message.CC.Add(mailAddress);
+ }
}
- public void AddRcpts(IEnumerable<string> addresses)
+ public void AddBcc(String address)
{
- throw new NotImplementedException();
+ var mailAddress = new MailAddress(address);
+ message.Bcc.Add(mailAddress);
}
- public void AddRcpts(IDictionary<string, string> addresssInfo)
+ public void AddBcc(IEnumerable<String> addresses)
{
- throw new NotImplementedException();
+ if (addresses != null)
+ {
+ foreach (var address in addresses)
+ {
+ if (address != null) AddBcc(address);
+ }
+ }
}
- public void AddRcpts(IEnumerable<IDictionary<string, string>> addressesInfo)
+ public void AddBcc(IDictionary<String, IDictionary<String, String>> addresssInfo)
{
- throw new NotImplementedException();
+ foreach (var address in addresssInfo.Keys)
+ {
+ var table = addresssInfo[address];
+ //DisplayName is the only option that this implementation of MailAddress implements.
+ var mailAddress = new MailAddress(address, table.ContainsKey("DisplayName") ? table["DisplayName"] : null);
+ message.Bcc.Add(mailAddress);
+ }
}
+ private Dictionary<string, string> _subs;
+
public void AddSubVal(string tag, string value)
{
- throw new NotImplementedException();
+ //let the system complain if they do something bad, since the function returns null
+ _subs[tag] = value;
}
public void AddAttachment(string filePath)
{
- throw new NotImplementedException();
+ var data = new Attachment(filePath, MediaTypeNames.Application.Octet);
+ message.Attachments.Add(data);
}
public void AddAttachment(Attachment attachment)
{
- throw new NotImplementedException();
+ message.Attachments.Add(attachment);
}
- public string GetMailFrom()
+ public void AddAttachment(Stream attachment, ContentType type)
{
- throw new NotImplementedException();
+ var data = new Attachment(attachment, type);
}
public IEnumerable<string> GetRecipients()
{
- throw new NotImplementedException();
+ List<MailAddress> tos = message.To.ToList();
+ List<MailAddress> ccs = message.CC.ToList();
+ List<MailAddress> bccs = message.Bcc.ToList();
+
+ var rcpts = tos.Union(ccs.Union(bccs)).Select(address => address.Address);
+ return rcpts;
}
- public string Get(string field)
+ private string Get(string field)
{
throw new NotImplementedException();
}
- public void Set(string field, string value)
+ private void Set(string field, string value)
{
throw new NotImplementedException();
}
@@ -305,11 +379,21 @@ namespace SendGrid throw new NotImplementedException();
}
- public void EnableBipassListManaement()
+ public void EnableBypassListManagement()
{
throw new NotImplementedException();
}
+ public MailMessage CreateMimeMessage()
+ {
+ String smtpapi = Header.AsJson();
+
+ if (!String.IsNullOrEmpty(smtpapi))
+ this.message.Headers.Add("X-SmtpApi", "{" + smtpapi + "}");
+
+ return this.message;
+ }
+
public void Mail()
{
throw new NotImplementedException();
|