Ejemplo n.º 1
0
        public static async void SendAlertSOSEmail(seekios_dbEntities seekiosEntities
                                                   , IEnumerable <string> emailsTo
                                                   , string firstName
                                                   , string lastName
                                                   , string seekiosName
                                                   , double?lat
                                                   , double?longi
                                                   , string alertContent
                                                   , int idcountryResources
                                                   , bool isGPS = true)
        {
            var latitude = lat != null?lat.ToString().Replace(",", ".") : string.Empty;

            var longitude = longi != null?longi.ToString().Replace(",", ".") : string.Empty;

            dynamic sendGridClient  = new SendGridClient(_API_KEY);
            var     contentEmailBdd = (from cr in seekiosEntities.countryResources
                                       where cr.idcountryResources == idcountryResources
                                       select cr).Take(1).First();

            var emailContent  = string.IsNullOrEmpty(alertContent) ? contentEmailBdd.alertSOSEmailContent : contentEmailBdd.alertSOSEmailWithMsgContent;
            var emailSendFrom = new EmailAddress(_EMAIL_SENDER);
            var emailSendTo   = new List <EmailAddress>();

            foreach (var email in emailsTo)
            {
                emailSendTo.Add(new EmailAddress()
                {
                    Email = email
                });
            }

            // add the text body
            string finalAlertContent = string.IsNullOrEmpty(alertContent) ?
                                       emailContent.Replace("{0}", firstName)
                                       .Replace("{1}", lastName)
                                       .Replace("{2}", seekiosName)
                                       .Replace("{3}", isGPS ? ResourcesHelper.GetLocalizedString("GPS", idcountryResources) : ResourcesHelper.GetLocalizedString("ApproxPosition", idcountryResources))
                                       .Replace("{4}", latitude)
                                       .Replace("{5}", longitude)
                : emailContent.Replace("{0}", firstName)
                                       .Replace("{1}", lastName)
                                       .Replace("{2}", seekiosName)
                                       .Replace("{3}", alertContent)
                                       .Replace("{4}", isGPS ? ResourcesHelper.GetLocalizedString("GPS", idcountryResources) : ResourcesHelper.GetLocalizedString("ApproxPosition", idcountryResources))
                                       .Replace("{5}", latitude)
                                       .Replace("{6}", longitude);

            var content  = new Content(_ACCOUNT_VERIFICATION_EMAIL_CONTENT_TYPE, finalAlertContent);
            var mail     = MailHelper.CreateSingleEmailToMultipleRecipients(emailSendFrom, emailSendTo, contentEmailBdd.alertSOSEmailTitle, null, content.Value);
            var response = await sendGridClient.SendEmailAsync(mail);
        }