summaryrefslogtreecommitdiffstats
path: root/SendGrid/SendGridMail/StreamedFileBody.cs
blob: faa9d5986cd974d0773156ef97f1767756824e59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

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";
     
            bytes.AddRange(Encoding.ASCII.GetBytes(paramBoundry + stringParam + paramEnd));
            bytes.AddRange(_content);
            bytes.AddRange(Encoding.ASCII.GetBytes("\r\n"));
            return bytes.ToArray();
        }
    }
}