diff options
author | Eric Becking <eric.becking@sendgrid.com> | 2012-01-12 14:28:41 -0700 |
---|---|---|
committer | Eric Becking <eric.becking@sendgrid.com> | 2012-01-12 14:28:41 -0700 |
commit | 5dbd58861409e1edf4c7607438e4fa8a7143d2e8 (patch) | |
tree | dce147cdc4e14b0b22fe39bda33312afc178b3cd | |
parent | 3e3285a98e09cb7563e70849de976dc15f1cfa57 (diff) | |
download | sendgrid-csharp-5dbd58861409e1edf4c7607438e4fa8a7143d2e8.zip sendgrid-csharp-5dbd58861409e1edf4c7607438e4fa8a7143d2e8.tar.gz sendgrid-csharp-5dbd58861409e1edf4c7607438e4fa8a7143d2e8.tar.bz2 |
Web Api interface
-rwxr-xr-x | SendGrid/SendGridMail/ISendGrid.cs | 85 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/SendGrid.cs | 63 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/Web/IWebApi.cs | 45 | ||||
-rwxr-xr-x | SendGrid/Tests/TestSendgridMessageSetup.cs | 27 |
4 files changed, 206 insertions, 14 deletions
diff --git a/SendGrid/SendGridMail/ISendGrid.cs b/SendGrid/SendGridMail/ISendGrid.cs index 8c07947..2fa1553 100755 --- a/SendGrid/SendGridMail/ISendGrid.cs +++ b/SendGrid/SendGridMail/ISendGrid.cs @@ -8,12 +8,20 @@ using System.Text; namespace SendGridMail
{
+ /// <summary>
+ /// Internal object to represent the way in which email may be sent.
+ /// The library supports sending through either SMTP or REST interfaces.
+ /// </summary>
public enum TransportType
{
SMTP,
REST
};
+ /// <summary>
+ /// Represents the basic set of functions that will be called by the user
+ /// includes basic message data manipulation and filter settings
+ /// </summary>
public interface ISendGrid
{
#region Properties
@@ -21,6 +29,7 @@ namespace SendGridMail MailAddress[] To { get; set; }
MailAddress[] Cc { get; }
MailAddress[] Bcc { get; }
+ Attachment[] Attachments { get; set; }
String Subject { get; set; }
IHeader Header { get; set; }
String Html { get; set; }
@@ -33,24 +42,100 @@ namespace SendGridMail #endregion
#region Methods for setting data
+ /// <summary>
+ /// Set the To address. In this case the input is a single string eg. 'you@company.com'
+ /// </summary>
+ /// <param name="address"></param>
void AddTo(String address);
+ /// <summary>
+ /// Set the To address. In this case the expected input is a list of addresses as strings
+ /// </summary>
+ /// <param name="addresses"></param>
void AddTo(IEnumerable<String> addresses);
+ /// <summary>
+ /// Set the To address. In this case the expected input is a list of addresses with paramaters.
+ /// The first parameter is a string for the email address.
+ /// The second paramater is a dictionary containing the key, value pairs for a property's name and its value.
+ /// Currently the only value that you can set with the MailAddress data object is the 'DisplayName' field.
+ /// </summary>
+ /// <param name="addresssInfo"></param>
void AddTo(IDictionary<String, IDictionary<String, String>> addresssInfo);
+ /// <summary>
+ /// Add the CC address. In this case the expected input is a single email address eg "you@company.com"
+ /// </summary>
+ /// <param name="address"></param>
void AddCc(String address);
+ /// <summary>
+ /// Set the CC address. In this case the expected input is a list of addresses as strings
+ /// </summary>
+ /// <param name="addresses"></param>
void AddCc(IEnumerable<String> addresses);
+ /// <summary>
+ /// Set the CC address. In this case the expected input is a list of addresses with paramaters.
+ /// The first parameter is a string for the email address.
+ /// The second paramater is a dictionary containing the key, value pairs for a property's name and its value.
+ /// Currently the only value that you can set with the MailAddress data object is the 'DisplayName' field.
+ /// </summary>
+ /// <param name="addresssInfo"></param>
void AddCc(IDictionary<String, IDictionary<String, String>> addresssInfo);
+ /// <summary>
+ /// Set the Bcc address. Expects a single email as the input eg "you@company.com"
+ /// </summary>
+ /// <param name="address"></param>
void AddBcc(String address);
+ /// <summary>
+ /// Set the Bcc address. Expects a list of emails as an array of strings.
+ /// </summary>
+ /// <param name="addresses"></param>
void AddBcc(IEnumerable<String> addresses);
+ /// <summary>
+ /// Set the Bcc address. In this case the expected input is a list of addresses with paramaters.
+ /// The first parameter is a string for the email address.
+ /// The second paramater is a dictionary containing the key, value pairs for a property's name and its value.
+ /// Currently the only value that you can set with the MailAddress data object is the 'DisplayName' field.
+ /// </summary>
+ /// <param name="addresssInfo"></param>
void AddBcc(IDictionary<String, IDictionary<String, String>> addresssInfo);
+ /// <summary>
+ /// Sets a list of substitution values for the email.
+ /// the 'tag' parameter is the value in the email that you'll replace eg. '-name-'
+ /// the 'value' parameter is a list of values that will be substituted in for the tag.
+ /// In our example above the list would be something like ['eric', 'tyler', 'cj'].
+ /// The 'value' parameter expects a 1:1 mapping from email addresses to values.
+ /// If you are left with more email addresses then values to substitute then the substitution tag will be left blank.
+ /// </summary>
+ /// <param name="tag"></param>
+ /// <param name="value"></param>
void AddSubVal(String tag, params String[] value);
+ /// <summary>
+ /// Add an attachment to the message.
+ /// The 'filePath' paramater expects a fully qualified file path as a string
+ /// </summary>
+ /// <param name="filePath"></param>
void AddAttachment(String filePath);
+ /// <summary>
+ /// Add an attachment to the message
+ /// The 'attachment' paramater expects an argument of the Attachment type.
+ /// </summary>
+ /// <param name="attachment"></param>
void AddAttachment(Attachment attachment);
+ /// <summary>
+ /// Add an attachment to the message
+ /// The 'attachment' paramater expects an argument of the Attachment type.
+ /// The 'type' parameter expects an argument of the ContentType type.
+ /// </summary>
+ /// <param name="attachment"></param>
+ /// <param name="type"></param>
void AddAttachment(Stream attachment, ContentType type);
+ /// <summary>
+ /// GetRecipients returns a list of all the recepients by retrieving the to, cc, and bcc lists.
+ /// </summary>
+ /// <returns></returns>
IEnumerable<String> GetRecipients();
#endregion
diff --git a/SendGrid/SendGridMail/SendGrid.cs b/SendGrid/SendGridMail/SendGrid.cs index 06d1eaf..2f44c38 100755 --- a/SendGrid/SendGridMail/SendGrid.cs +++ b/SendGrid/SendGridMail/SendGrid.cs @@ -235,6 +235,8 @@ namespace SendGridMail }
}
+ public Attachment[] Attachments { get; set; }
+
public void AddSubVal(String tag, params String[] value)
{
//let the system complain if they do something bad, since the function returns null
@@ -244,18 +246,55 @@ namespace SendGridMail public void AddAttachment(String filePath)
{
var data = new Attachment(filePath, MediaTypeNames.Application.Octet);
- message.Attachments.Add(data);
+
+ if (Attachments == null)
+ {
+ Attachments = new Attachment[1];
+ Attachments[0] = data;
+ }
+ else
+ {
+ var i = Attachments.Count();
+ var tmp = new Attachment[i + 1];
+ Attachments.CopyTo(tmp, 0);
+ Attachments = tmp;
+ Attachments[i] = data;
+ }
}
public void AddAttachment(Attachment attachment)
{
- message.Attachments.Add(attachment);
+ if (Attachments == null)
+ {
+ Attachments = new Attachment[1];
+ Attachments[0] = attachment;
+ }
+ else
+ {
+ var i = Attachments.Count();
+ var tmp = new Attachment[i + 1];
+ Attachments.CopyTo(tmp, 0);
+ Attachments = tmp;
+ Attachments[i] = attachment;
+ }
}
public void AddAttachment(Stream attachment, ContentType type)
{
var data = new Attachment(attachment, type);
- message.Attachments.Add(data);
+ if (Attachments == null)
+ {
+ Attachments = new Attachment[1];
+ Attachments[0] = data;
+ }
+ else
+ {
+ var i = Attachments.Count();
+ var tmp = new Attachment[i + 1];
+ Attachments.CopyTo(tmp, 0);
+ Attachments = tmp;
+ Attachments[i] = data;
+ }
}
public IEnumerable<String> GetRecipients()
@@ -267,16 +306,6 @@ namespace SendGridMail var rcpts = tos.Union(ccs.Union(bccs)).Select(address => address.Address);
return rcpts;
}
-
- private string Get(String field)
- {
- throw new NotImplementedException();
- }
-
- private void Set(String field, String value)
- {
- throw new NotImplementedException();
- }
#endregion
#region SMTP API Functions
@@ -433,6 +462,14 @@ namespace SendGridMail if (!String.IsNullOrEmpty(smtpapi))
message.Headers.Add("X-SmtpApi", "{" + smtpapi + "}");
+ if(Attachments != null)
+ {
+ foreach (Attachment attachment in Attachments)
+ {
+ message.Attachments.Add(attachment);
+ }
+ }
+
if (Html != null)
{
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Html, null, "text/html");
diff --git a/SendGrid/SendGridMail/Web/IWebApi.cs b/SendGrid/SendGridMail/Web/IWebApi.cs new file mode 100755 index 0000000..52d4cb2 --- /dev/null +++ b/SendGrid/SendGridMail/Web/IWebApi.cs @@ -0,0 +1,45 @@ +using System;
+using System.Collections.Generic;
+
+namespace SendGridMail.Web
+{
+ interface IWebApi
+ {
+ String user { get; set; }
+ String pass { get; set; }
+
+ String GetBounces(int date, String days, DateTime start_date, DateTime end_date, int limit, int offset, int type, String email);
+ void DeleteBounces(DateTime start_date, DateTime end_date, String type, String email);
+ String GetBlocks(int days, DateTime start_date, DateTime end_date, String email);
+ void DeleteBlocks(String email);
+ String GetEmailParse(String hostname, String url);
+ void SetEmailParse(String hostname, String url);
+ void EditEmailParse(String hostname, String url);
+ void DeleteEmailParse(String hostname);
+ String GetNotificationUrl();
+ void SetNotificationUrl(String url);
+ void DeleteNotificationUrl();
+ String GetFilter();
+ void ActivateFilter(String name);
+ void DeactivateFilter(String name);
+ void SetupFilter(String user, String password, Dictionary<String, String> args);
+ String GetFilterSettings(String name);
+ void GetInvalidEmails(int date, int days, DateTime start_date, DateTime end_date, int limit, int offset, String email);
+ void DeleteInvalidEmails(DateTime start_date, DateTime end_date, String email);
+ String CountInvalidEmails(DateTime start_date, DateTime end_date);
+ String GetProfile();
+ void UpdateProfile(String First_name, String last_name, String address, String city, String state, String country, int zip, int phone, String website);
+ void SetUsername(String username);
+ void SetPassword(String password, String confpass);
+ void SetEmail(String email);
+ String GetSpamReports(int date, int days, DateTime start_date, DateTime end_date, int limit, int offset, String email);
+ void DeleteSpamReports(DateTime start_date, DateTime end_date, String email);
+ String GetStats(int days, DateTime start_date, DateTime end_date);
+ String GetAggregateStats();
+ String GetCategoryStats();
+ String GetCategoryStats(String category, int days, DateTime start_date, DateTime end_date);
+ String GetUnsubscribes(int date, int days, DateTime start_date, DateTime end_date, int limit, int offset, String email);
+ void DeleteUnsubscribes(DateTime start_date, DateTime end_date, String email);
+ void AddUnsubscribes(String email);
+ }
+}
diff --git a/SendGrid/Tests/TestSendgridMessageSetup.cs b/SendGrid/Tests/TestSendgridMessageSetup.cs index 2e43598..c2ad73d 100755 --- a/SendGrid/Tests/TestSendgridMessageSetup.cs +++ b/SendGrid/Tests/TestSendgridMessageSetup.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
+using System.Net.Mime;
using System.Text;
using Moq;
using NUnit.Framework;
@@ -120,7 +121,6 @@ namespace Tests [Test]
public void TestGetRcpts()
{
- //public IEnumerable<String> GetRecipients()
var foo = new Mock<IHeader>();
var sg = new SendGrid(foo.Object);
@@ -135,5 +135,30 @@ namespace Tests Assert.AreEqual("cj@sendgrid.com", rcpts.Skip(2).First(), "getRecipients check Bcc");
Assert.AreEqual("foo@sendgrid.com", rcpts.Skip(3).First(), "getRecipients check Bcc x2");
}
+
+ [Test]
+ public void TestAddAttachment()
+ {
+ var foo = new Mock<IHeader>();
+ var sg = new SendGrid(foo.Object);
+
+ var data = new Attachment("pnunit.framework.dll", MediaTypeNames.Application.Octet);
+ sg.AddAttachment("pnunit.framework.dll");
+ sg.AddAttachment("pnunit.framework.dll");
+ Assert.AreEqual(data.ContentStream, sg.Attachments.First().ContentStream, "Attach via path");
+ Assert.AreEqual(data.ContentStream, sg.Attachments.Skip(1).First().ContentStream, "Attach via path x2");
+
+ sg = new SendGrid(foo.Object);
+ sg.AddAttachment(data);
+ sg.AddAttachment(data);
+ Assert.AreEqual(data.ContentStream, sg.Attachments.First().ContentStream, "Attach via attachment");
+ Assert.AreEqual(data.ContentStream, sg.Attachments.Skip(1).First().ContentStream, "Attach via attachment x2");
+
+ sg = new SendGrid(foo.Object);
+ sg.AddAttachment(data.ContentStream, data.ContentType);
+ sg.AddAttachment(data.ContentStream, data.ContentType);
+ Assert.AreEqual(data.ContentStream, sg.Attachments.First().ContentStream, "Attach via stream");
+ Assert.AreEqual(data.ContentStream, sg.Attachments.Skip(1).First().ContentStream, "Attach via stream x2");
+ }
}
}
|