Exemplo n.º 1
0
        /// <summary>
        /// Notifies the admins.
        /// </summary>
        /// <param name="subject">The subject.</param>
        /// <param name="message">The message.</param>
        /// <param name="appRoot">The application root.</param>
        /// <param name="themeRoot">The theme root.</param>
        /// <exception cref="System.Exception">
        /// Error sending System Email: Could not read Email Medium Entity Type
        /// </exception>
        public static void NotifyAdmins(string subject, string message, string appRoot = "", string themeRoot = "")
        {
            try
            {
                List <string> recipients = null;

                Guid adminGroup = Rock.SystemGuid.Group.GROUP_ADMINISTRATORS.AsGuid();
                using (var rockContext = new RockContext())
                {
                    recipients = new GroupMemberService(rockContext).Queryable()
                                 .Where(m =>
                                        m.Group.Guid.Equals(adminGroup) &&
                                        m.GroupMemberStatus == GroupMemberStatus.Active &&
                                        m.Person.Email != null &&
                                        m.Person.Email != "")
                                 .Select(m => m.Person.Email)
                                 .ToList();
                }

                if (recipients != null && recipients.Any())
                {
                    var mediumEntity = EntityTypeCache.Read(Rock.SystemGuid.EntityType.COMMUNICATION_MEDIUM_EMAIL.AsGuid());
                    if (mediumEntity != null)
                    {
                        var medium = MediumContainer.GetComponent(mediumEntity.Name);
                        if (medium != null && medium.IsActive)
                        {
                            var transport = medium.Transport;
                            if (transport != null && transport.IsActive)
                            {
                                try
                                {
                                    transport.Send(recipients, null, subject, message, appRoot, themeRoot);
                                }
                                catch (Exception ex1)
                                {
                                    throw new Exception(string.Format("Error sending System Email ({0}).", subject), ex1);
                                }
                            }
                            else
                            {
                                throw new Exception(string.Format("Error sending System Email: The '{0}' medium does not have a valid transport, or the transport is not active.", mediumEntity.FriendlyName));
                            }
                        }
                        else
                        {
                            throw new Exception(string.Format("Error sending System Email: The '{0}' medium does not exist, or is not active (type: {1}).", mediumEntity.FriendlyName, mediumEntity.Name));
                        }
                    }
                    else
                    {
                        throw new Exception("Error sending System Email: Could not read Email Medium Entity Type");
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionLogService.LogException(ex, HttpContext.Current);
            }
        }
Exemplo n.º 2
0
        private static CreateMessageResult CreateSmsMessage(Person person, Dictionary <string, object> mergeObjects, SystemCommunication systemCommunication)
        {
            var isSmsEnabled        = MediumContainer.HasActiveSmsTransport() && !string.IsNullOrWhiteSpace(systemCommunication.SMSMessage);
            var createMessageResult = new CreateMessageResult();
            var smsNumber           = person.PhoneNumbers.GetFirstSmsNumber();
            var recipients          = new List <RockMessageRecipient>();

            if (string.IsNullOrWhiteSpace(smsNumber) || !isSmsEnabled)
            {
                var smsWarningMessage = $"No SMS number could be found for {person.FullName}.";
                if (!isSmsEnabled)
                {
                    smsWarningMessage = $"SMS is not enabled. {person.FullName} did not receive a notification.";
                }

                RockLogger.Log.Warning(RockLogDomains.Jobs, smsWarningMessage);
                createMessageResult.Warnings.Add(smsWarningMessage);
                return(createMessageResult);
            }

            recipients.Add(new RockSMSMessageRecipient(person, smsNumber, mergeObjects));

            var message = new RockSMSMessage(systemCommunication);

            message.SetRecipients(recipients);
            createMessageResult.Message = message;
            return(createMessageResult);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Sends this message(Email, SMS, or PushDevice, depending which RockMessage class you are using).
        /// NOTE: Email exceptions will be logged to exception log, but won't throw an exception. Ensure you check for error messages and the boolean value to handle error causes where a communication may not be sent.
        /// </summary>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        public virtual bool Send(out List <string> errorMessages)
        {
            errorMessages = new List <string>();

            try
            {
                if (this.Recipients.Any())
                {
                    var mediumEntity = EntityTypeCache.Get(MediumEntityTypeId);
                    if (mediumEntity != null)
                    {
                        var medium = MediumContainer.GetComponent(mediumEntity.Name);
                        if (medium != null)
                        {
                            medium.Send(this, out errorMessages);
                            return(!errorMessages.Any());
                        }
                    }

                    errorMessages.Add("Could not find valid Medium");
                    return(false);
                }

                return(true);
            }
            catch (Exception ex)
            {
                ExceptionLogService.LogException(ex, HttpContext.Current);
                errorMessages.Add(ex.Message);
                return(false);
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Sends the specified email template unique identifier.
 /// </summary>
 /// <param name="emailTemplateGuid">The email template unique identifier.</param>
 /// <param name="recipients">The recipients.</param>
 /// <param name="appRoot">The application root.</param>
 /// <param name="themeRoot">The theme root.</param>
 public static void Send(Guid emailTemplateGuid, List <RecipientData> recipients, string appRoot = "", string themeRoot = "")
 {
     try
     {
         if (recipients != null && recipients.Any())
         {
             var mediumEntity = EntityTypeCache.Read(Rock.SystemGuid.EntityType.COMMUNICATION_MEDIUM_EMAIL.AsGuid());
             if (mediumEntity != null)
             {
                 var medium = MediumContainer.GetComponent(mediumEntity.Name);
                 if (medium != null && medium.IsActive)
                 {
                     var transport = medium.Transport;
                     if (transport != null && transport.IsActive)
                     {
                         using (var rockContext = new RockContext())
                         {
                             var template = new SystemEmailService(rockContext).Get(emailTemplateGuid);
                             if (template != null)
                             {
                                 try
                                 {
                                     transport.Send(template, recipients, appRoot, themeRoot);
                                 }
                                 catch (Exception ex1)
                                 {
                                     throw new Exception(string.Format("Error sending System Email ({0}).", template.Title), ex1);
                                 }
                             }
                             else
                             {
                                 throw new Exception(string.Format("Error sending System Email: An invalid System Email Identifier was provided ({0}).", emailTemplateGuid.ToString()));
                             }
                         }
                     }
                     else
                     {
                         throw new Exception(string.Format("Error sending System Email: The '{0}' medium does not have a valid transport, or the transport is not active.", mediumEntity.FriendlyName));
                     }
                 }
                 else
                 {
                     throw new Exception(string.Format("Error sending System Email: The '{0}' medium does not exist, or is not active (type: {1}).", mediumEntity.FriendlyName, mediumEntity.Name));
                 }
             }
             else
             {
                 throw new Exception("Error sending System Email: Could not read Email Medium Entity Type");
             }
         }
     }
     catch (Exception ex)
     {
         ExceptionLogService.LogException(ex, HttpContext.Current);
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// Sends the specified from email.
 /// </summary>
 /// <param name="fromEmail">From email.</param>
 /// <param name="fromName">From name.</param>
 /// <param name="subject">The subject.</param>
 /// <param name="recipients">The recipients.</param>
 /// <param name="message">The message.</param>
 /// <param name="appRoot">The application root.</param>
 /// <param name="themeRoot">The theme root.</param>
 /// <param name="attachments">The attachments.</param>
 /// <param name="createCommunicationHistory">if set to <c>true</c> [create communication history].</param>
 /// <exception cref="System.Exception">Error sending System Email: Could not read Email Medium Entity Type</exception>
 public static void Send(string fromEmail, string fromName, string subject, List <string> recipients, string message, string appRoot = "", string themeRoot = "", List <Attachment> attachments = null, bool createCommunicationHistory = true)
 {
     try
     {
         if (recipients != null && recipients.Any())
         {
             var mediumEntity = EntityTypeCache.Read(Rock.SystemGuid.EntityType.COMMUNICATION_MEDIUM_EMAIL.AsGuid());
             if (mediumEntity != null)
             {
                 var medium = MediumContainer.GetComponent(mediumEntity.Name);
                 if (medium != null && medium.IsActive)
                 {
                     var transport = medium.Transport;
                     if (transport != null && transport.IsActive)
                     {
                         try
                         {
                             if (transport is Rock.Communication.Transport.SMTPComponent)
                             {
                                 ((Rock.Communication.Transport.SMTPComponent)transport).Send(recipients, fromEmail, fromName ?? string.Empty, subject, message, appRoot, themeRoot, attachments, createCommunicationHistory);
                             }
                             else
                             {
                                 transport.Send(recipients, fromEmail, fromName ?? string.Empty, subject, message, appRoot, themeRoot, attachments);
                             }
                         }
                         catch (Exception ex1)
                         {
                             throw new Exception(string.Format("Error sending System Email ({0}).", subject), ex1);
                         }
                     }
                     else
                     {
                         throw new Exception(string.Format("Error sending System Email: The '{0}' medium does not have a valid transport, or the transport is not active.", mediumEntity.FriendlyName));
                     }
                 }
                 else
                 {
                     throw new Exception(string.Format("Error sending System Email: The '{0}' medium does not exist, or is not active (type: {1}).", mediumEntity.FriendlyName, mediumEntity.Name));
                 }
             }
             else
             {
                 throw new Exception("Error sending System Email: Could not read Email Medium Entity Type");
             }
         }
     }
     catch (Exception ex)
     {
         ExceptionLogService.LogException(ex, HttpContext.Current);
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// Sends the asynchronous.
        /// </summary>
        /// <returns></returns>
        public virtual async Task <SendMessageResult> SendAsync()
        {
            var sendEmailResult = new SendMessageResult();

            try
            {
                if (this.Recipients.Any())
                {
                    var mediumEntity = EntityTypeCache.Get(MediumEntityTypeId);
                    if (mediumEntity != null)
                    {
                        var medium = MediumContainer.GetComponent(mediumEntity.Name);
                        if (medium != null)
                        {
                            var iAsyncMedium = medium as IAsyncMediumComponent;
                            if (iAsyncMedium == null)
                            {
                                medium.Send(this, out var errorMessages);
                                sendEmailResult.Errors.AddRange(errorMessages);
                            }
                            else
                            {
                                sendEmailResult = await iAsyncMedium.SendAsync(this).ConfigureAwait(false);
                            }
                            return(sendEmailResult);
                        }
                    }

                    sendEmailResult.Errors.Add("Could not find valid Medium");
                    return(sendEmailResult);
                }

                return(sendEmailResult);
            }
            catch (Exception ex)
            {
                ExceptionLogService.LogException(ex, HttpContext.Current);
                sendEmailResult.Errors.Add(ex.Message);
                return(sendEmailResult);
            }
        }
Exemplo n.º 7
0
 /// <summary>
 /// Determines whether an active push transport exists.
 /// </summary>
 /// <returns>
 ///   <c>true</c> if an active push transport exists; otherwise, <c>false</c>.
 /// </returns>
 public static bool HasActivePushTransport()
 {
     return(MediumContainer.HasActiveTransport(Rock.SystemGuid.EntityType.COMMUNICATION_MEDIUM_PUSH_NOTIFICATION.AsGuid()));
 }
Exemplo n.º 8
0
 /// <summary>
 /// Determines whether an active email transport exists.
 /// </summary>
 /// <returns>
 ///   <c>true</c> if an active email transport exists; otherwise, <c>false</c>.
 /// </returns>
 public static bool HasActiveEmailTransport()
 {
     return(MediumContainer.HasActiveTransport(Rock.SystemGuid.EntityType.COMMUNICATION_MEDIUM_EMAIL.AsGuid()));
 }
Exemplo n.º 9
0
 /// <summary>
 /// Determines whether a transport is active for the specified unique identifier.
 /// </summary>
 /// <returns>
 ///   <c>true</c> if an active transport exists for the specified unique identifier; otherwise, <c>false</c>.
 /// </returns>
 public static bool HasActiveTransport(Guid guid)
 {
     return(MediumContainer.GetActiveMediumComponentsWithActiveTransports().Any(a => a.EntityType.Guid == guid));
 }