0

SMTP 和时间是否存在已知问题?我在下面包含了一些代码,它们基本上向某人发送生日祝福,以及星座和其他分享生日的人(为了好玩)。

我使用正则表达式替换从其他方法获取元素、星座和其他人,并将它们插入到一对“p”标签之间的 HTML 文档中。我很肯定这些方法有效,并且我很肯定 HTML 文档正在被正确地重新格式化(根据控制台,它在我的另一个测试中打印出我的新 HTML 文档)。我在想,也许我的问题是在更换有机会发生之前发送了电子邮件,但我很容易偏离基地。如果有人以前遇到过这个问题或知道它的原因,我将非常感激。TIA。

public void formatHTMLTest()

    { using (StreamReader reader = File.OpenText("../../BirthdayMessage.htm"))

        {
            //Format the body
            //Format the Famous People
            string html = reader.ReadToEnd();

            string replacePattern1 = "<!--INJECT FAMOUS PEOPLE HERE-->";
            List<string> famousPeople = new List<string>();

            famousPeople.Add("test1");
            famousPeople.Add("test2");
            famousPeople.Add("test3");

            StringBuilder famousSB = new StringBuilder();
            foreach (string person in famousPeople)
            {
                famousSB.Append(person + ", ");
            }
            int length = famousSB.Length;

            famousSB.Remove(length - 2, 2);
            string famousString = famousSB.ToString();

            string html1 = Regex.Replace(html, replacePattern1, famousString);
            //Format the Horoscope
            string horoscope = "FOO.";

            string replacePattern2 = "<!--INJECT HOROSCOPE HERE-->";
            string html2 = Regex.Replace(html1, replacePattern2, horoscope);


            //Configuring the SMTP client
            SmtpClient smtp = new SmtpClient();
            smtp.Port = 25;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential("barfoo@sender.com", "senderPass");
            smtp.Host = "mysmtp";

            //Set up email that is sent to the client
            MailMessage clientEmail = new MailMessage();
            clientEmail.From = new System.Net.Mail.MailAddress("barfoo@sender.com");
            clientEmail.To.Add(new MailAddress("foobar@recipient.com"));
            clientEmail.Subject = "Happy Birthday!";
            clientEmail.IsBodyHtml = true;
            clientEmail.Body = html;

            //using (smtp as IDisposable)
            {
                smtp.Send(clientEmail);
                clientEmail.Dispose();
            }
        }
    }

I guess this goes without saying since I was able to get an e-mail, but just for clarity's sake, the SMTP has no issues also. I am fully able to receieve e-mails. The issue is that they do not contain the replaced html that appears in my console for other tests.

Also, any other things you see wrong with this code (optimizations, etc), don't hesitate to comment. I'm always willing to learn! Thanks

4

1 回答 1

0

You are injecting the string html into the body. It should be html2?

于 2013-06-27T18:02:21.103 回答