예제 #1
0
        //Let's call it as a dependency here, for the sake of showing that the DI is working, but not very clever to do async here
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IMailjetEmailClient mailjetEmailClient, IMailjetSimpleClient mailjetSimpleClient, IMailjetOptions mailjetOptions, IMailjetSmsClient mailjetSmsClient)
        {
            var email = new EmailMessage("Test Testsson", "*****@*****.**");
            //var emailResponse = mailjetEmailClient.SendAsync(email).GetAwaiter().GetResult();

            var sms = new SmsMessage
            {
                To   = Environment.GetEnvironmentVariable("MAILJET_TEST_PHONE"),
                From = "Max Test",
                Text = "You should receive this :)"
            };
            var smsResponse = mailjetSmsClient.SendAsync(sms).GetAwaiter().GetResult();

            //The low level MailjetSimpleClient takes an IRequestFactory for sending a request. Anything that implements this (properly) can send whatever type of request
            //This is essentially the equivalent of what is being done in SendAsync() above.
            var basicResponse  = mailjetSimpleClient.SendRequestAsync(new SendEmailRequest(email, mailjetOptions)).GetAwaiter().GetResult();
            var basicResponse2 = mailjetSimpleClient.SendRequestAsync(new SendSmsRequest(sms, mailjetOptions));

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.Run(async(context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }
        public async Task <ISendSmsResponse> SendAsync(ISmsMessage smsMessage)
        {
            Log.Info("Sending an SMS");
            Log.Debug("SMS options: " + LogSerialiser.Serialise(options.SmsOptions));
            var response = await client.SendRequestAsync(new SendSmsRequest(smsMessage, options));

            return(new SendSmsResponse(JsonConvert.DeserializeObject <SendSmsResponseEntry>(response.RawResponse), response.RawResponse,
                                       response.StatusCode, response.Successful));
        }
예제 #3
0
        public async Task <ISendEmailResponse> SendAsync(IEnumerable <IEmailMessage> emailMessages)
        {
            try
            {
                var emails = emailMessages.ToList();
                Log.Info($"Sending {emails.Count} emails");
                Log.Debug("Email options: " + LogSerialiser.Serialise(Options.EmailOptions));
                var req = new SendEmailRequest(emails, Options);
                var res = await client.SendRequestAsync(req);

                var token = JToken.Parse(res.RawResponse);

                return(new SendEmailResponse(
                           token["Messages"]?.ToObject <List <SendEmailResponseEntry> >(),
                           res));
            }
            catch (Exception e)
            {
                Log.Error(e, "Error sending emails");
                throw;
            }
        }