0

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);
}
}
}
}
2
  • You could certainly read the contents of an external file into a string variable and then set mail.Body = fileContents; Commented Jul 11, 2012 at 18:18
  • how do I do that? Im new Commented Jul 12, 2012 at 2:25

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Even if you're using ASP.Net Webforms (yuck!), Postal is still useful there. Highly recommended. Reference: stackoverflow.com/questions/11368677/…
I thought it might be, but haven't done Webforms in year and years :-) Thanks for sharing that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.