简体   繁体   中英

C# Sending .htm email Template

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?

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

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);
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM