Exemplo n.º 1
0
        public void TestTwo()
        {
            EmailMessage emailmessage = new EmailMessage();

            emailmessage.FromAddress = TestAddressHelper.GetFromAddress();

            emailmessage.AddToAddress(TestAddressHelper.GetToAddress());

            emailmessage.Subject = "A photo of hawthornes";

            emailmessage.TextPart = new TextAttachment("This photo requires a better email reader.");
            emailmessage.HtmlPart = new HtmlAttachment("<html><body>" +
                                                       "<p>Note to self: look at this photo again in 30 years.</p>" +
                                                       "<p><img src=\"cid:hawthornes\" alt=\"Hawthorne bush\"/></p>" +
                                                       "<p>-Marcel</p>");

            FileInfo relatedfileinfo = new FileInfo(@"..\..\TestFiles\grover.jpg");

            FileAttachment relatedfileattachment = new FileAttachment(relatedfileinfo, "hawthornes");

            relatedfileattachment.ContentType = "image/jpeg";

            emailmessage.AddRelatedAttachment(relatedfileattachment);

            SmtpServer smtpserver = TestAddressHelper.GetSmtpServer();

            emailmessage.Send(smtpserver);
            //Assert.IsNull(smtpserver.GetSmtpConversation());
        }
Exemplo n.º 2
0
        public void TestRelatedAttachments()
        {
            EmailMessage emailmessage = new EmailMessage();

            emailmessage.FromAddress = TestAddressHelper.GetFromAddress();
            emailmessage.AddToAddress(TestAddressHelper.GetToAddress());
            emailmessage.Subject  = "EmailMessageTests Test Related Graphic";
            emailmessage.TextPart = new TextAttachment("This\r\nis the\r\ntext\r\npart.");
            emailmessage.HtmlPart = new HtmlAttachment("<html><body>This<br>\r\nis the<br><img src=\"cid:mycontentid\">\r\n<strong>HTML</strong><br>\r\npart.</body></html>");
            FileInfo relatedfileinfo = new FileInfo(@"..\..\TestFiles\grover.jpg");

            Assert.IsTrue(relatedfileinfo.Exists);
            FileAttachment relatedfileattachment = new FileAttachment(relatedfileinfo, "mycontentid");

            relatedfileattachment.ContentType = "image/jpeg";
            emailmessage.AddRelatedAttachment(relatedfileattachment);
            //emailmessage.Send(_smtpserver);
            String       content = emailmessage.ToDataString();
            StringReader sr      = new StringReader(content);

            int    i                       = 0;
            String line                    = null;
            bool   hasMimeVersion          = false;
            bool   hasMixedHeader          = false;
            bool   relatedHeaderComesFirst = false;
            bool   hasMultipartAlternative = false;
            bool   hasPlainText            = false;
            bool   hasHtmlText             = false;
            bool   hasAttachment           = false;
            bool   hasRelatedHeader        = false;
            int    quotedPrintableParts    = 0;

            String expectedToAddress = TestAddressHelper.GetToAddress().Name + " <" + TestAddressHelper.GetToAddress().Email + ">";

            //log.Debug("To Address is "+expectedToAddress);
            while ((line = sr.ReadLine()) != null)
            {
                i++;
                if (line.IndexOf("Content-Type: multipart/mixed") == 0)
                {
                    hasMixedHeader = true;
                }

                if (line.IndexOf("MIME-Version: 1.0") == 0)
                {
                    hasMimeVersion = true;
                }
                if (line.IndexOf("Content-Type: multipart/alternative;") == 0)
                {
                    hasMultipartAlternative = true;
                }
                if (line.IndexOf("Content-Type: text/html") == 0)
                {
                    hasHtmlText = true;
                }
                if (line.IndexOf("Content-Type: text/plain") == 0)
                {
                    hasPlainText = true;
                }
                if (line.IndexOf("Content-Type: image/jpeg") == 0)
                {
                    hasAttachment = true;
                }
                if (line.IndexOf("Content-Transfer-Encoding: quoted-printable") == 0)
                {
                    quotedPrintableParts++;
                }
                if (line.IndexOf("/9j/4AAQSkZJRgABAQEASABIAAD/") == 0)
                {
                    hasAttachment = true;
                }
                if (line.IndexOf("Content-Type: multipart/related") >= 0)
                {
                    hasRelatedHeader = true;
                    if (!hasMultipartAlternative && !hasMixedHeader)
                    {
                        relatedHeaderComesFirst = true;
                    }
                }
                //log.Debug("Line "+i+": "+line);
            }
            Assert.IsFalse(hasMixedHeader, "Should not have a  multipart/mixed header");
            Assert.IsTrue(hasMimeVersion, "Missing Mime Version header");
            Assert.IsTrue(relatedHeaderComesFirst, "The related header should come first");
            Assert.IsTrue(hasMultipartAlternative, "Missing Mime Multipart/Alternative setting");
            Assert.IsTrue(hasPlainText, "Missing Plain Text");
            Assert.IsTrue(hasHtmlText, "Missing HTML");
            Assert.IsTrue(hasAttachment, "Missing the base64 Attachment");
            Assert.IsTrue(hasRelatedHeader, "Missing the related header.");
            Assert.AreEqual(2, quotedPrintableParts, "Expected 2 quoted printable declarations, found " + quotedPrintableParts);
        }
