SendGrid(API v3)电子邮件模板不适用于C#MVC

时间:2019-01-29 22:31:36

标签: c# asp.net-mvc sendgrid sendgrid-api-v3 sendgrid-templates

作为订单流程的一部分,我想发送订单确认电子邮件。为此,我使用了SendGrid。现在一切都运行良好,直到我决定设计一个SendGrid模板,并用有关订单的实际数据(如订单项等)填充它。

我最终得到的是一段代码,该代码会将部分信息发送到模板,然后发送电子邮件(显然是错误的数据),但是我要表达的意思是我让它工作了从配置的角度来看。

但是,现在我终于相信已经弄清楚了如何将正确的数据放入模板(下面的代码),该电子邮件不再发送了(状态3131)。有什么想法可以破坏我的东西吗?

    var apiKey = ConfigurationManager.AppSettings["SGAPIkey"];
var client = new SendGridClient(apiKey);
var msg = new SendGridMessage();
msg.SetFrom(new EmailAddress("orders@freshNclean.ch", "freshNclean"));
msg.AddTo(new EmailAddress(UserManager.FindById(userID).Email));
msg.SetTemplateId("d-9f76f1ec08964219954067b31b3a97ad");

var orderedLineItems = DATADB.LineItemList.Where(x => x.OrderNumber == orderID).ToList();
var allLineItems = new List<orderConfirmation>();
string prd;
int qty;
decimal prc;
foreach(var itm in orderedLineItems)
{
    if (itm.SubscriptionQuantity == 0)
    {
        prd = itm.ProductName;
        qty = itm.SingleOrderQuantity;
        prc = itm.SingleOrderPrice;
    }
    else
    {
        prd = itm.ProductName;
        qty = itm.SubscriptionQuantity;
        prc = itm.SubscriptionPrice;
    }
    var bdy = new orderConfirmation
    {
        receipt = true,
        lineItem = new lineItem
        {
            product = prd,
            quantity = qty,
            price = 
        },
        total = "CHF " + order.OrderAmount.ToString(),
        company = DATADB.ShippingAddressList.Where(x => x.UserID == userID).Where(x => x.IsDefaultShippingAddress == true).Select(x => x.ShippingAddressCompanyName).First(),
        name = UserManager.FindById(userID).FirstName.ToString() + " " + UserManager.FindById(userID).LastName.ToString(),
        address01 = DATADB.ShippingAddressList.Where(x => x.UserID == userID).Where(x => x.IsDefaultShippingAddress == true).Select(x => x.ShippingAddressStreet).First() + " " + DATADB.ShippingAddressList.Where(x => x.UserID == userID).Where(x => x.IsDefaultShippingAddress == true).Select(x => x.ShippingAddressNumber).First(),
        address02 = DATADB.ShippingAddressList.Where(x => x.UserID == userID).Where(x => x.IsDefaultShippingAddress == true).Select(x => x.ShippingAddressAdditional).First(),
        zip = DATADB.ShippingAddressList.Where(x => x.UserID == userID).Where(x => x.IsDefaultShippingAddress == true).Select(x => x.ShippingAddressZIP).First(),
        city = DATADB.ShippingAddressList.Where(x => x.UserID == userID).Where(x => x.IsDefaultShippingAddress == true).Select(x => x.ShippingAddressCity).First(),
        state = DATADB.ShippingAddressList.Where(x => x.UserID == userID).Where(x => x.IsDefaultShippingAddress == true).Select(x => x.ShippingAddressState).First(),
        instructions = DATADB.ShippingAddressList.Where(x => x.UserID == userID).Where(x => x.IsDefaultShippingAddress == true).Select(x => x.ShippingInstructions).First()
    };
allLineItems.Add(bdy);
};
msg.SetTemplateData(allLineItems);
var response = await client.SendEmailAsync(msg);

0 个答案:

没有答案