Esempio n. 1
0
        public async Task <IHttpActionResult> ResetPassword([FromBody] Credential credential)
        {
            try {
                var restclient = new RestClient(_api);
                var request    = new RestRequest("ResetPassword");
                request.AddJsonBody(new {
                    userName = credential.UserName
                });
                //request.AddParameter("userName", credential.UserName);
                var response = await restclient.ExecutePostTaskAsync(request);

                //if (JsonConvert.DeserializeObject<IDictionary<string, dynamic>>(response.Content).FirstOrDefault().Value)
                //    return NotFound();

                var user     = Deserialize(response.Content);
                var password = user["Password"];

                string body = "<html>" + "<head></head>" + "<body>Estimado Cliente,<br/>" +
                              $"Su nueva contraseña es : {password} " +
                              "</body>" + "</html>";

                SendEmailClient send      = new SendEmailClient();
                var             userNames = new List <string>()
                {
                    credential.UserName
                };
                send.PostSendEmail(userNames, "Recuperación de Contraseña App Ferreyros", body);
                return(Ok());
            } catch (Exception ex) {
                Log.Write(ex);
                return(InternalServerError());
            }
        }
Esempio n. 2
0
        public async Task <bool> SendEmail(string toEmail, string emailTemplate, Dictionary <string, string> bodyReplacementValues,
                                           Dictionary <string, string> subjectReplacementValues)
        {
            var hermesApi          = ConfigurationManager.AppSettings["HermesApi"];
            var applicationId      = Convert.ToInt16(ConfigurationManager.AppSettings["ApplicationId"]);
            var authorizationToken = ConfigurationManager.AppSettings["EmailAuthorizationToken"];
            var sendEmails         = Convert.ToBoolean(ConfigurationManager.AppSettings["SendEmails"] ?? "false");
            var sendEmailClient    = new SendEmailClient(hermesApi);

            if (sendEmails)
            {
                if (!String.IsNullOrEmpty(toEmail))
                {
                    var emailItem = new EmailItem
                    {
                        ToEmail                  = toEmail,
                        ApplicationId            = applicationId,
                        EmailTemplate            = emailTemplate,
                        AuthorizationToken       = authorizationToken,
                        ReplacementValues        = bodyReplacementValues,
                        SubjectReplacementValues = subjectReplacementValues
                    };

                    await sendEmailClient.SendEmailAsync(emailItem);

                    return(true);
                }

                return(false);
            }

            return(false);
        }
 /// <summary>
 /// Send email with subject and body replacements
 /// </summary>
 protected async Task SendEmailAsync(string template, string toEmail, Dictionary <string, string> subjectReplacements, Dictionary <string, string> bodyReplacements)
 {
     if (appSettings.SendEmails)
     {
         var sendEmailClient = new SendEmailClient(appSettings.HermesApiUrl, appSettings.HermesSecurityToken);
         await sendEmailClient.SendAsync(template, toEmail, "", "", bodyReplacements, subjectReplacements, "");
     }
 }
Esempio n. 4
0
        public void Handle(NotEnoughCreditDto notEnoughCreditDto)
        {
            _notEnoughCreditDto = notEnoughCreditDto;
            var emailClient       = new SendEmailClient(_mailInfo.Hostname, _mailInfo.Hostport, _mailInfo.Username, _mailInfo.Password, _mailInfo.Security);
            var noteVeicoloSender = new NoteVeicoloSender(emailClient.ComposeMessage(_mailInfo.From), this);

            noteVeicoloSender.Send(_receiverString);
        }
Esempio n. 5
0
        public void Send(long idNota, string receiverMail, string connection)
        {
            var emailClient       = new SendEmailClient(_mailInfo.Hostname, _mailInfo.Hostport, _mailInfo.Username, _mailInfo.Password, _mailInfo.Security);
            var messageProvider   = new NoteVeicoloMessageProvider((int)idNota, Connections.Strings[connection]);
            var noteVeicoloSender = new NoteVeicoloSender(emailClient.ComposeMessage(_mailInfo.From, receiverMail), messageProvider);

            noteVeicoloSender.Send(receiverMail);
        }
