/// <summary> /// Create mail message from <paramref name="EmailTemplate"/>. /// </summary> public MailMessage CreateMessage(EmailTemplate emailTemplate, params object[] args) { var typepropertyToTokensMap = GetTokenMap(emailTemplate); var tokenToValueMap = GetTokenToValueMap(typepropertyToTokensMap, args); var message = CreateMessage(emailTemplate, tokenToValueMap); return message; }
/// <summary> /// Parse out <paramref name="EmailToken"/>s from <paramref name="EmailTemplate"/> /// and create a type,property->token map. /// </summary> private IDictionary<string, Dictionary<string, EmailToken>> GetTokenMap(EmailTemplate emailTemplate) { var toTokens = GetTokens(emailTemplate.To); var fromTokens = GetTokens(emailTemplate.From); var subjectTokens = GetTokens(emailTemplate.Subject); var bodyTokens = GetTokens(emailTemplate.Body); var tokens = new HashSet<EmailToken>(); AddTokenIfValid(tokens, toTokens); AddTokenIfValid(tokens, fromTokens); AddTokenIfValid(tokens, subjectTokens); AddTokenIfValid(tokens, bodyTokens); var typepropertyToTokensMap = Convert(tokens); return typepropertyToTokensMap; }
/// <summary> /// Create mail message from <paramref name="EmailTemplate"/> and token->value map. /// </summary> private MailMessage CreateMessage(EmailTemplate emailTemplate, IDictionary<string, string> tokenValueMap) { var from = Replace(emailTemplate.From, tokenValueMap); var to = Replace(emailTemplate.To, tokenValueMap); var subject = Replace(emailTemplate.Subject, tokenValueMap); var body = Replace(emailTemplate.Body, tokenValueMap); var message = new MailMessage(); message.From = new MailAddress(from); message.To.Add(new MailAddress(to)); message.Subject = subject; message.Body = body; return message; }