This question already has an answer here:
- C# Reflection and Getting Properties 1 answer
I've been working on a system to send out HTML emails, where I use Regex to read through a HTML template and replace words between two $ with the corresponding string from the dictionary.
I've tried searching Google for a solution to my problem, but can't seem to implement them correctly.
First of all you have the Data that will be sent out.
public class SubscriptionMessageData : IComparable , IMailData
{
public string DetailsURL { get; private set; }
public Guid ObjectID { get; private set; }
public MarketUniverseEnum? Market { get; private set; }
public string Subject { get; private set; }
public DateTime MessageTime { get; private set; }
public string NewsTitle { get; private set; }
public SubscriptionMessageData(string detailsURL, Guid objectID, MarketUniverseEnum? market, string newsTitle, DateTime messageTime)
{
this.DetailsURL = detailsURL;
this.ObjectID = objectID;
this.Market = market;
this.Subject = "Subject";
this.NewsTitle = newsTitle;
this.MessageTime = messageTime;
}
public Dictionary<string, string> getMergeValues()
{
return new Dictionary<string, string>()
{
{"News_Day", this.MessageTime.ToShortDateString()},
{"News_Subject", this.NewsTitle},
{"News_Url", this.DetailsURL},
{"Company_Link", "Link"},
{"Company_Subscription", "Link"},
{"Company_Logo", "Link_To_Logo"}
};
}
}
This code is then run through an "Email Generator" which as I mentioned above, runs through the template and replaces the context with the results and then creates the function to send an email.
public interface IMailData
{
string Subject { get; }
Dictionary<string, string> getMergeValues();
}
public interface IEmailGenerator
{
MailMessage generateEmail(IMailData mailData, string htmlTemplate, string textTemplate);
}
public class EmailGenerator : IEmailGenerator, IRegisterInIoC
{
// Setup the rules
static readonly Regex emailRegex = new Regex(@"\$([\w\-\,\.]+)\$", RegexOptions.Compiled);
private string mergeTemplate(string template, IReadOnlyDictionary<string, string> values)
{
string emailTextData = emailRegex.Replace(template, match => values[match.Groups[1].Value]);
return emailTextData;
}
public MailMessage generateEmail(IMailData mailData, string htmlTemplate, string textTemplate)
{
var mailMessage = new MailMessage();
mailMessage.IsBodyHtml = true;
mailMessage.Subject = mailData.Subject;
mailMessage.BodyEncoding = Encoding.UTF8;
// Create the Plain Text version of the email
mailMessage.Body = this.mergeTemplate(textTemplate, mailData.getMergeValues());
// Create the HTML version of the email
ContentType mimeType = new System.Net.Mime.ContentType("text/html");
AlternateView alternate = AlternateView.CreateAlternateViewFromString(this.mergeTemplate(htmlTemplate, mailData.getMergeValues()), mimeType);
mailMessage.AlternateViews.Add(alternate);
return mailMessage;
}
}
I want to run reflection on the SubscriptionMessageData class and do a foreach loop that prints the key and value of the class. Ex. Key = DetailsURL, Value = detailsUrl. So, that I then have a $DetailsURL$ in the template that is replaced with detailsURL, which is a value.
Aucun commentaire:
Enregistrer un commentaire