public MailboxResponse Get(Mailbox request) { var mailboxPath = new DirectoryInfo(Path.Combine(this.Config.MailFolder, request.Name)); ValidateExists(request.Name, mailboxPath); string[] emails = Directory.GetFiles(mailboxPath.FullName, "*.eml"); var response = new MailboxResponse() { Name = request.Name, Links = new List<Link>(new[] { this.GetMailboxLink(request.Name) }) }; foreach (var entry in emails) { var mailMessage = GetMailMessage(entry); response.Emails.Add(new EmailResponse() { Body = mailMessage.Body, Subject = mailMessage.Subject, To = mailMessage.To.Select(m => m.Address).ToList(), From = mailMessage.From.Address, Date = mailMessage.DeliveryDate, Links = new List<Link>(new [] { this.GetEmailLink(request.Name, entry) }) }); } return response; }
public void Can_Get_to_retrieve_mailbox() { var restClient = this.CreateRestClient(); MailboxResponse response = restClient.Get(new MailboxRequest()); Assert.That(response.Emails, Is.Not.Null); Assert.That(response.Emails.Count, Is.GreaterThan(0)); Assert.That(response.Emails[0].Body, Is.Not.Null, "Email body was null"); }
public MailboxResponse Get(MailboxRequest request) { var mailboxPath = new DirectoryInfo(this.Config.MailFolder); request.Page = request.Page == 0 ? 1 : request.Page; var emails = mailboxPath.GetFiles("*.eml"); var numPages = (int)Math.Ceiling(emails.Count() / ((double)Config.EmailsPerPage)); var emailsToRead = emails .OrderByDescending(x => x.LastWriteTimeUtc) .Skip(Config.EmailsPerPage * (request.Page - 1)) .Take(Config.EmailsPerPage) .Select(x => x.FullName); var response = new MailboxResponse() { Links = new List <Link>(new[] { this.GetNextPageLink(request.Page, numPages), this.GetPreviousPageLink(request.Page) }), Page = request.Page, Pages = numPages }; foreach (var entry in emailsToRead) { try { var mailMessage = GetMailMessage(entry); response.Emails.Add(new EmailResponse() { Id = Path.GetFileNameWithoutExtension(entry), Body = mailMessage.Body, Subject = mailMessage.Subject, To = mailMessage.To.Select(m => m.Address).ToList(), From = mailMessage.From.Address, Date = mailMessage.DeliveryDate, Links = new List <Link>(new[] { this.GetEmailLink(entry) }) }); } catch (Exception e) { response.Emails.Add(new EmailResponse() { Id = Path.GetFileNameWithoutExtension(entry), Body = e.Message, Subject = "[Error reading email]", From = "-", To = new List <string>() }); } } return(response); }