Reputation: 381
Using the simple SMTP C# code below to send an email, how can i send an email template?
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(toEmailAddress);
message.Subject = "subject";
message.From = new System.Net.Mail.MailAddress(from);
message.Body = "http://www.yoursite.com/email.htm";
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("server");
smtp.Send(message);
Currently, as expected the received email just contains the URL for the template. how can i get it to send the template?
Upvotes: 0
Views: 3955
Reputation: 1231
If the file is local, instead of using a download, you can simply read it in using System.IO, such as
string html;
System.IO.StreamReader fstream;
fstream = File.OpenText("yourpathgoeshere.html");
html = fstream.ReadToEnd();
fstream.Close();
after this, just assign the rest of the properties as suggested in the other posts. If the html file you're after is stored locally, this is probably better, or if it will be accessed frequently, it may be a better idea to store it locally and use this method.
note, you will need to import System.IO for this to work correctly.
Upvotes: 0
Reputation: 14521
Your question is actually about reading a string from url and one of the possible answers is:
var url = "http://www.yoursite.com/email.htm";
var body = "";
using(var client = new WebClient()) {
body = client.DownloadString(url);
}
Upvotes: 0
Reputation: 1425
System.Net.WebClient client = new System.Net.WebClient();
string html = client.DownloadString("http://www.yoursite.com/email.htm");
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(toEmailAddress);
message.Subject = "subject";
message.From = new System.Net.Mail.MailAddress(from);
message.Body = html;
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("server");
smtp.Send(message);
Upvotes: 2