You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何向HTML邮件模板中动态追加用户ID与密码

Insert Dynamic Content into HTML Email Template's Span Tag

Let's break down how to get your dynamic emailData into the span tag of your HTML template. Here are two straightforward approaches depending on whether you can modify the template or not:

Approach 1: Use a Placeholder in the HTML Template (Simplest)

If you can edit your HTML template, adding a clear placeholder inside the target span makes replacing content trivial.

Step 1: Update Your HTML Template

Add a unique placeholder (like [USER_LOGIN_DETAILS]) inside the span where you want the content to appear:

<!-- Example HTML Template -->
<html>
  <body>
    <div class="email-container">
      <p>Here are your login details:</p>
      <!-- Target span with placeholder -->
      <span class="login-info">[USER_LOGIN_DETAILS]</span>
    </div>
  </body>
</html>

Step 2: Generate Dynamic Content and Replace Placeholder

Use string replacement to swap the placeholder with your emailData. Also, switch to string interpolation instead of concatenation for cleaner, more readable code:

// Your user and password variables
string userName = "Debasis"; // Replace with actual UserName value
string password = "AutoGen_789"; // Replace with your generated Password

// Clean up content creation with interpolation
string emailData = $"Hi {userName}, Please use this {password} as your password to login to the Application.";

// Load the HTML template (read from file or use an embedded resource)
string htmlTemplate = File.ReadAllText(@"C:\path\to\your\email-template.html");

// Replace the placeholder in the span with your dynamic content
string finalEmailHtml = htmlTemplate.Replace("[USER_LOGIN_DETAILS]", emailData);

// finalEmailHtml now has your content inserted into the span!

Approach 2: Modify the Span Directly with HTML Parsing (No Template Edits)

If you can't modify the original HTML template, use an HTML parsing library like HtmlAgilityPack to target the span by its ID or class and inject the content.

Step 1: Install HtmlAgilityPack

First, add the package via NuGet:

Install-Package HtmlAgilityPack

Step 2: Code to Inject Content

using HtmlAgilityPack;

// Your dynamic content
string userName = "Debasis";
string password = "AutoGen_789";
string emailData = $"Hi {userName}, Please use this {password} as your password to login to the Application.";

// Load the HTML template
HtmlDocument doc = new HtmlDocument();
doc.Load(@"C:\path\to\your\email-template.html");

// Find the target span (adjust the XPath to match your span's ID/class)
HtmlNode spanNode = doc.DocumentNode.SelectSingleNode("//span[@id='login-span']"); // Replace with your span's actual ID

if (spanNode != null)
{
    // Replace the span's inner text with your emailData
    spanNode.InnerHtml = emailData;
}

// Get the final HTML string
string finalEmailHtml = doc.DocumentNode.OuterHtml;

Quick Tips:

  • Always sanitize user input if UserName comes from user-provided data to prevent XSS risks (even email clients can execute malicious scripts in some cases).
  • String interpolation ($"") is far less error-prone than string concatenation with +—stick with it for dynamic text!

内容的提问来源于stack exchange,提问作者Debasis Mund

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 20:57:57