Exemplo n.º 1
0
        public virtual async Task <MailMessage> GetMessageAsync(string uid, Scope scope = Scope.HeadersAndBody)
        {
            CheckConnectionStatus();
            var line = await SendCommandGetResponseAsync(string.Format(scope == Scope.Headers ? "TOP {0} 0" : "RETR {0}", uid));

            var size = rxOctets.Match(line).Groups[1].Value.ToInt();

            CheckResultOK(line);
            var msg = new MailMessage();

            msg.Load(_Stream, scope, size, '.');

            msg.Uid = uid;
            var last = await GetResponseAsync();

            if (string.IsNullOrEmpty(last))
            {
                last = await GetResponseAsync();
            }

            if (last != ".")
            {
                System.Diagnostics.Debugger.Break();
                RaiseWarning(msg, "Expected \".\" in stream, but received \"" + last + "\"");
            }

            return(msg);
        }
Exemplo n.º 2
0
        private WinPhone.Mail.Protocols.MailMessage GetMessage(string raw)
        {
            var msg = new WinPhone.Mail.Protocols.MailMessage();

            msg.Load(raw, Scope.HeadersAndBody);

            return(msg);
        }
Exemplo n.º 3
0
		public void Parse_Spam() {
			var dir = System.IO.Path.Combine(Environment.CurrentDirectory.Split(new[] { "AE.Net.Mail" }, StringSplitOptions.RemoveEmptyEntries)[0],
				"AE.Net.Mail\\Tests\\Spam");
			var files = System.IO.Directory.GetFiles(dir, "*.lorien", System.IO.SearchOption.AllDirectories);

			var mindate = new DateTime(1900, 1, 1).Ticks;
			var maxdate = DateTime.MaxValue.Ticks;
			var rxSubject = new Regex(@"^Subject\:\s+\S+");
			MailMessage msg = new MailMessage();
			for (var i = 0; i < files.Length; i++) {
				var file = files[i];
				var txt = System.IO.File.ReadAllText(file);
				using (var stream = System.IO.File.OpenRead(file))
					msg.Load(stream, Scope.HeadersAndBody, (int)stream.Length);

				if (msg.ContentTransferEncoding.IndexOf("quoted", StringComparison.OrdinalIgnoreCase) == -1) {
					continue;
				}

				if (string.IsNullOrEmpty(msg.Body)) {
					continue;
				}

				try {

					msg.Date.Ticks.ShouldBeInRange(mindate, maxdate);
                    Assert.True(!string.IsNullOrEmpty(msg.Subject) || !rxSubject.IsMatch(txt));
					//msg.From.ShouldBe();
					if (msg.To.Count > 0) msg.To.First().ShouldBe();
					if (msg.Cc.Count > 0) msg.Cc.First().ShouldBe();
					if (msg.Bcc.Count > 0) msg.Bcc.First().ShouldBe();

					(msg.Body ?? string.Empty).Trim().ShouldNotBeNullOrEmpty();


				} catch (Exception ex) {
					Console.WriteLine(ex);
					Console.WriteLine(txt);
					throw;
				}
			}
		}
Exemplo n.º 4
0
        public virtual async Task<MailMessage> GetMessageAsync(string uid, Scope scope = Scope.HeadersAndBody)
        {
            CheckConnectionStatus();
            var line = await SendCommandGetResponseAsync(string.Format(scope == Scope.Headers ? "TOP {0} 0" : "RETR {0}", uid));
            var size = rxOctets.Match(line).Groups[1].Value.ToInt();
            CheckResultOK(line);
            var msg = new MailMessage();
            msg.Load(_Stream, scope, size, '.');

            msg.Uid = uid;
            var last = await GetResponseAsync();
            if (string.IsNullOrEmpty(last))
                last = await GetResponseAsync();

            if (last != ".")
            {
                System.Diagnostics.Debugger.Break();
                RaiseWarning(msg, "Expected \".\" in stream, but received \"" + last + "\"");
            }

            return msg;
        }
