diff options
Diffstat (limited to 'SendGrid/SendGridMail/WebFileUpload.cs')
-rwxr-xr-x | SendGrid/SendGridMail/WebFileUpload.cs | 137 |
1 files changed, 2 insertions, 135 deletions
diff --git a/SendGrid/SendGridMail/WebFileUpload.cs b/SendGrid/SendGridMail/WebFileUpload.cs index f752472..31b6f7d 100755 --- a/SendGrid/SendGridMail/WebFileUpload.cs +++ b/SendGrid/SendGridMail/WebFileUpload.cs @@ -26,127 +26,17 @@ namespace SendGridMail }
- public void testNoAttach(Attachment attachment)
+ public void TestAddAttachment(Attachment attachment)
{
WebClient myWebClient = new WebClient();
NameValueCollection collection = new NameValueCollection();
StreamReader sr = new StreamReader(attachment.ContentStream);
var data = sr.ReadToEnd();
- //byte[] bytes = new byte[attachment.ContentStream.Length];
- //attachment.ContentStream.Position = 0;
-
- //attachment.ContentStream.Read(bytes, 0, (int) attachment.ContentStream.Length);
-
- //String data = Encoding.Default.GetString(bytes);
-
- collection.Add("api_user", "cjbuchmann");
- collection.Add("api_key", "Gadget_15");
- collection.Add("from", "cj.buchmann@sendgrid.com");
- collection.Add("to", "cj.buchmann@sendgrid.com");
- collection.Add("subject", "hello world test");
- collection.Add("text", "hello world plain text");
- collection.Add("files[file1.jpg]", data);
+ byte[] bytes = new byte[attachment.ContentStream.Length];
byte[] responseArray = myWebClient.UploadValues("https://sendgrid.com/api/mail.send.xml", collection);
Console.WriteLine("\nResponse received was :\n{0}", Encoding.ASCII.GetString(responseArray));
- Console.WriteLine("DONE IN HERE!");
- }
-
- public static string UploadFileEx(string uploadfile, string url,
- string fileFormName, string contenttype, NameValueCollection querystring)
- {
- if ((fileFormName == null) ||
- (fileFormName.Length == 0))
- {
- fileFormName = "file";
- }
-
- if ((contenttype == null) ||
- (contenttype.Length == 0))
- {
- contenttype = "application/octet-stream";
- }
-
-
- string postdata;
- postdata = "?";
- if (querystring != null)
- {
- foreach (string key in querystring.Keys)
- {
- postdata += key + "=" + querystring.Get(key) + "&";
- }
- }
- Uri uri = new Uri(url + postdata);
-
- Console.WriteLine("data is "+uri.AbsoluteUri);
-
-
- string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
- HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
-
- Console.WriteLine(webrequest.RequestUri);
-
- webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
- webrequest.Method = "POST";
-
-
- // Build up the post message header
-
- StringBuilder sb = new StringBuilder();
- sb.Append("--");
- sb.Append(boundary);
- sb.Append("\r\n");
- sb.Append("Content-Disposition: form-data; name=\"");
- sb.Append(fileFormName);
- sb.Append("\"; filename=\"");
- sb.Append(Path.GetFileName(uploadfile));
- sb.Append("\"");
- sb.Append("\r\n");
- sb.Append("Content-Type: ");
- sb.Append(contenttype);
- sb.Append("\r\n");
- sb.Append("\r\n");
-
- string postHeader = sb.ToString();
- byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
-
- // Build the trailing boundary string as a byte array
-
- // ensuring the boundary appears on a line by itself
-
- byte[] boundaryBytes =
- Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
-
- FileStream fileStream = new FileStream(uploadfile,
- FileMode.Open, FileAccess.Read);
- long length = postHeaderBytes.Length + fileStream.Length +
- boundaryBytes.Length;
- webrequest.ContentLength = length;
-
- Stream requestStream = webrequest.GetRequestStream();
-
- // Write out our post header
-
- requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
-
- // Write out the file contents
-
- byte[] buffer = new Byte[checked((uint)Math.Min(4096,
- (int)fileStream.Length))];
- int bytesRead = 0;
- while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
- requestStream.Write(buffer, 0, bytesRead);
-
- // Write out the trailing boundary
-
- requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
- WebResponse responce = webrequest.GetResponse();
- Stream s = responce.GetResponseStream();
- StreamReader sr = new StreamReader(s);
-
- return sr.ReadToEnd();
}
public void AddAttachment(String filename)
@@ -166,8 +56,6 @@ namespace SendGridMail public void SendAttachments()
{
- StreamAttachments();
-
WebResponse _response = null;
try
@@ -184,26 +72,5 @@ namespace SendGridMail _response.Close();
}
-
- private void StreamAttachments()
- {
- Stream rs = _request.GetRequestStream();
- string formdataTemplate = "Content-Disposition: form-data; name=\"files[{0}]\"; filename=\"{1}\"";
-
- Console.Write("\r\n\r\n\r\n\r\n");
- Console.WriteLine("Request : "+_request.RequestUri);
- Console.Write(_request.Headers.ToString());
-
- Attachment attachment = _attachments.First();
-
- rs.Write(_boundaryBytes, 0, _boundaryBytes.Length);
- String formitem = String.Format(formdataTemplate, attachment.Name, attachment.ToString());
- byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
- rs.Write(formitembytes, 0, formitembytes.Length);
-
- rs.Write(_boundaryBytes, 0, _boundaryBytes.Length);
- }
-
-
}
}
|