0

所以我正在尝试清理后端并美化我的电子邮件。有没有办法包含一个 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);
}
}
}
}
4

1 回答 1

2

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

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

于 2012-07-11T18:20:07.710 回答