Esempio n. 1
0
        public Task SendEmailAsync(string email, string subject, string message)
        {
            //_appSettings

            var client = new RestClient();

            client.BaseUrl = new Uri(_appSettings.EmailBaseUrl);
            // client.BaseUrl = new Uri("https://api.elasticemail.com");
            var request = new RestRequest(_appSettings.EmailServiceUrl, Method.POST);

            //request.AddParameter("apikey", "21eddf0a-7467-4b23-a2c0-6134e1bc60f2");
            //request.AddParameter("from", "*****@*****.**");

            request.AddParameter("apikey", _appSettings.EmailApiKey);
            request.AddParameter("from", _appSettings.EmailFromAddress);

            request.AddParameter("to", email);
            request.AddParameter("subject", subject);
            request.AddParameter("body", message);
            request.AddParameter("isTransactional", true.ToString());

            //method 1
            // var respon = client.ExecuteAsync(request);

            //method 2
            //var response = new RestResponse();
            //Task.Run(async () =>
            //{
            //    response = await GetResponseContentAsync(client, request) as RestResponse;
            //}).Wait();
            //var jsonResponse = JsonConvert.DeserializeObject<dynamic>(response.Content);


            //method 3

            TaskCompletionSource <IRestResponse> taskCompletion = new TaskCompletionSource <IRestResponse>();
            RestRequestAsyncHandle handle = client.ExecuteAsync(
                request, r => taskCompletion.SetResult(r));
            RestResponse         response1     = (RestResponse)(taskCompletion.Task.Result);
            ElasticEmailResponse responsemodel = JsonConvert.DeserializeObject <ElasticEmailResponse>(response1.Content);

            if (responsemodel.success)
            {
                var transcationId = responsemodel.data.transactionid;
                var messageId     = responsemodel.data.messageid;
            }
            //same with 3 but with async
            //TaskCompletionSource<IRestResponse> taskCompletion = new TaskCompletionSource<IRestResponse>();
            //RestRequestAsyncHandle handle = client.ExecuteAsync(
            //    request, r => taskCompletion.SetResult(r));

            //RestResponse response1 = (RestResponse)(await taskCompletion.Task);
            //return JsonConvert.DeserializeObject<SomeObject>(response.Content);

            return(Task.CompletedTask);
        }
Esempio n. 2
0
        public ElasticEmailResponse EmailSenderHelper(string email, string subject, string message)
        {
            var client = new RestClient();

            client.BaseUrl = new Uri(_appSettings.EmailBaseUrl);
            var request = new RestRequest(_appSettings.EmailServiceUrl, Method.POST);

            request.AddParameter("apikey", _appSettings.EmailApiKey);
            request.AddParameter("from", _appSettings.EmailFromAddress);

            request.AddParameter("to", email);
            request.AddParameter("subject", subject);
            request.AddParameter("body", message);
            request.AddParameter("isTransactional", true.ToString());

            TaskCompletionSource <IRestResponse> taskCompletion = new TaskCompletionSource <IRestResponse>();
            RestRequestAsyncHandle handle = client.ExecuteAsync(
                request, r => taskCompletion.SetResult(r));
            RestResponse         response1     = (RestResponse)(taskCompletion.Task.Result);
            ElasticEmailResponse responsemodel = JsonConvert.DeserializeObject <ElasticEmailResponse>(response1.Content);

            return(responsemodel);
        }