Reputation: 350
So I am trying to clean up the back end and pretty up my emails. Is there a way to have a include to a html file instead of putting the html in here directly? or maybe a better way to do it?
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("[email protected]");
mail.To.Add(new MailAddress(emailAddress));
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("myemail", "mypass"),
EnableSsl = true
};
client.Send(mail);
}
}
}
}
Upvotes: 0
Views: 288
Reputation: 150108
If you are using ASP.Net MVC there are a few really nice solutions to write your email as a view, which allows for easy injection of elements of the model (such as the person's name):
If you are not using MVC, you can certainly read an HTML file from the file system, or read HTML from a database (much easier to maintain if you have more than a few emails).
Upvotes: 2