diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | CHANGELOG.md | 15 | ||||
-rw-r--r-- | README.md | 8 | ||||
-rw-r--r-- | SendGrid/Example/Program.cs | 7 | ||||
-rw-r--r-- | SendGrid/Example/packages.config | 5 | ||||
-rw-r--r-- | SendGrid/SendGridMail/ISendGrid.cs | 2 | ||||
-rw-r--r-- | SendGrid/SendGridMail/Mail.csproj | 5 | ||||
-rw-r--r-- | SendGrid/SendGridMail/Properties/AssemblyInfo.cs | 4 | ||||
-rw-r--r-- | SendGrid/SendGridMail/SendGrid.cs | 38 | ||||
-rw-r--r-- | SendGrid/SendGridMail/StreamedFileBody.cs | 2 | ||||
-rw-r--r-- | SendGrid/SendGridMail/Transport/Web.cs | 31 | ||||
-rw-r--r-- | SendGrid/SendGridMail/packages.config | 1 | ||||
-rw-r--r-- | SendGrid/Tests/TestSendgrid.cs | 63 | ||||
-rw-r--r-- | SendGrid/Tests/Tests.csproj | 5 | ||||
-rw-r--r-- | SendGrid/Tests/packages.config | 1 |
15 files changed, 159 insertions, 30 deletions
@@ -16,3 +16,5 @@ SendGrid/test-results/ .DS_store SendGrid/SendGrid.userprefs SendGrid/packages/ +SendGrid/TestResult.xml +SendGrid/SendGrid.sln.VisualState.xml diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..335b129 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,15 @@ +# Change Log +All notable changes to this project will be documented in this file. + +## [5.1.0] - 2015-1-26 +### Added +- This changelog. +- `Web` transport constructor that accepts a `TimeSpan` to specify HTTP timeout +- Null values in header will now result in a `ArgumentNullException` + +### Changed +- Updated to SendGrid.SmtpApi 1.2.0, which means Unicode in header values will work properly. + +### Fixed +- Removed redundant status code check that was throwing unhelpful errors +- Unicode in header values will now work properly @@ -1,14 +1,10 @@ [](https://travis-ci.org/sendgrid/sendgrid-csharp) -##Breaking API changes in 3.0.0! - -Use `new SendGridMessage()` instead of `SendGrid.GetInstance()` - -and `new Web()` instead of `Web.GetInstance()` +See the [changelog](https://github.com/sendgrid/sendgrid-csharp/blob/master/CHANGELOG.md) for updates. #Requirements -As of 4.0.0, this library requires .NET 4.5 and above. If you need .NET 4.0 support, `Install-Package sendgri -V 3.0.2` +As of 4.0.0, this library requires .NET 4.5 and above. [Fork with .NET 4.0 support](https://www.nuget.org/packages/SendGrid.Net40/) #Installation diff --git a/SendGrid/Example/Program.cs b/SendGrid/Example/Program.cs index fa97707..113e3f5 100644 --- a/SendGrid/Example/Program.cs +++ b/SendGrid/Example/Program.cs @@ -1,4 +1,5 @@ using System;
+using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using SendGrid;
@@ -15,7 +16,11 @@ namespace Example myMessage.AddTo("anna@example.com");
myMessage.From = new MailAddress("john@example.com", "John Smith");
myMessage.Subject = "Testing the SendGrid Library";
- myMessage.Text = "Hello World!";
+ myMessage.Text = "Hello World! %tag%";
+
+ var subs = new List<String> { "私は%type%ラーメンが大好き" };
+ myMessage.AddSubstitution("%tag%",subs);
+ myMessage.AddSection("%type%", "とんこつ");
SendAsync(myMessage);
diff --git a/SendGrid/Example/packages.config b/SendGrid/Example/packages.config new file mode 100644 index 0000000..74a7fa5 --- /dev/null +++ b/SendGrid/Example/packages.config @@ -0,0 +1,5 @@ +<?xml version="1.0" encoding="utf-8"?>
+<packages>
+ <package id="Sendgrid" version="4.0.0" targetFramework="net45" />
+ <package id="SendGrid.SmtpApi" version="1.2.1" targetFramework="net45" />
+</packages>
\ No newline at end of file diff --git a/SendGrid/SendGridMail/ISendGrid.cs b/SendGrid/SendGridMail/ISendGrid.cs index 1d5ea8f..b4fcdc9 100644 --- a/SendGrid/SendGridMail/ISendGrid.cs +++ b/SendGrid/SendGridMail/ISendGrid.cs @@ -108,6 +108,8 @@ namespace SendGrid /// <param name="name">Name of file to be attached</param>
void AddAttachment(Stream stream, String name);
+ void EmbedStreamImage(Stream stream, String name);
+
/// <summary>
/// GetRecipients returns a list of all the recepients by retrieving the to, cc, and bcc lists.
/// </summary>
diff --git a/SendGrid/SendGridMail/Mail.csproj b/SendGrid/SendGridMail/Mail.csproj index 9dcb4ad..b1f8f84 100644 --- a/SendGrid/SendGridMail/Mail.csproj +++ b/SendGrid/SendGridMail/Mail.csproj @@ -88,8 +88,9 @@ </DebugType>
</PropertyGroup>
<ItemGroup>
- <Reference Include="SendGrid.SmtpApi">
- <HintPath>..\packages\SendGrid.SmtpApi.1.1.3\lib\net40\SendGrid.SmtpApi.dll</HintPath>
+ <Reference Include="SendGrid.SmtpApi, Version=1.2.1.0, Culture=neutral, PublicKeyToken=2ae73662c35d80e4, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\packages\SendGrid.SmtpApi.1.2.1\lib\net40\SendGrid.SmtpApi.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
diff --git a/SendGrid/SendGridMail/Properties/AssemblyInfo.cs b/SendGrid/SendGridMail/Properties/AssemblyInfo.cs index c2d2a11..4d2311c 100644 --- a/SendGrid/SendGridMail/Properties/AssemblyInfo.cs +++ b/SendGrid/SendGridMail/Properties/AssemblyInfo.cs @@ -48,5 +48,5 @@ using System.Runtime.InteropServices; // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("4.1.0")]
-[assembly: AssemblyFileVersion("4.1.0")]
\ No newline at end of file +[assembly: AssemblyVersion("5.1.0")]
+[assembly: AssemblyFileVersion("5.1.0")]
\ No newline at end of file diff --git a/SendGrid/SendGridMail/SendGrid.cs b/SendGrid/SendGridMail/SendGrid.cs index 1533a46..e4e0d22 100644 --- a/SendGrid/SendGridMail/SendGrid.cs +++ b/SendGrid/SendGridMail/SendGrid.cs @@ -20,7 +20,9 @@ namespace SendGrid private static readonly Regex TemplateTest = new Regex(@"<%\s*body\s*%>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex TextUnsubscribeTest = new Regex(@"<%\s*%>", RegexOptions.Compiled);
private static readonly Regex HtmlUnsubscribeTest = new Regex(@"<%\s*([^\s%]+\s?)+\s*%>", RegexOptions.Compiled);
- #endregion
+ private const string SinkHost = "sink.sendgrid.net";
+
+ #endregion
#region Initialization and Constructors
@@ -96,7 +98,16 @@ namespace SendGrid public MailAddress[] To
{
- get { return _message.To.ToArray(); }
+ get
+ {
+ if (_sendToSink)
+ {
+ return _message.To
+ .Select(ma => new MailAddress(string.Format("{0}_at_{1}@{2}", ma.User, ma.Host, SinkHost), ma.DisplayName))
+ .ToArray();
+ }
+ return _message.To.ToArray();
+ }
set
{
_message.To.Clear();
@@ -151,8 +162,9 @@ namespace SendGrid 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>();
+ private bool _sendToSink;
- public void AddTo(String address)
+ public void AddTo(String address)
{
var mailAddress = new MailAddress(address);
_message.To.Add(mailAddress);
@@ -222,6 +234,11 @@ namespace SendGrid Header.AddSubstitution(replacementTag, substitutionValues);
}
+ public void AddSection(String relacementTag, String sectionValue)
+ {
+ Header.AddSection(relacementTag, sectionValue);
+ }
+
public void AddUniqueArgs(IDictionary<String, String> identifiers)
{
Header.AddUniqueArgs(identifiers);
@@ -245,6 +262,16 @@ namespace SendGrid StreamedAttachments[name] = ms;
}
+ public void EmbedStreamImage(Stream stream, String name)
+ {
+ var ms = new MemoryStream();
+ stream.CopyTo(ms);
+ ms.Seek(0, SeekOrigin.Begin);
+ StreamedAttachments[name] = ms;
+
+ _contentImages[name] = name;
+ }
+
public void AddAttachment(String filePath)
{
_attachments.Add(filePath);
@@ -265,6 +292,11 @@ namespace SendGrid headers.Keys.ToList().ForEach(key => Headers[key] = headers[key]);
}
+ public void SendToSink(bool value = true)
+ {
+ _sendToSink = value;
+ }
+
#endregion
#region SMTP API Functions
diff --git a/SendGrid/SendGridMail/StreamedFileBody.cs b/SendGrid/SendGridMail/StreamedFileBody.cs index faa9d59..125fe24 100644 --- a/SendGrid/SendGridMail/StreamedFileBody.cs +++ b/SendGrid/SendGridMail/StreamedFileBody.cs @@ -6,7 +6,7 @@ using System.Text; namespace SendGridMail
{
- public class StreamedFileBody : Body
+ public class StreamedFileBody
{
private string _name;
private string _filename;
diff --git a/SendGrid/SendGridMail/Transport/Web.cs b/SendGrid/SendGridMail/Transport/Web.cs index 05e710b..7c965f3 100644 --- a/SendGrid/SendGridMail/Transport/Web.cs +++ b/SendGrid/SendGridMail/Transport/Web.cs @@ -22,26 +22,38 @@ namespace SendGrid public const String Endpoint = "https://api.sendgrid.com/api/mail.send";
private readonly NetworkCredential _credentials;
+ private readonly TimeSpan _timeout;
#endregion
/// <summary>
- /// Creates a new Web interface for sending mail. Preference is using the Factory method.
+ /// Creates a new Web interface for sending mail
/// </summary>
/// <param name="credentials">SendGridMessage user parameters</param>
- /// <param name="https">Use https?</param>
public Web(NetworkCredential credentials)
{
_credentials = credentials;
+ _timeout = TimeSpan.FromSeconds(100);
}
+ /// <summary>
+ /// Creates a new Web interface for sending mail.
+ /// </summary>
+ /// <param name="credentials">SendGridMessage user parameters</param>
+ /// <param name="httpTimeout">HTTP request timeout</param>
+ public Web(NetworkCredential credentials, TimeSpan httpTimeout)
+ {
+ _ credentials = credentials;
+ _ timeout = httpTimeout;
+ }
+
/// <summary>
/// Delivers a message over SendGrid's Web interface
/// </summary>
/// <param name="message"></param>
public void Deliver(ISendGrid message)
{
- var client = new HttpClient ();
+ var client = new HttpClient()
var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "sendgrid/" + version + ";csharp");
@@ -62,16 +74,17 @@ namespace SendGrid var client = new HttpClient ();
var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
- client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "sendgrid/" + version + ";csharp");
+
+ client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "sendgrid/" + version + ";csharp");
var content = new MultipartFormDataContent();
AttachFormParams(message, content);
AttachFiles(message, content);
- var response = await client.PostAsync(Endpoint + ".xml", content);
+ var response = await client.PostAsync("https://" + BaseUrl + Endpoint + ".xml", content);
await CheckForErrorsAsync(response);
}
- #region Support Methods
+ #region Support Methods
private void AttachFormParams(ISendGrid message, MultipartFormDataContent content)
{
@@ -164,12 +177,6 @@ namespace SendGrid private static async Task CheckForErrorsAsync(HttpResponseMessage response)
{
- //transport error
- if (response.StatusCode != HttpStatusCode.OK)
- {
- throw new Exception(response.ReasonPhrase);
- }
-
var content = await response.Content.ReadAsStreamAsync();
var errors = GetErrorsInResponse(content);
diff --git a/SendGrid/SendGridMail/packages.config b/SendGrid/SendGridMail/packages.config index 003f620..606bf16 100644 --- a/SendGrid/SendGridMail/packages.config +++ b/SendGrid/SendGridMail/packages.config @@ -1,4 +1,5 @@ <?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="SendGrid.SmtpApi" version="1.1.3" targetFramework="net40" />
+ <package id="SendGrid.SmtpApi" version="1.2.1" targetFramework="net45" />
</packages>
diff --git a/SendGrid/Tests/TestSendgrid.cs b/SendGrid/Tests/TestSendgrid.cs index 3bf0077..37a9990 100644 --- a/SendGrid/Tests/TestSendgrid.cs +++ b/SendGrid/Tests/TestSendgrid.cs @@ -346,5 +346,66 @@ namespace Tests var json = header.JsonString();
Assert.AreEqual("{\"filters\" : {\"opentrack\" : {\"settings\" : {\"enable\" : \"0\"}}}}", json);
}
+ [Test]
+ public void TestAddSection()
+ {
+ var header = new Header();
+ var sendgrid = new SendGridMessage(header);
+
+ sendgrid.AddSection("tag", "value");
+
+ var json = header.JsonString();
+ Assert.AreEqual("{\"section\" : {\"tag\" : \"value\"}}", json);
}
-}
\ No newline at end of file +
+ [Test]
+ public void TestSendToSink()
+ {
+ // Arrange
+
+ var message = new SendGridMessage();
+ message.To = new[]
+ {
+ new MailAddress("foo@bar.com", "Foo Bar"),
+ };
+ message.AddTo("foo1@bar1.com");
+
+ // Act
+
+ message.SendToSink();
+
+ // Assert
+
+ Assert.AreEqual("foo_at_bar.com@sink.sendgrid.net", message.To[0].Address);
+ Assert.AreEqual("Foo Bar", message.To[0].DisplayName);
+
+ Assert.AreEqual("foo1_at_bar1.com@sink.sendgrid.net", message.To[1].Address);
+ Assert.AreEqual("", message.To[1].DisplayName);
+ }
+
+ [Test]
+ public void TestSendToSinkOff()
+ {
+ // Arrange
+
+ var message = new SendGridMessage();
+ message.To = new[]
+ {
+ new MailAddress("foo@bar.com", "Foo Bar"),
+ };
+ message.AddTo("foo1@bar1.com");
+ message.SendToSink();
+
+ // Act
+
+ message.SendToSink(false);
+
+ // Assert
+
+ Assert.AreEqual("foo@bar.com", message.To[0].Address);
+ Assert.AreEqual("Foo Bar", message.To[0].DisplayName);
+
+ Assert.AreEqual("foo1@bar1.com", message.To[1].Address);
+ Assert.AreEqual("", message.To[1].DisplayName);
+ }
+}
diff --git a/SendGrid/Tests/Tests.csproj b/SendGrid/Tests/Tests.csproj index ff18252..7a62424 100644 --- a/SendGrid/Tests/Tests.csproj +++ b/SendGrid/Tests/Tests.csproj @@ -55,8 +55,9 @@ <Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
</Reference>
- <Reference Include="SendGrid.SmtpApi">
- <HintPath>..\packages\SendGrid.SmtpApi.1.1.3\lib\net40\SendGrid.SmtpApi.dll</HintPath>
+ <Reference Include="SendGrid.SmtpApi, Version=1.2.1.0, Culture=neutral, PublicKeyToken=2ae73662c35d80e4, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\packages\SendGrid.SmtpApi.1.2.1\lib\net40\SendGrid.SmtpApi.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
diff --git a/SendGrid/Tests/packages.config b/SendGrid/Tests/packages.config index 25c4bf6..d70d4e6 100644 --- a/SendGrid/Tests/packages.config +++ b/SendGrid/Tests/packages.config @@ -4,4 +4,5 @@ <package id="Moq" version="4.2.1402.2112" targetFramework="net40" />
<package id="NUnit" version="2.6.3" targetFramework="net40" />
<package id="SendGrid.SmtpApi" version="1.1.3" targetFramework="net40" />
+ <package id="SendGrid.SmtpApi" version="1.2.1" targetFramework="net45" />
</packages>
\ No newline at end of file |