【问题标题】:Include HTML page in .cs c# .net在 .cs c# .net 中包含 HTML 页面
【发布时间】:2023-03-05 19:16:02
【问题描述】:

所以我正在尝试清理后端并美化我的电子邮件。有没有办法包含一个 html 文件而不是直接把 html 放在这里?或者是更好的方法?

public void SendWelcomeEmail(int uid)
{
SqlCommand command = EmailCommandList.GetRegisteredEmail;
BciDatabase db = new BciDatabase("db");
command.Parameters["@User_Id"].Value = uid;
using (SqlDataReader dr = db.ExecuteReader(command))
{
Member member = new Member();
if (dr != null && !dr.IsClosed && dr.Read())
{
while (dr.Read())
 {
string emailAddress = (string)dr["Email"];
MailMessage mail = new MailMessage();
mail.Subject = "Welcome to the site";
 mail.Body = "<html><head></head><body><p>Hello, welcome to the site!</body></html>";
 mail.IsBodyHtml = true;
mail.From = new MailAddress("noreply@email.com");
mail.To.Add(new MailAddress(emailAddress));
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("myemail", "mypass"),
EnableSsl = true
};
client.Send(mail);
}
}
}
}

【问题讨论】:

  • 你当然可以将外部文件的内容读入字符串变量,然后设置mail.Body = fileContents;
  • 我该怎么做?我是新人

标签: c# html email


【解决方案1】:

如果您使用的是 ASP.Net MVC,有一些非常好的解决方案可以将您的电子邮件写成视图,这样可以轻松注入模型的元素(例如人名):

如果您不使用 MVC,您当然可以从文件系统中读取 HTML 文件,或者从数据库中读取 HTML(如果您有多个电子邮件,则更容易维护)。

【讨论】:

  • 即使您使用的是 ASP.Net Webforms(糟糕!),Postal 仍然有用。强烈推荐。参考:stackoverflow.com/questions/11368677/…
  • 我认为可能是这样,但多年来没有做过 Webforms :-) 感谢分享。
猜你喜欢
  • 2014-05-11
  • 1970-01-01
  • 2014-10-19
  • 1970-01-01
  • 2015-10-30
  • 1970-01-01
  • 1970-01-01
  • 2011-03-07
  • 1970-01-01
相关资源
最近更新 更多