diff options
-rwxr-xr-x | SendGrid/SendGridMail/ISendGrid.cs | 9 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/Mail.csproj | 1 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/SendGrid.cs | 23 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/StreamedFileBody.cs | 42 | ||||
-rwxr-xr-x | SendGrid/SendGridMail/Transport/Web.cs | 10 |
5 files changed, 84 insertions, 1 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 42214b7..4a14211 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
{
@@ -298,6 +306,12 @@ 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)
{
@@ -496,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 8e0c512..f3448ad 100755 --- a/SendGrid/SendGridMail/Transport/Web.cs +++ b/SendGrid/SendGridMail/Transport/Web.cs @@ -85,7 +85,10 @@ namespace SendGridMail.Transport private void AttachFiles(ISendGrid message, MultipartEntity multipartEntity)
{
var files = FetchFileBodies(message);
- files.ForEach(kvp => multipartEntity.AddBody(new FileBody("files[" + kvp.Key + "]", kvp.Key, kvp.Value)));
+ files.ForEach(kvp => multipartEntity.AddBody(new FileBody("files[" + Path.GetFileName(kvp.Key) + "]", 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)
{
|