Beispiel #1
0
        public static IList <ReadOnlyUrl> GetImageLinks(this MockEmailView view)
        {
            var htmlDoc = new HtmlDocument();

            htmlDoc.LoadHtml(view.Body);
            return((from a in htmlDoc.DocumentNode.SelectNodes("//img")
                    select new ReadOnlyUrl(a.Attributes["src"].Value)).ToList());
        }
Beispiel #2
0
        public static IList <ReadOnlyUrl> GetLinks(this MockEmailView view)
        {
            var htmlDoc = new HtmlDocument();

            htmlDoc.LoadHtml(view.Body);
            return((from a in htmlDoc.DocumentNode.SelectNodes("//a")
                    where !a.Attributes["href"].Value.StartsWith("mailto:")
                    select new ReadOnlyUrl(a.Attributes["href"].Value)).ToList());
        }
Beispiel #3
0
        public static IList <ReadOnlyUrl> GetPlainTextLinks(this MockEmailView view)
        {
            // Need to parse out directly.

            var list = new List <ReadOnlyUrl>();

            var body = view.Body;
            var pos  = body.IndexOf("http", 0);

            while (pos != -1)
            {
                var endPos = body.IndexOfAny(new[] { ' ', '\r', '\n', '\t' }, pos);
                var link   = endPos == -1 ? body.Substring(pos) : body.Substring(pos, endPos - pos);
                list.Add(new ReadOnlyUrl(link));

                pos = endPos == -1 ? -1 : body.IndexOf("http", endPos);
            }

            return(list);
        }
Beispiel #4
0
        internal MockEmail(MailMessage message)
        {
            // Addresses

            _to = new List <EmailRecipient>();
            foreach (var address in message.To)
            {
                _to.Add(new EmailRecipient(address.Address, address.DisplayName));
            }

            _from = new EmailRecipient(message.From.Address, message.From.DisplayName);

            _cc = new List <EmailRecipient>();
            foreach (var address in message.CC)
            {
                _cc.Add(new EmailRecipient(address.Address, address.DisplayName));
            }

            _bcc = new List <EmailRecipient>();
            foreach (var address in message.Bcc)
            {
                _bcc.Add(new EmailRecipient(address.Address, address.DisplayName));
            }

            if (message.Sender != null)
            {
                _sender = new EmailRecipient(message.Sender.Address, message.Sender.DisplayName);
            }
            if (message.ReplyToList.Count > 0)
            {
                _replyTo = new EmailRecipient(message.ReplyToList[0].Address, message.ReplyToList[0].DisplayName);
            }

            // Subject.

            _subjectEncoding = message.SubjectEncoding;
            _subject         = message.Subject;

            // Body.

            _isBodyHtml   = message.IsBodyHtml;
            _bodyEncoding = message.BodyEncoding;
            _body         = message.Body;

            // Alternate views.

            _alternateViews = new List <MockEmailView>();
            foreach (var alternateView in message.AlternateViews)
            {
                var           reader = new StreamReader(alternateView.ContentStream);
                MockEmailView view;

                if (alternateView.LinkedResources.Count == 0)
                {
                    view = new MockEmailView {
                        Body = reader.ReadToEnd(), MediaType = alternateView.ContentType.MediaType
                    };
                }
                else
                {
                    var resources = new List <MockEmailResource>();
                    foreach (var linkedResource in alternateView.LinkedResources)
                    {
                        resources.Add(new MockEmailResource {
                            ContentId = linkedResource.ContentId, MediaType = linkedResource.ContentType.MediaType
                        });
                    }
                    view = new MockEmailView {
                        Body = reader.ReadToEnd(), MediaType = alternateView.ContentType.MediaType, Resources = resources
                    };
                }

                _alternateViews.Add(view);
            }

            // Attachments.

            _attachments = new List <MockEmailAttachment>();
            foreach (var attachment in message.Attachments)
            {
                _attachments.Add(new MockEmailAttachment {
                    Name = attachment.Name, MediaType = attachment.ContentType.MediaType
                });
            }
        }