在C#中如何解决以下电子邮件内容中的错误

How to solve the error in the below email content in C#

我正在使用 C# MVC 架构。

我要从数据库中检索问题并在电子邮件内容中显示为问题列表。

下面是邮件模板的方法。

 public bool SendJobAcceptanceToRecruiter(string recruiterName, string recruiterEmail, string jobTitle,string joblink, string mailBody, string organization, List<JobQuestion> ques)
    {
        string subject = "Job Advert Accepted - " + jobTitle + "-" + organization;
        var generaltemplate = GetEmailTemplate("GENERAL EMAIL TEMPLATE");
        var template = "<br/>Hello " + organization + "<br/>" +
        "<br/>Your Job Advert has now been accepted.<br/>" +
        "<br/>View Job Posted: " + joblink + "<br/>" +
         "<br/>Questions Posted: " + ques + "<br/>" +
        "<br/>Please contact us if you need more information.<br/>";
        var body = generaltemplate.Replace("@Content", template);
        body = body.Replace("@Orgname", organization);        
        body = body.Replace("@JobLink", joblink) + GetEmailFooter();
        var result = _emailProvider.SendEmail(recruiterEmail, subject, body, true);     
        return result;
    }

这里是问题列表,我得到的问题如下。

我想在第二张图片中显示'Question'作为问题列表(在上面的例子中,因为计数是7,所以想显示7个问题)从1开始编号。(数量问题可能因广告而异。)作为电子邮件内容中的列表。

所有其他详细信息(职位名称、工作链接、招聘人员电子邮件等)都显示在电子邮件中。 只有 Questions Posted: 下的 'ques' 在电子邮件内容中显示为

[![在此处输入图片描述][3]][3]

我希望内容显示为,

1.Question 1

2.Question 3

3.Question 3 ...

我该如何解决这个问题?

您需要“展开”问题列表,如果您这样做,您将拥有所需的所有自由。

而不是:

"<br/>Questions Posted: " + ques + "<br/>"

使用这样的东西:

"<br/>Questions Posted: " + string.Join(", ",Enumerable.Range(0, ques.Count()).Select(n => n.Description).ToArray()) + "<br/>"

或者为了更好的可读性:

var template = "<br/>Hello " + organization + "<br/>" +
"<br/>Your Job Advert has now been accepted.<br/>" +
"<br/>View Job Posted: " + joblink + "<br/>" +
"<br/>Questions Posted: ";

foreach(var question in ques)
    template += $"somthing {question.Description} something"

template += "<br/>Please contact us if you need more information.<br/>";

var body = generaltemplate.Replace("@Content", template);

进一步优化:使用StringBuilder:

var sb = new StringBuilder();
sb.AppendLine("<br/>Hello " + organization + "<br/>");
sb.AppendLine("<br/>Your Job Advert has now been accepted.<br/>");
sb.AppendLine("<br/>View Job Posted: " + joblink + "<br/>");
sb.AppendLine("<br/>Questions Posted: ");

foreach(var question in ques)
{
    sb.AppendLine($"somthing {question.Description} something");
}

sb.AppendLine("<br/>Please contact us if you need more information.<br/>");

var body = generaltemplate.Replace("@Content", sb.ToString());

补充:对于编号,您有多种选择。这里简单理解一下:

var sb = new StringBuilder();
sb.AppendLine("<br/>Hello " + organization + "<br/>");
sb.AppendLine("<br/>Your Job Advert has now been accepted.<br/>");
sb.AppendLine("<br/>View Job Posted: " + joblink + "<br/>");
sb.AppendLine("<br/>Questions Posted: ");

int number = 0;
foreach(var question in ques)
{
    sb.AppendLine($"QUESTION {++number}");
    sb.AppendLine($"somthing {question.Description} something");
}

sb.AppendLine("<br/>Please contact us if you need more information.<br/>");

var body = generaltemplate.Replace("@Content", sb.ToString());