我正在使用 Sendgrid 发送带有包含多个变量的模板的电子邮件。当我只有一个电子邮件收件人时,一切正常。
当我在“收件人”或“收件人”中有多个收件人,在“抄送”中有一个收件人时,第一封电子邮件没问题,但下面的替换标签中有空字符串。
下面是我的代码:
private bool SendEmail(MailAddress from, string[] to, string template, Dictionary<string, string> keyToReplace, string[] cc = null)
{
var message = new SendGridMessage();
message.From = from;
message.AddTo(to);
if (cc != null && cc.Any())
{
foreach (var ccAddress in cc)
{
message.AddCc(ccAddress);
}
}
message.Subject = " ";
message.Text = string.Empty;
message.Html = "<p></p>";
message.EnableTemplate("<%body%>");
message.EnableTemplateEngine(templateIds[template]);
foreach (var keyValue in keyToReplace)
{
var key = keyValue.Key;
if (!key.StartsWith("#"))
{
key = string.Format("#{0}#", key);
}
var value = keyValue.Value;
if (string.IsNullOrEmpty(keyValue.Value))
{
value = " ";
}
message.AddSubstitution(key, new List<string> { value });
}
var transportWeb = transportFactory(credentials);
transportWeb.Deliver(message);
logger.Info("Mail sent to : " + string.Join(", ", to));
return true;
}