/// <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); } }
/// <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); } }
/// <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); } }
/// <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); } }
/// <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); } }