public void Inline_ContentDispositionInlineWithFileName_True() { var attachment = new Attachment { Headers = new HeaderDictionary { { "Content-Disposition", new HeaderValue(@"inline; filename=""test.html""") } } }; attachment.ContentDisposition.Inline.ShouldBe(); attachment.ContentDisposition.FileName.ShouldBe("test.html"); }
public void Inline_ContentDispositionInlineWithFileNameWithExoticCharacter_True() { var attachment = new Attachment { Headers = new HeaderDictionary { { "Content-Disposition", new HeaderValue(@"attachment;filename=""2013135 Charité.pdf""") } } }; attachment.ContentDisposition.Inline.ShouldNotBe(); attachment.ContentDisposition.FileName.ShouldBe("2013135 Charite.pdf"); }
public void Attachment_SavesWithMessage() { var msg = new AE.Net.Mail.MailMessage() { From = new System.Net.Mail.MailAddress("*****@*****.**") }; var firstAttachmentContents = "This is a test."; var attachment = new Attachment() { Body = Convert.ToBase64String(Encoding.Default.GetBytes(firstAttachmentContents)), ContentTransferEncoding = "base64", Encoding = Encoding.ASCII }; attachment.Headers.Add("Content-Type", new HeaderValue(@"text/plain; filename=""Readme.txt""")); msg.Attachments.Add(attachment); var rnd = new Random(); var secondAttachmentContents = new byte[rnd.Next(10, 1000)]; rnd.NextBytes(secondAttachmentContents); attachment = new Attachment() { Body = Convert.ToBase64String(secondAttachmentContents), ContentTransferEncoding = "base64", Encoding = Encoding.ASCII }; attachment.Headers.Add("Content-Type", new HeaderValue(@"application/binary; filename=""Data.bin""")); msg.Attachments.Add(attachment); var reparsed = Reparse(msg); reparsed.Attachments.Count.ShouldBe(2); reparsed.Attachments.First().Filename.ShouldBe("Readme.txt"); reparsed.Attachments.First().Body.ShouldBe(firstAttachmentContents); reparsed.Attachments.Last().Filename.ShouldBe("Data.bin"); Convert.FromBase64String(reparsed.Attachments.Last().Body).ShouldBe(secondAttachmentContents); }
public void GetAttachment(Property[] inputs, RequiredProperties required, Property[] returns, MethodType methodType, ServiceObject serviceObject) { serviceObject.Properties.InitResultTable(); Helper h = new Helper(serviceBroker); string mailbox = inputs.Where(p => p.Name.Equals("mailbox")).FirstOrDefault().Value.ToString(); string uid = string.Empty; string subjectfilter = string.Empty; int index = -1; uid = inputs.Where(p => p.Name.Equals("uid")).FirstOrDefault().Value.ToString(); index = int.Parse(inputs.Where(p => p.Name.Equals("attachmentindex")).FirstOrDefault().Value.ToString()); AE.Net.Mail.MailMessage m = null; List <AE.Net.Mail.Attachment> attachments = new List <AE.Net.Mail.Attachment>(); try { using (var ic = h.GetImapClient()) { m = ic.GetMessage(uid, false); if (m == null) { return; } attachments = m.Attachments as List <AE.Net.Mail.Attachment>; AE.Net.Mail.Attachment a = null; if (index == -1 || index > attachments.Count) { return; } a = attachments[index]; if (a == null) { return; } returns.Where(p => p.Name.ToLower().Equals("body")).FirstOrDefault().Value = a.Body; returns.Where(p => p.Name.ToLower().Equals("charset")).FirstOrDefault().Value = a.Charset; returns.Where(p => p.Name.ToLower().Equals("contenttransferencoding")).FirstOrDefault().Value = a.ContentTransferEncoding; returns.Where(p => p.Name.ToLower().Equals("contenttype")).FirstOrDefault().Value = a.ContentType; returns.Where(p => p.Name.ToLower().Equals("filename")).FirstOrDefault().Value = a.Filename; returns.Where(p => p.Name.ToLower().Equals("rawheaders")).FirstOrDefault().Value = a.RawHeaders; returns.Where(p => p.Name.ToLower().Equals("onserver")).FirstOrDefault().Value = a.OnServer; returns.Where(p => p.Name.ToLower().Equals("mailbox")).FirstOrDefault().Value = inputs.Where(p => p.Name.Equals("mailbox")).FirstOrDefault().Value.ToString(); returns.Where(p => p.Name.ToLower().Equals("uid")).FirstOrDefault().Value = inputs.Where(p => p.Name.Equals("uid")).FirstOrDefault().Value.ToString();; returns.Where(p => p.Name.ToLower().Equals("attachmentindex")).FirstOrDefault().Value = inputs.Where(p => p.Name.Equals("attachmentindex")).FirstOrDefault().Value.ToString();; ic.Disconnect(); } } catch (Exception ex) { //serviceObject.Properties.BindPropertiesToResultTable(); } serviceObject.Properties.BindPropertiesToResultTable(); }
public void GetAttachment(Property[] inputs, RequiredProperties required, Property[] returns, MethodType methodType, ServiceObject serviceObject) { serviceObject.Properties.InitResultTable(); Helper h = new Helper(serviceBroker); string mailbox = inputs.Where(p => p.Name.Equals("mailbox")).FirstOrDefault().Value.ToString(); string uid = string.Empty; string subjectfilter = string.Empty; int index = -1; uid = inputs.Where(p => p.Name.Equals("uid")).FirstOrDefault().Value.ToString(); index = int.Parse(inputs.Where(p => p.Name.Equals("attachmentindex")).FirstOrDefault().Value.ToString()); AE.Net.Mail.MailMessage m = null; List<AE.Net.Mail.Attachment> attachments = new List<AE.Net.Mail.Attachment>(); try { using (var ic = h.GetImapClient()) { m = ic.GetMessage(uid, false); if (m == null) { return; } attachments = m.Attachments as List<AE.Net.Mail.Attachment>; AE.Net.Mail.Attachment a = null; if (index == -1 || index > attachments.Count) { return; } a = attachments[index]; if (a == null) { return; } returns.Where(p => p.Name.ToLower().Equals("body")).FirstOrDefault().Value = a.Body; returns.Where(p => p.Name.ToLower().Equals("charset")).FirstOrDefault().Value = a.Charset; returns.Where(p => p.Name.ToLower().Equals("contenttransferencoding")).FirstOrDefault().Value = a.ContentTransferEncoding; returns.Where(p => p.Name.ToLower().Equals("contenttype")).FirstOrDefault().Value = a.ContentType; returns.Where(p => p.Name.ToLower().Equals("filename")).FirstOrDefault().Value = a.Filename; returns.Where(p => p.Name.ToLower().Equals("rawheaders")).FirstOrDefault().Value = a.RawHeaders; returns.Where(p => p.Name.ToLower().Equals("onserver")).FirstOrDefault().Value = a.OnServer; returns.Where(p => p.Name.ToLower().Equals("mailbox")).FirstOrDefault().Value = inputs.Where(p => p.Name.Equals("mailbox")).FirstOrDefault().Value.ToString(); returns.Where(p => p.Name.ToLower().Equals("uid")).FirstOrDefault().Value = inputs.Where(p => p.Name.Equals("uid")).FirstOrDefault().Value.ToString();; returns.Where(p => p.Name.ToLower().Equals("attachmentindex")).FirstOrDefault().Value = inputs.Where(p => p.Name.Equals("attachmentindex")).FirstOrDefault().Value.ToString();; ic.Disconnect(); } } catch (Exception ex) { //serviceObject.Properties.BindPropertiesToResultTable(); } serviceObject.Properties.BindPropertiesToResultTable(); }
public void Inline_NoContentDisposition_True() { var attachment = new Attachment { Headers = new HeaderDictionary() }; attachment.ContentDisposition.Inline.ShouldBe(); }
public void Inline_ContentDispositionUnknown_False() { var attachment = new Attachment { Headers = new HeaderDictionary { { "Content-Disposition", new HeaderValue("rubbish") } } }; attachment.ContentDisposition.Inline.ShouldNotBe(); }
public void Inline_ContentDispositionInline_True() { var attachment = new Attachment { Headers = new HeaderDictionary { { "Content-Disposition", new HeaderValue("inline") } } }; attachment.ContentDisposition.Inline.ShouldBe(); }
private static string ParseMime(Stream reader, string boundary, ref int maxLength, ICollection <Attachment> attachments, Encoding encoding, char?termChar) { var maxLengthSpecified = maxLength > 0; string data = null, bounderInner = "--" + boundary, bounderOuter = bounderInner + "--"; var n = 0; var body = new System.Text.StringBuilder(); do { if (maxLengthSpecified && maxLength <= 0) { return(body.ToString()); } if (data != null) { body.Append(data); } data = reader.ReadLine(ref maxLength, encoding, termChar); n++; } while (data != null && !data.StartsWith(bounderInner)); while (data != null && !data.StartsWith(bounderOuter) && !(maxLengthSpecified && maxLength == 0)) { data = reader.ReadLine(ref maxLength, encoding, termChar); if (data == null) { break; } var a = new Attachment { Encoding = encoding }; var part = new StringBuilder(); // read part header while (!data.StartsWith(bounderInner) && data != string.Empty && !(maxLengthSpecified && maxLength == 0)) { part.AppendLine(data); data = reader.ReadLine(ref maxLength, encoding, termChar); if (data == null) { break; } } a.RawHeaders = part.ToString(); // header body // check for nested part var nestedboundary = a.Headers.GetBoundary(); if (!string.IsNullOrEmpty(nestedboundary)) { ParseMime(reader, nestedboundary, ref maxLength, attachments, encoding, termChar); while (!data.StartsWith(bounderInner) && !(maxLengthSpecified && maxLength == 0)) { data = reader.ReadLine(ref maxLength, encoding, termChar); } } else { data = reader.ReadLine(ref maxLength, a.Encoding, termChar); if (data == null) { break; } var nestedBody = new StringBuilder(); while (!data.StartsWith(bounderInner) && !(maxLengthSpecified && maxLength == 0)) { nestedBody.AppendLine(data); data = reader.ReadLine(ref maxLength, a.Encoding, termChar); } a.SetBody(nestedBody.ToString()); attachments.Add(a); } } return(body.ToString()); }