Home > database >  How to solve the error in the below email content in C#
How to solve the error in the below email content in C#

Time:02-21

I am using C# MVC Architecture.

Brief Introduction about the function that I have implemented.

A Recruiter can post job adverts by logging into their accounts.Once a job is posted,that particular job will be listed under pending confirmation.An admin can view the confirmation pending job adverts from the admin panel.They can view it and either accept it or reject it or edit and accept it.So when an admin accepts the job advert,that particular job advert will go live in the website.And also an email will be sent to that particular recruiter indicating that their job vacancy has been published on the website along with the link.

So when a recruiter is posting a job advert,they can include the basic job details and also there is a feature that they can add screening questions for applicants to answer when they submit their CV.So when an admin is checking for confirmation pending jobs adverts ,they view these screening questions and sometimes they edit some questions and accept the advert.

Problem

Along with the email sent when an admin accepts the job advert,I want to add the list of the screening questions.

I am going to retrieve the questions from the database and display in the email content as a list of questions.

Below is the method of the email template.

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

Here for the List of questions, I get the questions as below.

enter image description here

enter image description here

I want to display the 'Question' in the second image as list of questions(In the above case as the count is 7,want to display the 7 questions) with numbering starting from 1.(Number of questions may differ according to the advert.) as a list in the email content.

Every other details(jobTitle,JobLink,RecruiterEmail etc..) are displayed in the email. Only the 'ques' under Questions Posted: in the email content are displayed as

enter image description here

I want the content to be displayed as,

1.Question 1

2.Question 3

3.Question 3 ...

How can I solve this?

CodePudding user response:

You need to "unfold" the list of questions, if you do that, you'll have all the freedom you need.

In stead of:

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

use something like this:

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

Or for better readability:

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

Further optimization: use a 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());

Addition: for numbering you have various options. Here's a simple one to understand:

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());
  • Related