Exemplo n.º 5
0
        public virtual async Task <MailMessage[]> GetMessagesAsync(string start, string end, bool uid, Scope scope, bool setseen)
        {
            var x = new List <MailMessage>();

            await GetMessagesAsync(start, end, uid, scope, setseen,
                                   async (stream, size, imapHeaders) =>
            {
                var mail = new MailMessage {
                    Encoding = Encoding
                };
                mail.Size = size;

                if (imapHeaders["UID"] != null)
                {
                    mail.Uid = imapHeaders["UID"];
                }

                if (imapHeaders["Flags"] != null)
                {
                    mail.SetFlags(imapHeaders["Flags"]);
                }

                await _Stream.EnsureBufferAsync(mail.Size);
                mail.Load(_Stream, scope, mail.Size);

                foreach (var key in imapHeaders.Keys.Except(new[] { "UID", "Flags", "BODY[]", "BODY[HEADER]" }, StringComparer.OrdinalIgnoreCase))
                {
                    mail.Headers.Add(key, new HeaderValue(imapHeaders[key]));
                }

                x.Add(mail);

                return(mail);
            }
                                   );

            return(x.ToArray());
        }
Exemplo n.º 6
0
        public async Task Download_Message()
        {
            var filename = System.IO.Path.GetTempFileName();

            try
            {
                using (var imap = await GetClientAsync <ImapClient>())
                    using (var file = new System.IO.FileStream(filename, System.IO.FileMode.Create))
                    {
                        await imap.DownloadMessageAsync(file, 0, false);
                    }

                using (var file = new System.IO.FileStream(filename, System.IO.FileMode.Open))
                {
                    var msg = new WinPhone.Mail.Protocols.MailMessage();
                    msg.Load(file, Scope.HeadersAndBody, maxLength: 0);
                    msg.Subject.ShouldNotBeNullOrEmpty();
                }
            }
            finally
            {
                File.Delete(filename);
            }
        }
Exemplo n.º 7
0
 public Task<MailMessage> GetMessageHeadersAsync(string conversationId, string messageId)
 {
     string messageDir = Path.Combine(AccountDir, _accountName, ConversationsDir, conversationId, messageId);
     string headersFile = Path.Combine(messageDir, HeadersFile);
     if (!_storage.DirectoryExists(messageDir) || !_storage.FileExists(headersFile))
     {
         return null;
     }
     Stream stream = _storage.OpenFile(headersFile, FileMode.Open, FileAccess.Read, FileShare.Read);
     using (stream)
     {
         // TODO: Async
         MailMessage message = new MailMessage();
         message.Load(stream, Scope.HeadersAndMime, 0);
         return Task.FromResult(message);
     }
 }
Exemplo n.º 8
0
        public virtual async Task<MailMessage[]> GetMessagesAsync(string start, string end, bool uid, Scope scope, bool setseen)
        {
            var x = new List<MailMessage>();

            await GetMessagesAsync(start, end, uid, scope, setseen,
                async (stream, size, imapHeaders) =>
                {
                    var mail = new MailMessage { Encoding = Encoding };
                    mail.Size = size;

                    if (imapHeaders["UID"] != null)
                        mail.Uid = imapHeaders["UID"];

                    if (imapHeaders["Flags"] != null)
                        mail.SetFlags(imapHeaders["Flags"]);

                    await _Stream.EnsureBufferAsync(mail.Size);
                    mail.Load(_Stream, scope, mail.Size);

                    foreach (var key in imapHeaders.Keys.Except(new[] { "UID", "Flags", "BODY[]", "BODY[HEADER]" }, StringComparer.OrdinalIgnoreCase))
                        mail.Headers.Add(key, new HeaderValue(imapHeaders[key]));

                    x.Add(mail);

                    return mail;
                }
            );

            return x.ToArray();
        }