Esempio n. 6
0
        public async Task SendEmailClientAsyncShouldWork()
        {
            var sender = new SendEmailClient(this.Configuration["SendGrid:ApiKey"]);

            using var consoleText = new StringWriter();
            Console.SetOut(consoleText);
            await sender.SendEmailAsync("*****@*****.**", "goshko", "*****@*****.**", "zaglavie", "tainbrat");

            Assert.Equal("Accepted", consoleText.ToString().Trim());
        }
Esempio n. 7
0
        public void TestSmtpClient()
        {
            SendEmailClient emailClient   = new SendEmailClient(_hostname, _hostport, _username, _password, _security);
            ISenderService  senderService = emailClient.ComposeMessage(_from);

            Mock <IMessageProvider> messageProvideMock = new Mock <IMessageProvider>();

            messageProvideMock.Setup(provider => provider.GetText()).Returns("Test text");
            messageProvideMock.Setup(provider => provider.GetAbstract()).Returns("Test subject");

            Assert.DoesNotThrow(() => senderService.Send(messageProvideMock.Object, _to));
        }
Esempio n. 8
0
 public async Task SendEmailClientAsyncShouldThrowException(string data)
 {
     var sender = new SendEmailClient(this.Configuration["SendGrid:ApiKey"]);
     await Assert.ThrowsAsync <ArgumentException>(() => sender.SendEmailAsync("*****@*****.**", "goshko", "*****@*****.**", data, data));
 }
