summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrandon West <brawest@gmail.com>2012-07-13 15:57:31 -0600
committerBrandon West <brawest@gmail.com>2012-07-13 15:57:31 -0600
commit546ce43c7f7321aadf49b797cc876c28adf1e098 (patch)
tree4e1a0f81e4797a1388f7a377eac7c4c146417f95
parent9da0d78cacd7530dbb1c6b26353bc22a90dea2d9 (diff)
parent2bec2e3d78b5d993e5daa7b8d0e5edfd0c03034d (diff)
downloadsendgrid-csharp-546ce43c7f7321aadf49b797cc876c28adf1e098.zip
sendgrid-csharp-546ce43c7f7321aadf49b797cc876c28adf1e098.tar.gz
sendgrid-csharp-546ce43c7f7321aadf49b797cc876c28adf1e098.tar.bz2
merge streaming-attachments branch
-rwxr-xr-xSendGrid/SendGridMail/ISendGrid.cs9
-rwxr-xr-xSendGrid/SendGridMail/Mail.csproj1
-rwxr-xr-xSendGrid/SendGridMail/SendGrid.cs26
-rwxr-xr-xSendGrid/SendGridMail/StreamedFileBody.cs42
-rwxr-xr-xSendGrid/SendGridMail/Transport/Web.cs10
-rwxr-xr-xSendGrid/Tests/TestStreamedFileBody.cs35
-rwxr-xr-xSendGrid/Tests/Tests.csproj5
7 files changed, 126 insertions, 2 deletions
diff --git a/SendGrid/SendGridMail/ISendGrid.cs b/SendGrid/SendGridMail/ISendGrid.cs
index 10f0826..a96e93c 100755
--- a/SendGrid/SendGridMail/ISendGrid.cs
+++ b/SendGrid/SendGridMail/ISendGrid.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.IO;
using System.Net;
using System.Net.Mail;
@@ -17,6 +18,7 @@ namespace SendGridMail
MailAddress[] Cc { get; }
MailAddress[] Bcc { get; }
MailAddress[] ReplyTo { get; set; }
+ Dictionary<String, MemoryStream> StreamedAttachments { get; set; }
String[] Attachments { get; set; }
String Subject { get; set; }
Dictionary<String, String> Headers { get; set; }
@@ -120,6 +122,13 @@ namespace SendGridMail
void AddAttachment(String filePath);
/// <summary>
+ /// Add a stream as an attachment to the message
+ /// </summary>
+ /// <param name="stream">Stream of file to be attached</param>
+ /// <param name="name">Name of file to be attached</param>
+ void AddAttachment(Stream stream, String name);
+
+ /// <summary>
/// GetRecipients returns a list of all the recepients by retrieving the to, cc, and bcc lists.
/// </summary>
/// <returns></returns>
diff --git a/SendGrid/SendGridMail/Mail.csproj b/SendGrid/SendGridMail/Mail.csproj
index f1bf7f6..91d3b91 100755
--- a/SendGrid/SendGridMail/Mail.csproj
+++ b/SendGrid/SendGridMail/Mail.csproj
@@ -53,6 +53,7 @@
<Compile Include="ISendGrid.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SendGrid.cs" />
+ <Compile Include="StreamedFileBody.cs" />
<Compile Include="Transport\ITransport.cs" />
<Compile Include="Transport\Web.cs" />
<Compile Include="Transport\SMTP.cs" />
diff --git a/SendGrid/SendGridMail/SendGrid.cs b/SendGrid/SendGridMail/SendGrid.cs
index 14b9a9a..3f2878f 100755
--- a/SendGrid/SendGridMail/SendGrid.cs
+++ b/SendGrid/SendGridMail/SendGrid.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
+using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
@@ -275,6 +276,13 @@ namespace SendGridMail
}
}
+ private Dictionary<String, MemoryStream> _streamedAttachments = new Dictionary<string, MemoryStream>();
+ public Dictionary<String, MemoryStream> StreamedAttachments
+ {
+ get { return _streamedAttachments; }
+ set { _streamedAttachments = value; }
+ }
+
private List<String> _attachments = new List<String>();
public String[] Attachments
{
@@ -297,7 +305,14 @@ namespace SendGridMail
{
Header.SetCategory(category);
}
-
+
+ public void AddAttachment(Stream stream, String name)
+ {
+ MemoryStream ms = new MemoryStream();
+ stream.CopyTo(ms);
+ StreamedAttachments[name] = ms;
+ }
+
public void AddAttachment(String filePath)
{
_attachments.Add(filePath);
@@ -495,6 +510,15 @@ namespace SendGridMail
}
}
+ if(StreamedAttachments != null)
+ {
+ foreach (var attachment in StreamedAttachments)
+ {
+ attachment.Value.Position = 0;
+ message.Attachments.Add(new Attachment(attachment.Value, attachment.Key));
+ }
+ }
+
if (Text != null)
{
AlternateView plainView = AlternateView.CreateAlternateViewFromString(Text, null, "text/plain");
diff --git a/SendGrid/SendGridMail/StreamedFileBody.cs b/SendGrid/SendGridMail/StreamedFileBody.cs
new file mode 100755
index 0000000..6bb5ec1
--- /dev/null
+++ b/SendGrid/SendGridMail/StreamedFileBody.cs
@@ -0,0 +1,42 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using CodeScales.Http.Entity.Mime;
+using CodeScales.Http.Network;
+
+namespace SendGridMail
+{
+ public class StreamedFileBody : Body
+ {
+ private string _name;
+ private string _filename;
+ private byte[] _content;
+
+ public StreamedFileBody(MemoryStream stream, String name)
+ {
+ if (stream == null) throw new ArgumentException("Invalid attachment stream");
+ if (String.IsNullOrEmpty(name)) throw new ArgumentException("Invalid attachment name");
+
+ _name = "files[" + Path.GetFileName(name) + "]";
+ _filename = name;
+ _content = stream.ToArray();
+ }
+
+ public byte[] GetContent(string boundry)
+ {
+ var bytes = new List<byte>();
+
+ string paramBoundry = "--" + boundry + "\r\n";
+ string stringParam = "Content-Disposition: form-data; name=\"" + _name + "\"; filename=\"" + _filename + "\"\r\n";
+ string paramEnd = "Content-Type: image/png\r\n\r\n";
+ string foo = paramBoundry + stringParam + paramEnd;
+
+ bytes.AddRange(Encoding.ASCII.GetBytes(paramBoundry + stringParam + paramEnd));
+ bytes.AddRange(_content);
+ bytes.AddRange(Encoding.ASCII.GetBytes("\r\n"));
+ return bytes.ToArray();
+ }
+ }
+}
diff --git a/SendGrid/SendGridMail/Transport/Web.cs b/SendGrid/SendGridMail/Transport/Web.cs
index 885f482..5a27469 100755
--- a/SendGrid/SendGridMail/Transport/Web.cs
+++ b/SendGrid/SendGridMail/Transport/Web.cs
@@ -84,8 +84,11 @@ namespace SendGridMail.Transport
private void AttachFiles(ISendGrid message, MultipartEntity multipartEntity)
{
- var files = FetchFileBodies(message);
+ var files = FetchFileBodies(message);
files.ForEach(kvp => multipartEntity.AddBody(new FileBody("files[" + Path.GetFileName(kvp.Key) + "]", Path.GetFileName(kvp.Key), kvp.Value)));
+
+ var streamingFiles = FetchStreamingFileBodies(message);
+ streamingFiles.ForEach(kvp => multipartEntity.AddBody(new StreamedFileBody(kvp.Value, kvp.Key)));
}
private void CheckForErrors(CodeScales.Http.Methods.HttpResponse response)
@@ -148,6 +151,11 @@ namespace SendGridMail.Transport
}
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)
{
diff --git a/SendGrid/Tests/TestStreamedFileBody.cs b/SendGrid/Tests/TestStreamedFileBody.cs
new file mode 100755
index 0000000..60d43b8
--- /dev/null
+++ b/SendGrid/Tests/TestStreamedFileBody.cs
@@ -0,0 +1,35 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+using SendGridMail;
+
+namespace Tests
+{
+ [TestFixture]
+ public class TestStreamedFileBody
+ {
+ [Test]
+ public void TestGetContent()
+ {
+ var name = "foo";
+ var file = "bar";
+ var boundary = "raz";
+
+ var memoryStream = new MemoryStream();
+ var stream = new StreamWriter(memoryStream);
+ stream.Write(file);
+ stream.Flush();
+ stream.Close();
+
+ var streamedFile = new StreamedFileBody(memoryStream, name);
+ var bytes = streamedFile.GetContent(boundary);
+ var result = System.Text.Encoding.ASCII.GetString(bytes);
+ var expected = "--raz\r\nContent-Disposition: form-data; name=\"files[foo]\"; filename=\"foo\"\r\nContent-Type: image/png\r\n\r\nbar\r\n";
+ Assert.AreEqual(expected, result, "message formated correctly");
+
+ }
+ }
+}
diff --git a/SendGrid/Tests/Tests.csproj b/SendGrid/Tests/Tests.csproj
index c75563a..cdfd9b7 100755
--- a/SendGrid/Tests/Tests.csproj
+++ b/SendGrid/Tests/Tests.csproj
@@ -31,6 +31,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
+ <Reference Include="CodeScales.Http, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\SendGridMail\lib\CodeScales.Http.dll</HintPath>
+ </Reference>
<Reference Include="Moq">
<HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
</Reference>
@@ -57,6 +61,7 @@
<Compile Include="TestJsonUtils.cs" />
<Compile Include="TestSendgrid.cs" />
<Compile Include="TestSendgridMessageSetup.cs" />
+ <Compile Include="TestStreamedFileBody.cs" />
<Compile Include="TestTreeNode.cs" />
<Compile Include="TestUtils.cs" />
<Compile Include="Transport\TestWeb.cs" />