Exemplo n.º 3
0
        public static EmailMessage CreateEmbeddedEmail(string body, int newsletterId)
        {
            EmailMessage message = new EmailMessage();

            Hashtable addedAtt = new Hashtable();

            body = template.ParseInternalLinks(body);

            //string currentDomain = "http://" + System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
            //string pattern = "href=\"?([^\\\"' >]+)|src=\\\"?([^\\\"' >]+)";

            string currentDomain = "http://" + System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
            string pattern       = "href=\"?([^\\\"' >]+)|src=\\\"?([^\\\"' >]+)|background=\\\"?([^\\\"' >]+)";

            string          appendNewsletter = "umbNl=" + newsletterId.ToString();
            MatchCollection tags             = Regex.Matches(body, pattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

            foreach (Match tag in tags)
            {
                if (tag.Groups.Count > 0)
                {
                    if (tag.Groups[1].Value.ToLower().IndexOf("http://") == -1 &&
                        tag.Groups[2].Value.ToLower().IndexOf("http://") == -1 &&
                        tag.Groups[1].Value.ToLower().IndexOf("mailto:") == -1 &&
                        tag.Groups[2].Value.ToLower().IndexOf("mailto:") == -1)
                    {
                        // links
                        if (tag.Groups[1].Value != "")
                        {
                            if (tag.Groups[0].Value.ToLower() == "href=\"/")
                            {
                                if (tag.Groups[1].Value.IndexOf("?") == -1)
                                {
                                    body = body.Replace(tag.Groups[0].Value + "\"", "href=\"" + currentDomain + tag.Groups[1].Value + "?" + appendNewsletter + "\"");
                                }
                                else
                                {
                                    body = body.Replace(tag.Groups[0].Value + "\"", "href=\"" + currentDomain + tag.Groups[1].Value + "&" + appendNewsletter + "\"");
                                }
                            }
                            else
                            {
                                if (tag.Groups[1].Value.IndexOf("?") == -1)
                                {
                                    body = body.Replace("href=\"" + tag.Groups[1].Value + "\"", "href=\"" + currentDomain + tag.Groups[1].Value + "?" + appendNewsletter + "\"");
                                }
                                else
                                {
                                    body = body.Replace("href=\"" + tag.Groups[1].Value + "\"", "href=\"" + currentDomain + tag.Groups[1].Value + "&" + appendNewsletter + "\"");
                                }
                            }
                        }
                        // src
                        else
                        {
                            string imageExtextions = "jpg,jpeg,gif,png";
                            string image           = tag.Groups[2].Value;
                            if (image == "")
                            {
                                image = tag.Groups[3].Value;
                            }
                            string orgImage = image;

                            string ext = image.Split(char.Parse("."))[image.Split(char.Parse(".")).Length - 1].ToLower();

                            bool isImage = imageExtextions.IndexOf(ext) != -1;

                            if (isImage)
                            {
                                string         guid       = Guid.NewGuid().ToString();
                                FileAttachment attachment = CreateImageAttachment(image, ext, guid);
                                if (attachment != null)
                                {
                                    if (addedAtt.ContainsKey(image))
                                    {
                                        body = body.Replace(image, "cid:" + addedAtt[image].ToString());
                                    }
                                    else
                                    {
                                        message.AddRelatedAttachment(attachment);
                                        body = body.Replace(image, "cid:" + guid);
                                        addedAtt.Add(image, guid);
                                    }
                                }
                                else
                                {
                                    body = body.Replace(orgImage, currentDomain + tag.Groups[2].Value);
                                }
                                // break;
                            }
                            else
                            {
                                body = body.Replace(orgImage, currentDomain + tag.Groups[2].Value);
                            }
                        }
                    }
                }
            }

            message.HtmlPart = new HtmlAttachment(body);

            return(message);
        }