Esempio n. 9
0
        private void SendMail(dynamic assignment, ActivityType?type = null, string userId = null)
        {
            #region Workorder

            var client  = new RestClient(_workorderApi);
            var request = new RestRequest("getorderbyid");
            request.AddParameter("Id", assignment.WorkOrderId);
            var response  = client.ExecuteGetTaskAsync(request).GetAwaiter().GetResult();
            var workOrder = Deserialize(response.Content);

            #endregion

            #region Customer

            var customer = workOrder.Machinery;

            #endregion

            #region Machine

            var machine = new Machine()
            {
                Id                 = customer.Id,
                AssignmentId       = assignment.Id,
                Brand              = customer.Brand,
                Model              = customer.Model,
                SerialNumber       = customer.SerialNumber,
                TotalHoursFunction = customer.LifeHours
            };

            #endregion

            #region Actividades

            client  = new RestClient(_assignmentApi);
            request = new RestRequest("GetActivityByIdAndUserId");
            request.AddParameter("Id", assignment.Id);
            request.AddParameter("userId", userId);
            response = client.ExecuteGetTaskAsync(request).GetAwaiter().GetResult();
            var activity = JsonConvert.DeserializeObject <IDictionary <string, List <Activity> > >(response.Content)
                           .FirstOrDefault()
                           .Value;

            #endregion

            #region ValidateActivity

            #region New Version

            var notify      = false;
            int countExists = activity.Where(x => x.ActivityType == ActivityType.FieldReport ||
                                             x.ActivityType == ActivityType.FieldService ||
                                             x.ActivityType == ActivityType.StandByClient ||
                                             x.ActivityType == ActivityType.StandByFesa).Count();

            int OnlyTime = 0;

            if (countExists == 0)
            {
                switch (type)
                {
                case ActivityType.Traveling:
                    OnlyTime = activity.Where(x => x.ActivityType == type).Count();
                    break;

                case ActivityType.Driving:
                    OnlyTime = activity.Where(x => x.ActivityType == type).Count();
                    break;
                }
                if (OnlyTime == 1)
                {
                    notify = true;
                }
            }
            else
            {
                if (type == ActivityType.Traveling || type == ActivityType.Driving)
                {
                    notify = false;
                }
                else
                {
                    notify = true;
                }
            }

            #endregion

            #region LastVersion

            //int travel = 0, driving = 0;
            //var notify = false;

            //foreach (var item in activity) {

            //    var a = item.ActivityType.ToString();

            //    if (item.ActivityType == ActivityType.Traveling) {
            //        travel++;
            //    }
            //    if (item.ActivityType == ActivityType.Driving) {
            //        driving++;
            //    }

            //}

            //if (type == ActivityType.Traveling || type == ActivityType.Driving) {
            //    switch (type) {
            //        case ActivityType.Traveling:
            //            if (travel <= 1) {
            //                notify = true;
            //            }
            //            break;
            //        case ActivityType.Driving:
            //            if (driving <= 1) {
            //                notify = true;
            //            }
            //            break;
            //    }
            //} else {
            //    notify = true;
            //}

            #endregion

            #endregion

            #region Contact Ferreyros

            string id = assignment.Id;
            client  = new RestClient(_identityApi);
            request = new RestRequest("getusersbyidassignment");
            request.AddParameter("id", id);
            response = client.ExecuteGetTaskAsync(request).GetAwaiter().GetResult();
            var contactsFerreyros =
                JsonConvert.DeserializeObject <IDictionary <string, dynamic> >(response.Content)
                .FirstOrDefault()
                .Value;

            dynamic lead       = null;
            dynamic supervisor = null;

            foreach (var item in contactsFerreyros)
            {
                if (item.UserType == 2)
                {
                    lead = item;
                }
                else if (item.UserType == 1)
                {
                    supervisor = item;
                }
            }

            #endregion

            #region Correo Destinatario

            #region PRODUCCION

            //var userNames = new List<string>();
            //foreach (var item in contactsFerreyros) {
            //	if (item.UserType == 1) {
            //		userNames.Add(item.Email.ToString());
            //	}
            //}
            //foreach (var item in workOrder.TechnicalContacts) {
            //	userNames.Add(item.Email.ToString());
            //}

            #endregion

            #region TEST

            var userNames = new List <string>();
            userNames = new List <string>();
            userNames.Add("*****@*****.**");
            userNames.Add("*****@*****.**");

            #endregion

            string logo = "http://imgur.com/XAjtjmj.jpg";

            #endregion

            #region Formato Fecha

            var timezone = TimeZoneInfo.FindSystemTimeZoneById("Hawaiian Standard Time");
            var fecha    = DateTime.Now.AddHours(5);
            var now      = TimeZoneInfo.ConvertTime(fecha, timezone);

            #endregion

            SendEmailClient mail = new SendEmailClient();
            if (type == null)
            {
                var subject = string.Format(Constants.Mailsubject.FieldReport, workOrder.Code);

                var body = string.Format(Constants.MailBodies.FieldReport
                                         , now
                                         , assignment?.Description
                                         , machine?.Model
                                         , machine?.SerialNumber
                                         , ($"{assignment.Location.Department} {assignment.Location.Province} {assignment.Location.District}")
                                         , lead?.Name
                                         , lead?.Celullar
                                         , lead?.Email
                                         , supervisor?.Name
                                         , supervisor?.Email
                                         , logo);
                if (userId == lead.Id.Value)
                {
                    mail.PostSendEmail(userNames, subject, body);
                }
                return;
            }

            if (notify)
            {
                switch (type)
                {
                case ActivityType.Traveling:
                    var    subject1     = string.Format(Constants.Mailsubject.Traveling, workOrder.Code);
                    string assignedTec1 = string.Empty;
                    foreach (var item in contactsFerreyros)
                    {
                        var isSupervisor = item.UserType == 1 ? "( Supervisor )" : "";
                        assignedTec1 = assignedTec1 + $"<li> {item.Name} {item.LastName} {isSupervisor}</li>";
                    }

                    var body1 = string.Format(Constants.MailBodies.Traveling
                                              , now
                                              , ($"{assignment.Location.Department} {assignment.Location.Province} {assignment.Location.District}")
                                              , assignment?.Description
                                              , machine?.Model
                                              , machine?.SerialNumber
                                              , assignedTec1
                                              , lead?.Name
                                              , lead?.Celullar
                                              , lead?.Email
                                              , supervisor?.Name
                                              , supervisor?.Email
                                              , logo);

                    if (userId == lead.Id.Value)
                    {
                        mail.PostSendEmail(userNames, subject1, body1);
                    }

                    break;

                case ActivityType.Driving:

                    var    subject2     = string.Format(Constants.Mailsubject.Driving, workOrder.Code);
                    string assignedTec2 = string.Empty;
                    foreach (var item in contactsFerreyros)
                    {
                        var isSupervisor = item.UserType == 1 ? "( Supervisor )" : "";
                        assignedTec2 = assignedTec2 + $"<li> {item.Name} {item.LastName} {isSupervisor}</li>";
                    }

                    var body2 = string.Format(Constants.MailBodies.Driving
                                              , now
                                              , ($"{assignment.Location.Department} {assignment.Location.Province} {assignment.Location.District}")
                                              , assignment?.Description
                                              , machine?.Model
                                              , machine?.SerialNumber
                                              , assignedTec2
                                              , lead?.Name
                                              , lead?.Celullar
                                              , lead?.Rpm
                                              , lead?.Email
                                              , supervisor?.Name
                                              , supervisor?.Celullar
                                              , supervisor?.Email
                                              , logo);
                    if (userId == lead.Id.Value)
                    {
                        mail.PostSendEmail(userNames, subject2, body2);
                    }

                    break;

                case ActivityType.FieldService:
                    var subject3 = string.Format(Constants.Mailsubject.FieldService, workOrder.Code);

                    var body3 = string.Format(Constants.MailBodies.FieldService
                                              , now
                                              , ($"{assignment.Location.Department} {assignment.Location.Province} {assignment.Location.District}")
                                              , assignment?.Description
                                              , machine?.Model
                                              , machine?.SerialNumber
                                              , lead == null ? "" : lead.Name
                                              , lead == null ? "" : lead.Celullar
                                              , lead == null ? "" : lead.Rpm
                                              , lead == null ? "" : lead.Email
                                              , supervisor == null ? "" : supervisor.Name
                                              , supervisor == null ? "" : supervisor.Celullar
                                              , supervisor == null ? "" : supervisor.Email
                                              , logo);
                    if (userId == lead.Id.Value)
                    {
                        mail.PostSendEmail(userNames, subject3, body3);
                    }
                    break;

                case ActivityType.StandByClient:
                    var subject4 = string.Format(Constants.Mailsubject.StandByClient, workOrder.Code);

                    var body4 = string.Format(Constants.MailBodies.StandByClient
                                              , now
                                              , assignment?.Description
                                              , machine?.Model
                                              , machine?.SerialNumber
                                              , supervisor?.Name
                                              , supervisor?.Email
                                              , lead?.Name
                                              , lead?.Celullar
                                              , lead?.Email
                                              , logo);
                    if (userId == lead.Id.Value)
                    {
                        mail.PostSendEmail(userNames, subject4, body4);
                    }
                    break;

                case ActivityType.StandByFesa:
                    var subject5 = string.Format(Constants.Mailsubject.StandByFesa, workOrder.Code);

                    var body5 = string.Format(Constants.MailBodies.StandByFesa
                                              , now
                                              , assignment?.Description
                                              , machine?.Model
                                              , machine?.SerialNumber
                                              , supervisor?.Name
                                              , supervisor?.Celullar
                                              , supervisor?.Email
                                              , lead?.Name
                                              , lead?.Celullar
                                              , lead?.Email
                                              , logo);
                    if (userId == lead.Id.Value)
                    {
                        mail.PostSendEmail(userNames, subject5, body5);
                    }
                    break;
                }
            }
        }