Exemplo n.º 9
0
        public void SaveAndLoadHeadersOnlyMultipart()
        {
            MailMessage message1 = new MailMessage();
            message1.Scope = Scope.HeadersAndMime;

            message1.Sender = new MailAddress("*****@*****.**");
            message1.From = new MailAddress("*****@*****.**");
            message1.To.Add(new MailAddress("*****@*****.**"));
            message1.Cc.Add(new MailAddress("*****@*****.**"));
            message1.Bcc.Add(new MailAddress("*****@*****.**"));
            message1.Subject = "Subject line";

            message1.ContentType = "multipart/mixed; boundary=abcdefg";
            message1.AlternateViews.Add(new Attachment()
            {
               ContentType = "text/plain; charset=utf-8",
               ContentTransferEncoding = "7BIT",
            });
            message1.Attachments.Add(new Attachment()
            {
                ContentType = "text/plain; charset=utf-8",
                ContentTransferEncoding = "7BIT",
                IsAttachment = true,
            });

            MemoryStream buffer = new MemoryStream();
            TextWriter writer = new StreamWriter(buffer);
            message1.Save(writer);
            writer.Flush();
            buffer.Seek(0, SeekOrigin.Begin);

            MailMessage message2 = new MailMessage();
            message2.Load(buffer, Scope.HeadersAndMime, 0);

            Assert.Equal(message1.Sender.ToString(), message2.Sender.ToString());
            Assert.Equal(message1.From.ToString(), message2.From.ToString());
            Assert.Equal(message1.To.First().ToString(), message2.To.First().ToString());
            Assert.Equal(message1.Cc.First().ToString(), message2.Cc.First().ToString());
            Assert.Equal(message1.Bcc.First().ToString(), message2.Bcc.First().ToString());

            Assert.Equal(message1.Subject, message2.Subject);
            Assert.Equal(message1.ContentType, message2.ContentType);
            Assert.Equal(message1.Body, message2.Body);
            Assert.Equal(message1.AlternateViews.Count, message1.AlternateViews.Count);
            Assert.Equal(message1.Attachments.Count, message1.Attachments.Count);

            Assert.Equal(message1.AlternateViews.First().ContentType, message2.AlternateViews.First().ContentType);
            Assert.Equal(message1.AlternateViews.First().ContentTransferEncoding, message2.AlternateViews.First().ContentTransferEncoding);
            Assert.Equal(message1.AlternateViews.First().IsAttachment, message2.AlternateViews.First().IsAttachment);

            Assert.Equal(message1.Attachments.First().ContentType, message2.Attachments.First().ContentType);
            Assert.Equal(message1.Attachments.First().ContentTransferEncoding, message2.Attachments.First().ContentTransferEncoding);
            Assert.Equal(message1.Attachments.First().IsAttachment, message2.Attachments.First().IsAttachment);
        }
Exemplo n.º 10
0
        public void SaveAndLoadHeadersOnly()
        {
            MailMessage message1 = new MailMessage();
            message1.Scope = Scope.HeadersAndMime;

            message1.Sender = new MailAddress("*****@*****.**");
            message1.From = new MailAddress("*****@*****.**");
            message1.To.Add(new MailAddress("*****@*****.**"));
            message1.Cc.Add(new MailAddress("*****@*****.**"));
            message1.Bcc.Add(new MailAddress("*****@*****.**"));
            message1.Subject = "Subject line";

            message1.ContentType = "text/plain; charset=utf-8";

            MemoryStream buffer = new MemoryStream();
            TextWriter writer = new StreamWriter(buffer);
            message1.Save(writer);
            writer.Flush();
            buffer.Seek(0, SeekOrigin.Begin);

            MailMessage message2 = new MailMessage();
            message2.Load(buffer, Scope.Headers, 0);

            Assert.Equal(message1.Sender.ToString(), message2.Sender.ToString());
            Assert.Equal(message1.From.ToString(), message2.From.ToString());
            Assert.Equal(message1.To.First().ToString(), message2.To.First().ToString());
            Assert.Equal(message1.Cc.First().ToString(), message2.Cc.First().ToString());
            Assert.Equal(message1.Bcc.First().ToString(), message2.Bcc.First().ToString());

            Assert.Equal(message1.Subject, message2.Subject);
            Assert.Equal(message1.ContentType, message2.ContentType);
            Assert.Equal(message1.Body, message2.Body);
            Assert.Equal(message1.AlternateViews.Count, message1.AlternateViews.Count);
            Assert.Equal(message1.Attachments.Count, message1.Attachments.Count);
        }
Exemplo n.º 11
0
        private WinPhone.Mail.Protocols.MailMessage GetMessage(string raw)
        {
            var msg = new WinPhone.Mail.Protocols.MailMessage();
            msg.Load(raw, Scope.HeadersAndBody);

            return msg;
        }