示例#1
0
    public void ShoulConvertToCorrectMailjetEntity()
    {
        var options = new MailjetOptions
        {
            ApiKeyPublic         = "",
            ApiKeyPrivate        = "",
            IsSendingMailAllowed = true
        };

        var client = new MailjetService(options);

        var mailjetMail = new MailjetMail()
        {
            Users = new List <User> {
                new User {
                    Email = "*****@*****.**"
                }
            },
        };

        var converted = client.ConvertToMailjetMessage(mailjetMail);

        Assert.Null(converted.From);
        Assert.IsType <List <MailjetMailUser> >(converted.To);
        Assert.Equal("*****@*****.**", converted.To[0].Email);
    }
示例#2
0
    public void ShouldConvertMailjetMessageToCorrectJson()
    {
        var options = new MailjetOptions
        {
            ApiKeyPublic         = "",
            ApiKeyPrivate        = "",
            IsSendingMailAllowed = true,
        };

        var client = new MailjetService(options);

        var mailjetMail = new MailjetMail()
        {
            Users = new List <User> {
                new User {
                    Email = "*****@*****.**"
                }
            },
            Variables = new NestedClass
            {
                I = 42,
                S = "toto"
            }
        };

        var converted = client.ConvertToMailjetMessage(mailjetMail);

        var json = JObject.FromObject(converted);

        Assert.Equal("toto", json["Variables"]?["field"]?.Value <string>());
        Assert.Equal(42, json["Variables"]?["I"]?.Value <int>());
        Assert.Equal("*****@*****.**", json["To"]?[0]?["Email"]?.Value <string>());
        Assert.False(json.ContainsKey("From"));
    }
示例#3
0
    public async Task SendMail(MailjetMail mailjetMail)
    {
        if (!_isSendingMailAllowed)
        {
            return;
        }

        var email  = ConvertToMailjetMessage(mailjetMail);
        var emails = new List <MailjetMessage <object> > {
            email
        };

        try
        {
            var request = new MailjetRequest
            {
                Resource = Send.Resource
            }
            .Property(Send.Messages, JArray.FromObject(emails));

            var response = await _clientV3_1.PostAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                throw new MailjetApiClientException(response.FormatForLogs());
            }
        }
        catch (Exception e)
        {
            throw new MailjetApiClientException(e.Message, e.InnerException);
        }
    }
示例#4
0
    public MailjetMessage <object> ConvertToMailjetMessage(MailjetMail mailjetMail)
    {
        var mailjetMessage = new MailjetMessage <object>
        {
            Variables    = mailjetMail.Variables,
            Attachements = mailjetMail?.AttachmentFiles?.Select(x => new MailjetAttachement
            {
                ContentType   = x.ContentType,
                Filename      = x.Filename,
                Base64Content = x.Base64Content
            })?.ToList(),
            TemplateId = mailjetMail.TemplateId,
            To         = mailjetMail.Users?.Select(x => new MailjetMailUser {
                Email = x.Email, Name = x.Email
            })?.ToList(),
            Bcc = mailjetMail.UsersInBcc?.Select(x => new MailjetMailUser {
                Email = x.Email, Name = x.Email
            })?.ToList(),
            Cc = mailjetMail.UsersInCc?.Select(x => new MailjetMailUser {
                Email = x.Email, Name = x.Email
            })?.ToList(),
        };

        if (!string.IsNullOrEmpty(_senderEmail) && !string.IsNullOrEmpty(_senderName))
        {
            mailjetMessage.From = new MailjetMailUser {
                Email = _senderEmail, Name = _senderName
            };
        }

        if (IsInTestMode())
        {
            mailjetMessage.Bcc = null;
            mailjetMessage.Cc  = null;
            mailjetMessage.To  = new List <MailjetMailUser>()
            {
                new MailjetMailUser
                {
                    Email = _testingRedirectionMail,
                    Name  = _testingRedirectionMail,
                }
            };
        }

        return(mailjetMessage);
    }
