Exemplo n.º 1
0
 public static async Task<StorageFile> SaveAttachmentToTempAsync(Attachment attachment)
 {
     StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(attachment.Filename, CreationCollisionOption.ReplaceExisting);
     IRandomAccessStream randomStream = await file.OpenAsync(FileAccessMode.ReadWrite);
     Stream writeStream = randomStream.AsStream();
     attachment.Save(writeStream);
     writeStream.Close();
     return file;
 }
Exemplo n.º 2
0
        public void Attachment_SavesWithMessage()
        {
            var msg = new WinPhone.Mail.Protocols.MailMessage()
            {
                From = new MailAddress("*****@*****.**"),
                Scope = Scope.HeadersAndBody,
            };
            var firstAttachmentContents = "This is a test.";
            var attachment = new Attachment()
            {
                Body = Convert.ToBase64String(Encoding.Default.GetBytes(firstAttachmentContents)),
                ContentTransferEncoding = "base64",
                Encoding = Encoding.ASCII,
                Scope = Scope.HeadersAndBody,
            };
            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,
                Scope = Scope.HeadersAndBody,
            };
            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);
        }
Exemplo n.º 3
0
 public virtual void Add(Attachment viewOrAttachment)
 {
     if (viewOrAttachment.IsAttachment)
     {
         Attachments.Add(viewOrAttachment);
     }
     else
     {
         AlternateViews.Add(viewOrAttachment);
     }
 }
Exemplo n.º 4
0
        public async Task OpenAttachmentAsync(MailMessage message, Attachment attachment)
        {
            // Lazy load
            if (attachment.Scope < Scope.HeadersAndBody)
            {
                // Check local storage
                if (MailStorage.HasMessagePart(message.GetThreadId(), message.GetMessageId(), attachment.BodyId))
                {
                    // TODO: Can we open the attachment directly from isolated storage?
                    attachment.Body = await MailStorage.GetMessagePartAsync(message.GetThreadId(), message.GetMessageId(), attachment.BodyId);
                }
                else
                {
                    // Download from the network
                    await GmailImap.GetBodyPartAsync(message.Uid, attachment, async () =>
                    {
                        if (ActiveLabel.Info.StoreMessages && ActiveLabel.Info.StoreAttachments)
                        {
                            await MailStorage.StoreMessagePartAsync(message.GetThreadId(), message.GetMessageId(), attachment.BodyId, attachment.Body);
                        }
                    }, CancellationToken.None);
                }
            }

            StorageFile file = await MailStorage.SaveAttachmentToTempAsync(attachment);
            // http://architects.dzone.com/articles/lap-around-windows-phone-8-sdk
            await Launcher.LaunchFileAsync(file);

            // TODO: Delete temp files on app close?
        }
Exemplo n.º 5
0
        private static string ParseMime(Stream reader, string boundary, ref int maxLength, ICollection<Attachment> attachments, Encoding encoding, char? termChar, Scope scope)
        {
            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, scope);
                    while (!data.StartsWith(bounderInner))
                        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);
                        if (data == null)
                        {
                            throw new EndOfStreamException("Unexpected end of file");
                        }
                    }
                    if (scope > Scope.HeadersAndMime)
                    {
                        a.SetBody(nestedBody.ToString());
                    }
                    attachments.Add(a);
                }
            }
            return body.ToString();
        }