如何使用C#在Windows控制台中使用邮件模板?

发布于 2025-02-10 23:41:43 字数 860 浏览 1 评论 0原文

我创建了一个Windows Console应用程序,并且必须发送电子邮件。我想创建一个邮件模板并在邮件模板中替换一些值。

我创建了一个邮件模板(/mailtemplate/Resignnotify.html)。

如何在Windows Console应用程序中使用以下邮件模板?

我的wistnotify.html,

   <table>
            <tr>
                <td colspan="2">
                    Hi ! ##User## 
                </td>
            </tr>
            
            
            <tr>
                <td>Company:</td>
                <td>##AppCom##</td>
            </tr>
            <tr>
                <td>People:</td>
                <td>##AppUser##</td>
            </tr>
            <tr>
                <td>Date: </td>
                <td>##ResignDate## </td>
            </tr>
        
        </table>

I have created a windows console application, and I have to send an email. I want to create a mail template and replace some value in mail template.

I have created a mail template (/MailTemplate/Resignnotify.html).

How to use mail template as below in windows console application ?

my Resignnotify.html,

   <table>
            <tr>
                <td colspan="2">
                    Hi ! ##User## 
                </td>
            </tr>
            
            
            <tr>
                <td>Company:</td>
                <td>##AppCom##</td>
            </tr>
            <tr>
                <td>People:</td>
                <td>##AppUser##</td>
            </tr>
            <tr>
                <td>Date: </td>
                <td>##ResignDate## </td>
            </tr>
        
        </table>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清音悠歌 2025-02-17 23:41:43

创建一个用于替换HTML文件中标记的值类。

public class TemplateItem
{
    public string User { get; set; }
    public string AppCom { get; set; }
    public List<string> AppUser { get; set; }
    public DateTime ResignDate { get; set; }
}

创建一个用于执行标记/代币替换的类。

public class TemplateOperations
{
    public static string Populate(string fileName, TemplateItem templateItem)
    {
        string contents = File.ReadAllText(fileName);
        
        contents = contents.Replace("##User##", templateItem.User);
        contents = contents.Replace("##AppCom##", templateItem.AppCom);
        contents = contents.Replace("##AppUser##", string.Join(",", templateItem.AppUser));
        contents = contents.Replace("##ResignDate##", templateItem.ResignDate.ToString("d"));

        return contents;

    }
}

在主方法上以上运行

partial class Program
{
    static void Main(string[] args)
    {
        TemplateItem templateItem = new TemplateItem
        {
            AppCom = "ABC",
            AppUser = new List<string>() { "Mike", "Jane", "Fran" },
            ResignDate = DateTime.Now,
            User = "Adam"
        };

        string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, 
            "MailTemplate", "Resignnotify.html");

        var body = TemplateOperations.Populate(fileName, templateItem);

        Console.WriteLine(body);
        Console.ReadLine();
    }
}

“在此处输入图像描述”

Create a class for values for replacing markers in the HTML file.

public class TemplateItem
{
    public string User { get; set; }
    public string AppCom { get; set; }
    public List<string> AppUser { get; set; }
    public DateTime ResignDate { get; set; }
}

Create a class for performing marker/token replacements.

public class TemplateOperations
{
    public static string Populate(string fileName, TemplateItem templateItem)
    {
        string contents = File.ReadAllText(fileName);
        
        contents = contents.Replace("##User##", templateItem.User);
        contents = contents.Replace("##AppCom##", templateItem.AppCom);
        contents = contents.Replace("##AppUser##", string.Join(",", templateItem.AppUser));
        contents = contents.Replace("##ResignDate##", templateItem.ResignDate.ToString("d"));

        return contents;

    }
}

Run above in Main method

partial class Program
{
    static void Main(string[] args)
    {
        TemplateItem templateItem = new TemplateItem
        {
            AppCom = "ABC",
            AppUser = new List<string>() { "Mike", "Jane", "Fran" },
            ResignDate = DateTime.Now,
            User = "Adam"
        };

        string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, 
            "MailTemplate", "Resignnotify.html");

        var body = TemplateOperations.Populate(fileName, templateItem);

        Console.WriteLine(body);
        Console.ReadLine();
    }
}

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文