您可以使用SubstitutionTags。
这就是我这样做的方式:
我创建了一个EmailBase
类:
public abstract class EmailBase
{
public EmailBase()
{
ToDisplayName = string.Empty;
ToFullName = string.Empty;
FromDisplayName = string.Empty;
FromFullName = string.Empty;
Subject = string.Empty;
Content = string.Empty;
HtmlContent = string.Empty;
TemplateId = string.Empty;
}
// note that I am using a default Sender email, you can modify this...
public EmailBase(string toFirstName, string toLastName, string toEmail)
{
ToDisplayName = toFirstName;
ToFullName = toFirstName + " " + toLastName;
ToEmail = toEmail;
FromDisplayName = string.Empty;
FromEmail = "mailer@my-domain.com"; // I am using a default sender email
FromFullName = "my-domain support"; // Whatever name you want for this email
ReplyToEmail = string.Empty;
ReplyToFullName = string.Empty;
Subject = string.Empty;
Content = string.Empty;
HtmlContent = string.Empty;
TemplateId = string.Empty;
Attachment = null;
AttachmentFileName = string.Empty;
}
public string ToDisplayName { get; set; }
public string ToEmail { get; set; }
public string ToFullName { get; set; }
public string FromDisplayName { get; set; }
public string FromEmail { get; set; }
public string FromFullName { get; set; }
public string ReplyToEmail { get; set; }
public string ReplyToFullName { get; set; }
public string Subject { get; set; }
public string Content { get; set; }
public string HtmlContent { get; set; }
public string TemplateId { get; set; }
public byte[] Attachment { get; set; }
public string AttachmentFileName { get; set; }
public Dictionary<string, string> SubstitutionTags { get; set; }
}
现在,对于您的每个模板,您都可以创建一个继承自 的相应类EmailBase
,例如,您可以EmailAddressConfirmationEmail
为此模板创建:
public class EmailAddressConfirmationEmail : EmailBase
{
public EmailAddressConfirmationEmail(string toFirstName, string toLastName, string toEmail, string embededUrl)
: base(toFirstName, toLastName, toEmail)
{
// by default from is set to mailer@my-domain.com
TemplateId = "your-template-id";
SubstitutionTags = new Dictionary<string, string>
{
{ "-name-", ToDisplayName },
{ "-confirmationurl-", embededUrl }
};
}
}
最后,这是您如何创建电子邮件服务来生成您的SendGridMessage
:
public class SendGridEmailService
{
private readonly string _apiKey;
public SendGridEmailService(string apiKey)
{
_apiKey = apiKey;
}
public int SendEmail(EmailBase email)
{
var client = new SendGridClient(_apiKey);
Task<int> task = Task.Run<int>(async () => await SendEmailAsync(email));
return task.Result;
}
public async Task<int> SendEmailAsync(EmailBase email)
{
var client = new SendGridClient(_apiKey);
var sendGridMessage = BuildSendGridMessage(email);
var response = await client.SendEmailAsync(sendGridMessage);
return (int)response.StatusCode;
}
/*
* This is the important function which builds SendGridMessage,
* Note how we are using SubstitutionTags to achieve what you want
*/
private SendGridMessage BuildSendGridMessage(EmailBase email)
{
SendGridMessage sendGridMessage = new SendGridMessage
{
From = new EmailAddress(email.FromEmail, email.FromFullName)
};
if (!string.IsNullOrEmpty(email.ReplyToEmail))
{
sendGridMessage.ReplyTo = new EmailAddress(email.ReplyToEmail, email.ReplyToFullName);
}
sendGridMessage.AddTo(new EmailAddress(email.ToEmail, email.ToFullName));
if (!string.IsNullOrEmpty(email.TemplateId))
{
sendGridMessage.SetTemplateId(email.TemplateId);
sendGridMessage.Personalizations[0].Substitutions = new Dictionary<string, string>();
foreach (KeyValuePair<string, string> p in email.SubstitutionTags)
{
sendGridMessage.Personalizations[0].Substitutions.Add(p.Key, p.Value);
}
}
if (!string.IsNullOrEmpty(email.Subject))
{
sendGridMessage.Subject = email.Subject;
}
if (!string.IsNullOrEmpty(email.Content))
{
sendGridMessage.AddContent("text/plain", email.Content);
}
if (!string.IsNullOrEmpty(email.HtmlContent))
{
sendGridMessage.HtmlContent = email.HtmlContent;
}
if (email.Attachment != null && !string.IsNullOrEmpty(email.AttachmentFileName))
{
using (WebClient webClient = new WebClient())
{
var base4File = Convert.ToBase64String(email.Attachment);
sendGridMessage.AddAttachment(email.AttachmentFileName, base4File);
}
}
return sendGridMessage;
}
}