summaryrefslogtreecommitdiffstats
path: root/SendGrid/SendGridMail
diff options
context:
space:
mode:
authorbrandonmwest <brawest@gmail.com>2013-07-29 12:51:31 -0700
committerbrandonmwest <brawest@gmail.com>2013-07-29 12:51:31 -0700
commit8313c8bab44bef9fde57ac2e22f994427d8d83bd (patch)
tree1c8c5550dab4b2c6fd62ae9698c5ecaaa2d8f5a0 /SendGrid/SendGridMail
parent2a1b360b89ac07fab5e4a377f380bd563dfa5f7e (diff)
parent072cd3ebf52417a275b5180cd1e6841ca4aa86df (diff)
downloadsendgrid-csharp-8313c8bab44bef9fde57ac2e22f994427d8d83bd.zip
sendgrid-csharp-8313c8bab44bef9fde57ac2e22f994427d8d83bd.tar.gz
sendgrid-csharp-8313c8bab44bef9fde57ac2e22f994427d8d83bd.tar.bz2
Merge pull request #33 from sendgrid/bw-net45
Remove RestSharp, refactor to System.Net.Http.HttpClient
Diffstat (limited to 'SendGrid/SendGridMail')
-rw-r--r--SendGrid/SendGridMail/Mail.csproj16
-rwxr-xr-xSendGrid/SendGridMail/Mail.nuspec18
-rw-r--r--[-rwxr-xr-x]SendGrid/SendGridMail/Properties/AssemblyInfo.cs6
-rw-r--r--SendGrid/SendGridMail/SendGrid.1.1.1.nupkgbin14723 -> 0 bytes
-rw-r--r--SendGrid/SendGridMail/Transport/Web.cs366
-rwxr-xr-xSendGrid/SendGridMail/lib/CodeScales.Http.dllbin40960 -> 0 bytes
-rw-r--r--SendGrid/SendGridMail/nuget/Mail.nuspec21
-rw-r--r--SendGrid/SendGridMail/nuget/SendGrid.1.2.0.nupkgbin0 -> 15009 bytes
-rw-r--r--SendGrid/SendGridMail/nuget/lib/SendGridMail.dllbin0 -> 28160 bytes
-rw-r--r--SendGrid/SendGridMail/packages.config2
10 files changed, 226 insertions, 203 deletions
diff --git a/SendGrid/SendGridMail/Mail.csproj b/SendGrid/SendGridMail/Mail.csproj
index 7744b73..e7b0cde 100644
--- a/SendGrid/SendGridMail/Mail.csproj
+++ b/SendGrid/SendGridMail/Mail.csproj
@@ -11,6 +11,8 @@
<RootNamespace>SendGridMail</RootNamespace>
<AssemblyName>SendGridMail</AssemblyName>
<FileAlignment>512</FileAlignment>
+ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>True</DebugSymbols>
@@ -20,6 +22,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
+ <Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@@ -28,15 +31,14 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
+ <Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
- <Reference Include="RestSharp">
- <HintPath>..\packages\RestSharp.104.1\lib\net4\RestSharp.dll</HintPath>
- <Private>False</Private>
- </Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net" />
+ <Reference Include="System.Net.Http" />
+ <Reference Include="System.Net.Http.WebRequest" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel.Web" />
<Reference Include="System.Web" />
@@ -57,6 +59,9 @@
<Compile Include="Transport\SMTP.cs" />
<Compile Include="Utils.cs" />
</ItemGroup>
+ <ItemGroup>
+ <None Include="packages.config" />
+ </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
@@ -65,7 +70,4 @@
<Target Name="AfterBuild">
</Target>
-->
- <ItemGroup>
- <None Include="packages.config" />
- </ItemGroup>
</Project> \ No newline at end of file
diff --git a/SendGrid/SendGridMail/Mail.nuspec b/SendGrid/SendGridMail/Mail.nuspec
deleted file mode 100755
index 0611930..0000000
--- a/SendGrid/SendGridMail/Mail.nuspec
+++ /dev/null
@@ -1,18 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<package >
- <metadata>
- <id>SendGrid</id>
- <version>1.1.1</version>
- <title>SendGrid</title>
- <authors>CJ Buchmann, Tyler Bischel, Eric Becking, Brandon West</authors>
- <owners>Sendgrid</owners>
- <requireLicenseAcceptance>false</requireLicenseAcceptance>
- <description>Basic C# client library and examples for using SendGrid API's to send mail. Github repo located at : https://github.com/sendgrid/sendgrid-csharp</description>
- <releaseNotes>Removed CodeScales, now uses RestSharp</releaseNotes>
- <copyright>Copyright 2013</copyright>
- <tags>SendGrid Email Mail Microsoft Azure</tags>
- <dependencies>
- <dependency id="RestSharp" version="104.1" />
- </dependencies>
- </metadata>
-</package> \ No newline at end of file
diff --git a/SendGrid/SendGridMail/Properties/AssemblyInfo.cs b/SendGrid/SendGridMail/Properties/AssemblyInfo.cs
index 1660c37..32cc1dd 100755..100644
--- a/SendGrid/SendGridMail/Properties/AssemblyInfo.cs
+++ b/SendGrid/SendGridMail/Properties/AssemblyInfo.cs
@@ -10,7 +10,7 @@ using System.Runtime.InteropServices;
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("SendGrid")]
[assembly: AssemblyProduct("SendGridMail")]
-[assembly: AssemblyCopyright("Copyright © 2012")]
+[assembly: AssemblyCopyright("Copyright © 2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
@@ -41,5 +41,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.1.0.0")]
-[assembly: AssemblyFileVersion("1.1.0.0")]
+[assembly: AssemblyVersion("1.2.0")]
+[assembly: AssemblyFileVersion("1.2.0")]
diff --git a/SendGrid/SendGridMail/SendGrid.1.1.1.nupkg b/SendGrid/SendGridMail/SendGrid.1.1.1.nupkg
deleted file mode 100644
index edb228f..0000000
--- a/SendGrid/SendGridMail/SendGrid.1.1.1.nupkg
+++ /dev/null
Binary files differ
diff --git a/SendGrid/SendGridMail/Transport/Web.cs b/SendGrid/SendGridMail/Transport/Web.cs
index 6ff89d5..2e32f47 100644
--- a/SendGrid/SendGridMail/Transport/Web.cs
+++ b/SendGrid/SendGridMail/Transport/Web.cs
@@ -1,175 +1,193 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using System.Net;
-using System.Text;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Net;
+using System.Net.Http.Headers;
using System.Xml;
-using RestSharp;
-
-namespace SendGridMail.Transport
-{
- public class Web : ITransport
- {
- #region Properties
- //TODO: Make this configurable
- public const String BaseURl = "sendgrid.com/api/";
- public const String Endpoint = "mail.send";
- public const String JsonFormat = "json";
- public const String XmlFormat = "xml";
-
- private readonly NetworkCredential _credentials;
- private readonly bool Https;
- #endregion
-
- /// <summary>
- /// Factory method for Web transport of sendgrid messages
- /// </summary>
- /// <param name="credentials">SendGrid credentials for sending mail messages</param>
- /// <param name="https">Use https?</param>
- /// <returns>New instance of the transport mechanism</returns>
- public static Web GetInstance(NetworkCredential credentials, bool https = true)
- {
- return new Web(credentials, https);
- }
-
- /// <summary>
- /// Creates a new Web interface for sending mail. Preference is using the Factory method.
- /// </summary>
- /// <param name="credentials">SendGrid user parameters</param>
- /// <param name="https">Use https?</param>
- internal Web(NetworkCredential credentials, bool https = true)
- {
- Https = https;
- _credentials = credentials;
- }
-
- /// <summary>
- /// Delivers a message over SendGrid's Web interface
- /// </summary>
- /// <param name="message"></param>
- public void Deliver(ISendGrid message)
- {
- var client = Https ? new RestClient("https://" + BaseURl) : new RestClient("http://" + BaseURl);
- var request = new RestRequest(Endpoint + ".xml", Method.POST);
- AttachFormParams(message, request);
- AttachFiles(message, request);
- var response = client.Execute(request);
- CheckForErrors(response);
- }
-
- #region Support Methods
- private void AttachFormParams(ISendGrid message, RestRequest request)
- {
- var formParams = FetchFormParams(message);
- formParams.ForEach(kvp => request.AddParameter(kvp.Key, kvp.Value));
- }
-
- private void AttachFiles (ISendGrid message, RestRequest request)
- {
- //TODO: think the files are being sent in the POST data... but we need to add them as params as well
-
- var files = FetchFileBodies (message);
- files.ForEach (kvp => request.AddFile ("files[" + Path.GetFileName (kvp.Key) + "]", kvp.Value.FullName));
-
- var streamingFiles = FetchStreamingFileBodies(message);
- foreach (KeyValuePair<string, MemoryStream> file in streamingFiles) {
- var name = file.Key;
- var stream = file.Value;
- var writer = new Action<Stream>(
- delegate(Stream s)
- {
- stream.CopyTo(s);
- }
- );
-
- request.AddFile("files[" + name + "]", writer, name);
- }
- }
-
- private void CheckForErrors (IRestResponse response)
- {
- //transport error
- if (response.ResponseStatus == ResponseStatus.Error) {
- throw new Exception(response.ErrorMessage);
- }
-
- //TODO: check for HTTP errors... don't throw exceptions just pass info along?
- var content = response.Content;
- var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
-
- using (var reader = XmlReader.Create(stream))
- {
- while (reader.Read())
- {
- if (reader.IsStartElement())
- {
- switch (reader.Name)
- {
- case "result":
- break;
- case "message": // success
- bool errors = reader.ReadToNextSibling("errors");
- if (errors)
- throw new ProtocolViolationException(content);
- else
- return;
- case "error": // failure
- throw new ProtocolViolationException(content);
- default:
- throw new ArgumentException("Unknown element: " + reader.Name);
- }
- }
- }
- }
- }
-
- internal List<KeyValuePair<String, String>> FetchFormParams(ISendGrid message)
- {
- var result = new List<KeyValuePair<string, string>>()
- {
- new KeyValuePair<String, String>("api_user", _credentials.UserName),
- new KeyValuePair<String, String>("api_key", _credentials.Password),
- new KeyValuePair<String, String>("headers", message.Headers.Count == 0 ? null : Utils.SerializeDictionary(message.Headers)),
- new KeyValuePair<String, String>("replyto", message.ReplyTo.Length == 0 ? null : message.ReplyTo.ToList().First().Address),
- new KeyValuePair<String, String>("from", message.From.Address),
- new KeyValuePair<String, String>("fromname", message.From.DisplayName),
- new KeyValuePair<String, String>("subject", message.Subject),
- new KeyValuePair<String, String>("text", message.Text),
- new KeyValuePair<String, String>("html", message.Html),
- new KeyValuePair<String, String>("x-smtpapi", message.Header.AsJson())
- };
- if(message.To != null)
- {
- result = result.Concat(message.To.ToList().Select(a => new KeyValuePair<String, String>("to[]", a.Address)))
- .Concat(message.To.ToList().Select(a => new KeyValuePair<String, String>("toname[]", a.DisplayName)))
- .ToList();
- }
- if(message.Bcc != null)
- {
- result = result.Concat(message.Bcc.ToList().Select(a => new KeyValuePair<String, String>("bcc[]", a.Address)))
- .ToList();
- }
- if(message.Cc != null)
- {
- result = result.Concat(message.Cc.ToList().Select(a => new KeyValuePair<String, String>("cc[]", a.Address)))
- .ToList();
- }
- return result.Where(r => !String.IsNullOrEmpty(r.Value)).ToList();
- }
-
- internal List<KeyValuePair<String, MemoryStream>> FetchStreamingFileBodies(ISendGrid message)
- {
- return message.StreamedAttachments.Select(kvp => kvp).ToList();
- }
-
- internal List<KeyValuePair<String, FileInfo>> FetchFileBodies(ISendGrid message)
- {
- if(message.Attachments == null)
- return new List<KeyValuePair<string, FileInfo>>();
- return message.Attachments.Select(name => new KeyValuePair<String, FileInfo>(name, new FileInfo(name))).ToList();
- }
-
- #endregion
- }
-}
+using System.Net.Http;
+
+namespace SendGridMail.Transport
+{
+ public class Web : ITransport
+ {
+ #region Properties
+ //TODO: Make this configurable
+ public const String BaseUrl = "sendgrid.com/api/";
+ public const String Endpoint = "/api/mail.send";
+ public const String JsonFormat = "json";
+ public const String XmlFormat = "xml";
+
+ private readonly NetworkCredential _credentials;
+ private readonly bool _https;
+ #endregion
+
+ /// <summary>
+ /// Factory method for Web transport of sendgrid messages
+ /// </summary>
+ /// <param name="credentials">SendGrid credentials for sending mail messages</param>
+ /// <param name="https">Use https?</param>
+ /// <returns>New instance of the transport mechanism</returns>
+ public static Web GetInstance(NetworkCredential credentials, bool https = true)
+ {
+ return new Web(credentials, https);
+ }
+
+ /// <summary>
+ /// Creates a new Web interface for sending mail. Preference is using the Factory method.
+ /// </summary>
+ /// <param name="credentials">SendGrid user parameters</param>
+ /// <param name="https">Use https?</param>
+ internal Web(NetworkCredential credentials, bool https = true)
+ {
+ _https = https;
+ _credentials = credentials;
+ }
+
+ /// <summary>
+ /// Delivers a message over SendGrid's Web interface
+ /// </summary>
+ /// <param name="message"></param>
+ public void Deliver(ISendGrid message)
+ {
+ var client = new HttpClient
+ {
+ BaseAddress = _https ? new Uri("https://" + BaseUrl) : new Uri("http://" + BaseUrl)
+ };
+
+ var content = new MultipartFormDataContent();
+ AttachFormParams(message, content);
+ AttachFiles(message, content);
+ var response = client.PostAsync(Endpoint + ".xml", content).Result;
+ CheckForErrors(response);
+ }
+
+ #region Support Methods
+ private void AttachFormParams(ISendGrid message, MultipartFormDataContent content)
+ {
+ var formParams = FetchFormParams(message);
+ foreach (var keyValuePair in formParams)
+ {
+ content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);
+ }
+ }
+
+ private void AttachFiles(ISendGrid message, MultipartFormDataContent content)
+ {
+ var files = FetchFileBodies (message);
+ foreach (var file in files)
+ {
+ var fs = new FileStream(file.Key, FileMode.Open, FileAccess.Read);
+ var fileContent = new StreamContent(fs);
+
+ fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
+ {
+ Name = "files[" + Path.GetFileName(file.Key) + "]",
+ FileName = Path.GetFileName(file.Key)
+ };
+
+ fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
+ content.Add(fileContent);
+ }
+
+ var streamingFiles = FetchStreamingFileBodies(message);
+ foreach (KeyValuePair<string, MemoryStream> file in streamingFiles) {
+ var name = file.Key;
+ var stream = file.Value;
+ var fileContent = new StreamContent(stream);
+
+ fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
+ {
+ Name = "files[" + Path.GetFileName(file.Key) + "]",
+ FileName = Path.GetFileName(file.Key)
+ };
+
+ fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
+ content.Add(fileContent);
+ }
+ }
+
+ private void CheckForErrors (HttpResponseMessage response)
+ {
+ //transport error
+ if (response.StatusCode != HttpStatusCode.OK) {
+ throw new Exception(response.ReasonPhrase);
+ }
+
+ //TODO: check for HTTP errors... don't throw exceptions just pass info along?
+ var content = response.Content.ReadAsStreamAsync().Result;
+
+ using (var reader = XmlReader.Create(content))
+ {
+ while (reader.Read())
+ {
+ if (reader.IsStartElement())
+ {
+ switch (reader.Name)
+ {
+ case "result":
+ break;
+ case "message": // success
+ bool errors = reader.ReadToNextSibling("errors");
+ if (errors)
+ throw new ProtocolViolationException();
+ return;
+ case "error": // failure
+ throw new ProtocolViolationException();
+ default:
+ throw new ArgumentException("Unknown element: " + reader.Name);
+ }
+ }
+ }
+ }
+ }
+
+ internal List<KeyValuePair<String, String>> FetchFormParams(ISendGrid message)
+ {
+ var result = new List<KeyValuePair<string, string>>
+ {
+ new KeyValuePair<String, String>("api_user", _credentials.UserName),
+ new KeyValuePair<String, String>("api_key", _credentials.Password),
+ new KeyValuePair<String, String>("headers", message.Headers.Count == 0 ? null : Utils.SerializeDictionary(message.Headers)),
+ new KeyValuePair<String, String>("replyto", message.ReplyTo.Length == 0 ? null : message.ReplyTo.ToList().First().Address),
+ new KeyValuePair<String, String>("from", message.From.Address),
+ new KeyValuePair<String, String>("fromname", message.From.DisplayName),
+ new KeyValuePair<String, String>("subject", message.Subject),
+ new KeyValuePair<String, String>("text", message.Text),
+ new KeyValuePair<String, String>("html", message.Html),
+ new KeyValuePair<String, String>("x-smtpapi", message.Header.AsJson())
+ };
+ if(message.To != null)
+ {
+ result = result.Concat(message.To.ToList().Select(a => new KeyValuePair<String, String>("to[]", a.Address)))
+ .Concat(message.To.ToList().Select(a => new KeyValuePair<String, String>("toname[]", a.DisplayName)))
+ .ToList();
+ }
+ if(message.Bcc != null)
+ {
+ result = result.Concat(message.Bcc.ToList().Select(a => new KeyValuePair<String, String>("bcc[]", a.Address)))
+ .ToList();
+ }
+ if(message.Cc != null)
+ {
+ result = result.Concat(message.Cc.ToList().Select(a => new KeyValuePair<String, String>("cc[]", a.Address)))
+ .ToList();
+ }
+ return result.Where(r => !String.IsNullOrEmpty(r.Value)).ToList();
+ }
+
+ internal List<KeyValuePair<String, MemoryStream>> FetchStreamingFileBodies(ISendGrid message)
+ {
+ return message.StreamedAttachments.Select(kvp => kvp).ToList();
+ }
+
+ internal List<KeyValuePair<String, FileInfo>> FetchFileBodies(ISendGrid message)
+ {
+ if(message.Attachments == null)
+ return new List<KeyValuePair<string, FileInfo>>();
+ return message.Attachments.Select(name => new KeyValuePair<String, FileInfo>(name, new FileInfo(name))).ToList();
+ }
+
+ #endregion
+ }
+}
diff --git a/SendGrid/SendGridMail/lib/CodeScales.Http.dll b/SendGrid/SendGridMail/lib/CodeScales.Http.dll
deleted file mode 100755
index 2509b52..0000000
--- a/SendGrid/SendGridMail/lib/CodeScales.Http.dll
+++ /dev/null
Binary files differ
diff --git a/SendGrid/SendGridMail/nuget/Mail.nuspec b/SendGrid/SendGridMail/nuget/Mail.nuspec
new file mode 100644
index 0000000..f318478
--- /dev/null
+++ b/SendGrid/SendGridMail/nuget/Mail.nuspec
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
+ <metadata>
+ <id>SendGrid</id>
+ <version>1.2.0</version>
+ <title>SendGrid</title>
+ <authors>CJ Buchmann,Tyler Bischel,Eric Becking,Brandon West</authors>
+ <owners>SendGrid</owners>
+ <requireLicenseAcceptance>false</requireLicenseAcceptance>
+ <description>Basic C# client library and examples for using SendGrid API's to send mail. Github repo located at : https://github.com/sendgrid/sendgrid-csharp</description>
+ <releaseNotes>Changed HTTP transport to use System.Net.Http</releaseNotes>
+ <copyright>Copyright 2013</copyright>
+ <tags>SendGrid Email Mail Microsoft Azure</tags>
+ <dependencies>
+ <dependency id="Microsoft.Net.Http" version="2.2.13" />
+ </dependencies>
+ </metadata>
+ <files>
+ <file src="lib\SendGridMail.dll" target="lib\SendGridMail.dll" />
+ </files>
+</package> \ No newline at end of file
diff --git a/SendGrid/SendGridMail/nuget/SendGrid.1.2.0.nupkg b/SendGrid/SendGridMail/nuget/SendGrid.1.2.0.nupkg
new file mode 100644
index 0000000..bedbca7
--- /dev/null
+++ b/SendGrid/SendGridMail/nuget/SendGrid.1.2.0.nupkg
Binary files differ
diff --git a/SendGrid/SendGridMail/nuget/lib/SendGridMail.dll b/SendGrid/SendGridMail/nuget/lib/SendGridMail.dll
new file mode 100644
index 0000000..63f02e2
--- /dev/null
+++ b/SendGrid/SendGridMail/nuget/lib/SendGridMail.dll
Binary files differ
diff --git a/SendGrid/SendGridMail/packages.config b/SendGrid/SendGridMail/packages.config
index 6b04fa8..1ef8214 100644
--- a/SendGrid/SendGridMail/packages.config
+++ b/SendGrid/SendGridMail/packages.config
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
- <package id="RestSharp" version="104.1" targetFramework="net40" />
+ <package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net45" />
</packages> \ No newline at end of file