Example #1
0
        private async Task AddContactToAMailingList(MailjetContact mailjetContact, int id)
        {
            try
            {
                var requestToContactList = new MailjetRequest
                {
                    Resource   = ContactManagecontactslists.Resource,
                    ResourceId = ResourceId.Numeric(id)
                }.Property(ContactManagecontactslists.ContactsLists, new JArray
                {
                    new JObject
                    {
                        { "Action", "addnoforce" },
                        { "ListID", mailjetContact.ContactListId }
                    }
                });
                var response = await _clientV3.PostAsync(requestToContactList);

                if (!response.IsSuccessStatusCode)
                {
                    throw new MailjetApiClientException(response.FormatForLogs());
                }
            }
            catch (Exception e)
            {
                throw new MailjetApiClientException(e.Message, e.InnerException);
            }
        }
Example #2
0
        private async Task FillTemplateContent(long templateId)
        {
            // arrange
            var content = File.ReadAllText(Path.Combine("Resources", "MJMLTemplate.mjml"));

            MailjetRequest request = new MailjetRequest
            {
                Resource   = TemplateDetailcontent.Resource,
                ResourceId = ResourceId.Numeric(templateId)
            }
            .Property(TemplateDetailcontent.MJMLContent, content)
            .Property(TemplateDetailcontent.Headers, JObject.FromObject(new Dictionary <string, string>()
            {
                { "Subject", "Test transactional template subject " + DateTime.UtcNow },
                { "SenderName", "Test transactional template" },
                { "SenderEmail", _senderEmail },
                { "From", _senderEmail },
            }));

            // act
            MailjetResponse response = await _client.PostAsync(request);

            // assert
            Assert.IsTrue(response.IsSuccessStatusCode);
            Assert.AreEqual(1, response.GetTotal());
            Assert.AreEqual(content, response.GetData().Single().Value <string>("MJMLContent"));
        }
Example #3
0
        private bool SendCampaign(MailjetClient client, long idCampaign)
        {
            try
            {
                MailjetRequest request = new MailjetRequest
                {
                    Resource   = CampaigndraftSend.Resource,
                    ResourceId = ResourceId.Numeric(idCampaign)
                };


                MailjetResponse response = Task.Run(() => client.PostAsync(request)).Result;
                if (response.IsSuccessStatusCode)
                {
                    return(true);
                }
                else
                {
                    telemetria.Critical(string.Format("StatusCode: {0}\n", response.StatusCode) + " " + string.Format("ErrorInfo: {0}\n", response.GetErrorInfo()) + " " + string.Format("ErrorMessage: {0}\n", response.GetErrorMessage()));
                }
            }
            catch (Exception ex)
            {
                var messageException = telemetria.MakeMessageException(ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
                telemetria.Critical(messageException);
            }

            return(false);
        }
Example #4
0
        private async Task DeleteTemplate(long templateId)
        {
            // arrange
            MailjetRequest request = new MailjetRequest
            {
                Resource   = Template.Resource,
                ResourceId = ResourceId.Numeric(templateId)
            };

            // act
            MailjetResponse response = await _client.DeleteAsync(request);

            // assert
            Assert.AreEqual(204, response.StatusCode);
        }
Example #5
0
        // Mailjet doesn't allow deleting a contact with their API (except in V4), you still need to delete it manually, but at least it won't recieve any mail from this list
        //TODO: add a method using HTTP client to delete the contact (V4 API is only accepting http requests). So you need to create the HTTP client too
        public async Task DeleteContactFromContactList(string contactEmail, string contactListId)
        {
            var id      = Convert.ToInt64(GetContactId(contactEmail));
            var request = new MailjetRequest
            {
                Resource   = Contactdata.Resource,
                ResourceId = ResourceId.Numeric(id)
            };
            var response = await _clientV3.DeleteAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                throw new MailjetApiClientException(response.FormatForLogs());
            }
        }
Example #6
0
        /// <summary>
        /// Removes a contact from any contact list.
        /// </summary>
        /// <param name="emailSender"></param>
        /// <param name="apiKey"></param>
        /// <param name="apiSecret"></param>
        /// <param name="contactId"></param>
        /// <returns></returns>
        public static async Task RemoveListRecipientAsync(this IEmailSender emailSender, string apiKey, string apiSecret, long contactId)
        {
            MailjetClient client = new MailjetClient(apiKey, apiSecret)
            {
                Version = ApiVersion.V3,
            };

            MailjetRequest request = new MailjetRequest
            {
                Resource   = Listrecipient.Resource,
                ResourceId = ResourceId.Numeric(contactId)
            };

            await client.DeleteAsync(request);
        }
        private async Task AssertDeleteContact(long contactId)
        {
            // arrange
            MailjetRequest request = new MailjetRequest
            {
                Resource   = Contacts.Resource, // pay attention - GDPR DELETE contact resource is named in plural
                ResourceId = ResourceId.Numeric(contactId)
            };

            // act
            MailjetResponse response = await _client.DeleteAsync(request);

            // assert
            Assert.AreEqual(200, response.StatusCode);
        }
        private async Task AssertGetContact(long contactId)
        {
            // arrange
            MailjetRequest request = new MailjetRequest
            {
                Resource   = Contact.Resource,
                ResourceId = ResourceId.Numeric(contactId)
            };

            // act
            MailjetResponse response = await _client.GetAsync(request);

            // assert
            Assert.AreEqual(200, response.StatusCode);
            Assert.AreEqual(1, response.GetCount());
            Assert.AreEqual(1, response.GetTotal());

            var firstObject = response.GetData()[0];

            Assert.AreEqual(true, firstObject.Value <bool>("IsExcludedFromCampaigns"));
            Assert.AreEqual(_contactName, firstObject.Value <string>("Name"));
            Assert.AreEqual(_contactEmail, firstObject.Value <string>("Email"));
            Assert.AreEqual(contactId, firstObject.Value <long>("ID"));
        }
        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);
        }