コード例 #1
0
        private async Task UpdateContactCustomProperties(int contactId, MailjetContact mailjetContact)
        {
            var enumerator = mailjetContact.CustomProperties.GetEnumerator();
            var request    = new MailjetRequest
            {
                Resource = new ResourceInfo("contactdata/" + contactId),
            }
            .Property(Contactdata.Data,

                      new JArray {
                from key in mailjetContact.CustomProperties.Keys select new JObject(new JProperty("Name", key), new JProperty("Value", mailjetContact.CustomProperties[key]))
            }
                      );

            var response = await _clientV3.PutAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                throw new MailjetApiClientException(response.FormatForLogs());
            }
        }
コード例 #2
0
        private async Task <bool> AddUserToContactList(string email, string name)
        {
            var request = new MailjetRequest {
                Resource = Contact.Resource, ResourceId = ResourceId.Alphanumeric(email)
            };
            var response = await _client.GetAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                request = new MailjetRequest {
                    Resource = Contact.Resource
                }
                .Property(Contact.Name, name)
                .Property(Contact.Email, email);
                response = await _client.PostAsync(request);

                if (response.IsSuccessStatusCode)
                {
                    request = new MailjetRequest {
                        Resource = Contact.Resource, ResourceId = ResourceId.Alphanumeric(email)
                    };
                    response = await _client.GetAsync(request);
                }
            }

            if (response.IsSuccessStatusCode)
            {
                var contactId = response.GetData().First.Value <long>(Contact.ID);

                request = new MailjetRequest {
                    Resource = Contactdata.Resource, ResourceId = ResourceId.Numeric(contactId)
                }
                .Property(Contactdata.Data, new JArray {
                    new JObject {
                        { "Name", "country" }, { "Value", CultureInfo.CurrentUICulture.Name }
                    }
                });
                response = await _client.PutAsync(request);

                if (response.IsSuccessStatusCode)
                {
                    var contactListId = _mailSettings.Value.ContacListId.ContainsKey(CultureInfo.CurrentUICulture.Name) ?
                                        _mailSettings.Value.ContacListId[CultureInfo.CurrentUICulture.Name] :
                                        _mailSettings.Value.ContacListId["en-US"];
                    request = new MailjetRequest {
                        Resource = ContactManagecontactslists.Resource, ResourceId = ResourceId.Numeric(contactId)
                    }
                    .Property(ContactManagecontactslists.ContactsLists, new JArray {
                        new JObject {
                            { "ListId", contactListId.ToString() },
                            { "Action", "addnoforce" }
                        }
                    });
                    response = await _client.PostAsync(request);

                    if (response.IsSuccessStatusCode)
                    {
                        return(true);
                    }
                    else
                    {
                        _logger.LogError(response.GetErrorMessage());
                    }
                }
                else
                {
                    _logger.LogError(response.GetErrorMessage());
                }
            }
            else
            {
                _logger.LogError(response.GetErrorMessage());
            }

            return(false);
        }