首页 > 解决方案 > 创建联系人并将其添加到列表 SendinBlue C#

问题描述

我想在我的网站上使用 SendinBlue API,但我面临两个问题。我想从电子邮件中创建一个新联系人并将这个新联系人添加到 SendinBlue 网站上的联系人列表中。为了做到这一点,我正在关注此页面https://developers.sendinblue.com/reference/createcontact

第一个问题: 当我尝试执行代码时,出现一个错误,告诉我密钥已经在这一行的字典中(我显然用 v3 API 密钥替换了“YOUR API KEY”):

Configuration.Default.ApiKey.Add("api-key", "YOUR API KEY");

第二个问题: 之后,当我将新创建的联系人添加到 API 时,我在这一行有一个错误,告诉我“发生了一个或多个错误”:

CreateUpdateContactModel result = apiInstance.CreateContact(createContact);

这是我的代码:

// I replaced "YOUR API KEY" with my private api key
sib_api_v3_sdk.Client.Configuration.Default.ApiKey.Add("api-key", "YOUR API KEY");
var apiInstance = new ContactsApi();
JObject attributes = new JObject();
List<long?> listIds = new List<long?>();
listIds.Add(2);
bool emailBlacklisted = false;
bool smsBlacklisted = false;
bool updateEnable = true;
List<string> smtpBlacklistSender = new List<string>();
try
{
    var createContact = new CreateContact(email, attributes, emailBlacklisted,
                            smsBlacklisted, listIds, updateEnable, smtpBlacklistSender);
    CreateUpdateContactModel result = apiInstance.CreateContact(createContact);
    Debug.WriteLine(result.ToJson());
    Console.WriteLine(result.ToJson());
    Console.ReadLine();
}
catch (Exception exc)
{
    Debug.WriteLine(exc.Message);
    Console.WriteLine(exc.Message);
    Console.ReadLine();
}

有没有人已经面临这个问题之一?

谢谢

标签: c#sendinblue

解决方案


第一个问题:您正在尝试向字典中添加新值,其中字典中的值已经存在。它可以从配置文件中初始化,或者从之前的任何运行中输入?您应该检查该值是否存在,如果缺少 - 您将添加它:

// Instead of this
Configuration.Default.ApiKey.Add("api-key", "YOUR API KEY");

// You should do something like this
// Note that there might be different method to be called, you have to check what is available
if(!Configuration.Default.ApiKey.HasKey("api-key"))
    Configuration.Default.ApiKey.Add("api-key", "YOUR API KEY");

第二题:

一个或多个错误发生

通常这个异常被抛出Multi-exception,将它们组合在一起。您需要检查属性内部的内容InnerException或获取StackTrace. 从那你应该能够看到细节,有什么问题。


最后请注意,我建议您在 github 上使用以下示例,因为您似乎正在使用他们的 C# 库。


推荐阅读