I'm attempting to insert a person's name into the body of an email as dynamic values using string interpolation in c# 6.0.
Proof of Concept
public class Program
{
public static void Main()
{
var p = new Person { FirstName = "John", LastName = "Smith" };
var msg = $"{p.FirstName} says 'Hello World!'";
Console.WriteLine($"Message: {msg}");
}
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
This proof of concept outputs the string:
Message: John says 'Hello World!'
Implementation
So now when I'm trying to use the concept to build my MailMessage, I'm getting {Contact.FirstName} output in my email. Here's the code:
private string _Body;
public string Body
{
get { return _Body; }
set
{
_Body = value;
RaisePropertyChangedEvent("Body");
}
}
var Contacts = Context.Contacts
.Where(x => x.ContactGroupId == ContactGroupId && x.Deleted == false);
// By this point, Body will already have a value containing the interpolation.
// For example: Body = "Message: {Contact.Name} says \"Hello World!\"";
foreach (var Contact in Contacts)
{
MailMessage Msg = new MailMessage();
MailMessage.Body = ${Body}
}
Problem
This should render the body to read as:
Message: John says "Hello World!"
Instead, what renders is:
Message: {Contact.FirstName} says "Hello World!"
How can I interpolate my variables into the string if I'm not actually hard coding the string in my code?
MailMessage.Body = $"{Body}"...