예제 #1
0
        public async Task <ActionResult> Post([FromBody] Xby2Message email)
        {
            var username = Configuration["XBY2_WEBSITE_API_MAILJET_USERNAME"];
            var password = Configuration["XBY2_WEBSITE_API_MAILJET_PASSWORD"];

            if (username == null || password == null)
            {
                return(StatusCode(500, "API Credentials not set."));
            }

            var apiUrl    = Configuration.GetValue <string>("ApplicationSettings:EmailApiUrl");
            var fromEmail = Configuration.GetValue <string>("ApplicationSettings:FromEmail");
            var toEmail   = Configuration.GetValue <string>("ApplicationSettings:ToEmail");
            var subject   = Configuration.GetValue <string>("ApplicationSettings:Subject");

            using (HttpClient client = new HttpClient())
            {
                var authorizationString = string.Format("{0}:{1}", username, password);
                var byteArray           = Encoding.ASCII.GetBytes(authorizationString);
                client.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

                MailjetMessage mailjetMessage =
                    MailjetMessage.FromXby2Message(email, fromEmail, toEmail, subject);
                MailjetRequest request     = new MailjetRequest(mailjetMessage);
                var            requestJson = request.ToJson();

                var response = await client.PostAsync(apiUrl, new StringContent(requestJson, Encoding.UTF8, "application/json"));

                response.EnsureSuccessStatusCode();
            }

            return(Ok());
        }
예제 #2
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);
    }