简体   繁体   中英

How to send an email using a template?

I'm using this code in c# to send an email. I want this email to be based on a template. this email contains images and styles. I wanna change certain contents of the email( for example, names, links etc.). Any ideas how to go about it? The code is below,

private void button1_Click(object sender, EventArgs e)
{


// Create outlook application object.
var outlookApplication = new Microsoft.Office.Interop.Outlook.Application();

// Create mail message.
var newMail = (Microsoft.Office.Interop.Outlook.MailItem)outlookApplication.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
newMail.To = "example@exam.com";
newMail.Subject = "Example";
newMail.SentOnBehalfOfName = "team@iny.co.uk";
newMail.Attachments.Add(@"c:\New\DebriefReportTemplate.docx");

newMail.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
newMail.HTMLBody = "<p>Dear Example,</p><p>Example example.</p>";
newMail.Display(true);

}

Razor Engine is recommended to you, http://razorengine.codeplex.com/ . It is easy to use and the syntax is same as asp.net mvc 3 razor

如果您在电子邮件模板中的关键字是固定的,则可以使用Sting.Replace(“ Example”,“ XYZ”)并获得所需的结果

The simplest solution is to use String.Format

wMail.HTMLBody = string.Format("<p>Dear {0},</p><p>{1}</p>", name, message);

If you need something more complex, you should use a template engine like Castle Velocity or Razor as @allentranks suggested.

您可以创建带有格式和标签的xsl文件。将数据传递到xsl并将其转换为html格式。

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