示例#5
0
    public async Task SendMail(string email, int templateId, object variables = null, List <Models.User> usersInCc = null, List <Models.User> usersInBcc = null)
    {
        var mailjetMail = new MailjetMail
        {
            Users = new List <Models.User> {
                new Models.User()
                {
                    Email = email
                }
            },
            TemplateId = templateId,
            Variables  = variables,
            UsersInCc  = usersInCc,
            UsersInBcc = usersInBcc,
        };

        await SendMail(mailjetMail);
    }
示例#6
0
        public async Task SendMail(string email, int templateId, object variables = null, List <Models.User> usersInCc = null, List <Models.User> usersInBcc = null)
        {
            var variablesAsDictionary = variables?.GetType().GetProperties().ToDictionary(x => x.Name, x => x.GetValue(variables, null));;

            var mailjetMail = new MailjetMail
            {
                Users = new List <Models.User> {
                    new Models.User()
                    {
                        Email = email
                    }
                },
                TemplateId = templateId,
                Variables  = variablesAsDictionary,
                UsersInCc  = usersInCc,
                UsersInBcc = usersInBcc,
            };

            await SendMail(mailjetMail);
        }
示例#7
0
        public async Task SendMail(MailjetMail mailJetMail)
        {
            if (!_isSendingMailAllowed)
            {
                return;
            }
            try
            {
                var mailTo = IsInTestMode() ?
                             new JArray {
                    new JObject(new JProperty("Email", _testingRedirectionMail), new JProperty("Name", " "))
                } :
                new JArray {
                    from user in mailJetMail.Users select new JObject(new JProperty("Email", user.Email), new JProperty("Name", user.Email))
                };


                var mailCc = new JArray();

                if (mailJetMail.UsersInCc != null && mailJetMail.UsersInCc.Any())
                {
                    mailCc = IsInTestMode() ?
                             new JArray {
                        new JObject(new JProperty("Email", _testingRedirectionMail), new JProperty("Name", "TESTING"))
                    } :
                    new JArray {
                        from userCc in mailJetMail.UsersInCc select new JObject(new JProperty("Email", userCc.Email), new JProperty("Name", userCc.Email))
                    };
                }

                var mailBcc = new JArray();

                if (mailJetMail.UsersInBcc != null && mailJetMail.UsersInBcc.Any())
                {
                    mailBcc = IsInTestMode() ?
                              new JArray {
                        new JObject(new JProperty("Email", _testingRedirectionMail), new JProperty("Name", "TESTING"))
                    } :
                    new JArray {
                        from userBcc in mailJetMail.UsersInBcc select new JObject(new JProperty("Email", userBcc.Email), new JProperty("Name", userBcc.Email))
                    };
                }

                JObject variables = null;

                if (mailJetMail.Variables != null)
                {
                    variables = new JObject
                    {
                        from key in mailJetMail.Variables.Keys select new JProperty(key, mailJetMail.Variables[key])
                    };
                }

                // Mail
                var request = new MailjetRequest
                {
                    Resource = Send.Resource
                }
                .Property(Send.Messages, new JArray
                {
                    new JObject
                    {
                        { "From", new JObject {
                              new JProperty("Email", _senderEmail), new JProperty("Name", _senderName)
                          } },
                        { "To", mailTo },
                        { "Cc", mailCc },
                        { "Bcc", mailBcc },
                        { "TemplateID", mailJetMail.TemplateId },
                        { "TemplateLanguage", true },
                        { "Variables", variables },
                        { "Attachments", !mailJetMail.AttachmentFiles.Any() ? null :
                          new JArray {
                              from file in mailJetMail.AttachmentFiles select
                              new JObject
                              {
                                  { "ContentType", file.ContentType },
                                  { "Filename", file.Filename },
                                  { "Base64Content", file.Base64Content }
                              }
                          } },
                        { "TemplateErrorReporting", IsInTestMode() ?
                          new JObject {
                              new JProperty("Email", _testingRedirectionMail),
                              new JProperty("Name", _testingRedirectionMail)
                          } : null },
                        { "TemplateErrorDeliver", true }
                    }
                });

                var response = await _clientV3_1.PostAsync(request);

                if (!response.IsSuccessStatusCode)
                {
                    throw new MailjetApiClientException(response.FormatForLogs());
                }
            }
            catch (Exception e)
            {
                throw new MailjetApiClientException(e.Message, e.InnerException);
            }
        }