summaryrefslogtreecommitdiffstats
path: root/SendGrid/SendGridMail/StreamedFileBody.cs
diff options
context:
space:
mode:
authorTyler Bischel <tyler.bischel@sendgrid.com>2012-04-25 15:51:54 -0700
committerTyler Bischel <tyler.bischel@sendgrid.com>2012-04-25 15:51:54 -0700
commitd805fa47bbe6fd2082e18b7c99a2be6e833f89b9 (patch)
tree89df96489c97c61bf69037978dbee0e2d448d753 /SendGrid/SendGridMail/StreamedFileBody.cs
parente8df320eaca9e0c2d23e4819cdf55c66bf3b7d8d (diff)
downloadsendgrid-csharp-d805fa47bbe6fd2082e18b7c99a2be6e833f89b9.zip
sendgrid-csharp-d805fa47bbe6fd2082e18b7c99a2be6e833f89b9.tar.gz
sendgrid-csharp-d805fa47bbe6fd2082e18b7c99a2be6e833f89b9.tar.bz2
added support for attachments by streams, and made the REST call truncate to onl the file name, not the full path
Diffstat (limited to 'SendGrid/SendGridMail/StreamedFileBody.cs')
-rwxr-xr-xSendGrid/SendGridMail/StreamedFileBody.cs42
1 files changed, 42 insertions, 0 deletions
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();
+ }
+ }
+}