SMTP和计时是否存在已知问题?我在下面添加了一些代码,基本上会向某人发送生日问候,还有一个星座运势和其他分享生日的人(为了好玩)。
我使用Regex替换来从其他方法获取元素,星座和其他人,并将它们插入到一对“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();
}
}
}
{ 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();
}
}
}
我想这是不言而喻的,因为我能收到一封电子邮件,但为了清楚起见,SMTP也没有问题。我完全能够收到电子邮件。问题是它们不包含我的控制台中出现的替换html用于其他测试。
此外,您在此代码中看到的任何其他问题(优化等)都会毫不犹豫地发表评论。我总是愿意学习!感谢
答案 0 :(得分:0)
您正在将字符串html
注入正文。它应该是html2
?