/// <summary> /// Sends the specified medium data to the specified list of recipients. /// </summary> /// <param name="mediumData">The medium data.</param> /// <param name="recipients">The recipients.</param> /// <param name="appRoot">The application root.</param> /// <param name="themeRoot">The theme root.</param> /// <param name="createCommunicationHistory">if set to <c>true</c> [create communication history].</param> /// <param name="metaData">The meta data.</param> public void Send( Dictionary<string, string> mediumData, List<string> recipients, string appRoot, string themeRoot, bool createCommunicationHistory, Dictionary<string, string> metaData ) { try { var globalAttributes = GlobalAttributesCache.Read(); string from = string.Empty; string fromName = string.Empty; mediumData.TryGetValue( "From", out from ); if ( string.IsNullOrWhiteSpace( from ) ) { from = globalAttributes.GetValue( "OrganizationEmail" ); fromName = globalAttributes.GetValue( "OrganizationName" ); } if ( !string.IsNullOrWhiteSpace( from ) ) { // Resolve any possible merge fields in the from address var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields( null ); from = from.ResolveMergeFields( mergeFields ); fromName = fromName.ResolveMergeFields( mergeFields ); string subject = string.Empty; mediumData.TryGetValue( "Subject", out subject ); string body = string.Empty; mediumData.TryGetValue( "Body", out body ); if ( !string.IsNullOrWhiteSpace( themeRoot ) ) { subject = subject.Replace( "~~/", themeRoot ); body = body.Replace( "~~/", themeRoot ); } if ( !string.IsNullOrWhiteSpace( appRoot ) ) { subject = subject.Replace( "~/", appRoot ); body = body.Replace( "~/", appRoot ); body = body.Replace( @" src=""/", @" src=""" + appRoot ); body = body.Replace( @" href=""/", @" href=""" + appRoot ); } MailMessage message = new MailMessage(); if ( string.IsNullOrWhiteSpace( fromName ) ) { message.From = new MailAddress( from ); } else { message.From = new MailAddress( from, fromName ); } // Reply To try { string replyTo = string.Empty; mediumData.TryGetValue( "ReplyTo", out replyTo ); if ( !string.IsNullOrWhiteSpace( replyTo ) ) { message.ReplyToList.Add( new MailAddress( replyTo ) ); } } catch { } CheckSafeSender( message, globalAttributes ); message.IsBodyHtml = true; message.Priority = MailPriority.Normal; message.To.Clear(); recipients.ForEach( r => message.To.Add( r ) ); message.Subject = subject; message.Body = body; AddAdditionalHeaders( message, metaData ); using ( var smtpClient = GetSmtpClient() ) { smtpClient.Send( message ); } if ( createCommunicationHistory ) { var transaction = new SaveCommunicationTransaction( recipients, fromName, from, subject, body ); RockQueue.TransactionQueue.Enqueue( transaction ); } } } catch ( Exception ex ) { ExceptionLogService.LogException( ex, null ); } }
/// <summary> /// Sends the specified recipients. /// </summary> /// <param name="recipients">The recipients.</param> /// <param name="from">From.</param> /// <param name="fromName">From name.</param> /// <param name="subject">The subject.</param> /// <param name="body">The body.</param> /// <param name="appRoot">The application root.</param> /// <param name="themeRoot">The theme root.</param> /// <param name="attachments">Attachments.</param> /// <param name="createCommunicationHistory">if set to <c>true</c> [create communication history].</param> public void Send( List<string> recipients, string from, string fromName, string subject, string body, string appRoot, string themeRoot, List<Attachment> attachments, bool createCommunicationHistory ) { try { var globalAttributes = GlobalAttributesCache.Read(); if ( string.IsNullOrWhiteSpace( from ) ) { from = globalAttributes.GetValue( "OrganizationEmail" ); } if ( !string.IsNullOrWhiteSpace( from ) ) { // Resolve any possible merge fields in the from address var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields( null ); string msgFrom = from.ResolveMergeFields( mergeFields ); string msgSubject = subject; string msgBody = body; if ( !string.IsNullOrWhiteSpace( themeRoot ) ) { msgSubject = msgSubject.Replace( "~~/", themeRoot ); msgBody = msgBody.Replace( "~~/", themeRoot ); } if ( !string.IsNullOrWhiteSpace( appRoot ) ) { msgSubject = msgSubject.Replace( "~/", appRoot ); msgBody = msgBody.Replace( "~/", appRoot ); msgBody = msgBody.Replace( @" src=""/", @" src=""" + appRoot ); msgBody = msgBody.Replace( @" href=""/", @" href=""" + appRoot ); } MailMessage message = new MailMessage(); // set from if ( !string.IsNullOrWhiteSpace( fromName ) ) { message.From = new MailAddress( msgFrom, fromName ); } else { message.From = new MailAddress( msgFrom ); } CheckSafeSender( message, globalAttributes ); message.IsBodyHtml = true; message.Priority = MailPriority.Normal; message.To.Clear(); recipients.ForEach( r => message.To.Add( r ) ); message.Subject = msgSubject; // strip out any unsubscribe links since we don't know the person msgBody = Regex.Replace( msgBody, @"\[\[\s*UnsubscribeOption\s*\]\]", string.Empty ); message.Body = msgBody; // add attachments if ( attachments != null ) { foreach ( var attachment in attachments ) { message.Attachments.Add( attachment ); } } AddAdditionalHeaders( message, null ); using ( var smtpClient = GetSmtpClient() ) { smtpClient.Send( message ); } if ( createCommunicationHistory ) { var transaction = new SaveCommunicationTransaction( recipients, fromName, from, subject, body ); RockQueue.TransactionQueue.Enqueue( transaction ); } } } catch ( Exception ex ) { ExceptionLogService.LogException( ex, null ); } }
/// <summary> /// Sends the specified template. /// </summary> /// <param name="template">The template.</param> /// <param name="recipients">The recipients.</param> /// <param name="appRoot">The application root.</param> /// <param name="themeRoot">The theme root.</param> /// <param name="createCommunicationHistory">if set to <c>true</c> [create communication history].</param> public void Send( SystemEmail template, List<RecipientData> recipients, string appRoot, string themeRoot, bool createCommunicationHistory ) { var globalAttributes = GlobalAttributesCache.Read(); string from = template.From; if ( string.IsNullOrWhiteSpace( from ) ) { from = globalAttributes.GetValue( "OrganizationEmail" ); } string fromName = template.FromName; if ( string.IsNullOrWhiteSpace( fromName ) ) { fromName = globalAttributes.GetValue( "OrganizationName" ); } if ( !string.IsNullOrWhiteSpace( from ) ) { // Resolve any possible merge fields in the from address var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields( null ); from = from.ResolveMergeFields( mergeFields ); fromName = fromName.ResolveMergeFields( mergeFields ); MailMessage message = new MailMessage(); if ( string.IsNullOrWhiteSpace( fromName ) ) { message.From = new MailAddress( from ); } else { message.From = new MailAddress( from, fromName ); } CheckSafeSender( message, globalAttributes ); if ( !string.IsNullOrWhiteSpace( template.Cc ) ) { foreach ( string ccRecipient in template.Cc.SplitDelimitedValues() ) { message.CC.Add( new MailAddress( ccRecipient ) ); } } if ( !string.IsNullOrWhiteSpace( template.Bcc ) ) { foreach ( string ccRecipient in template.Bcc.SplitDelimitedValues() ) { message.Bcc.Add( new MailAddress( ccRecipient ) ); } } message.IsBodyHtml = true; message.Priority = MailPriority.Normal; using ( var smtpClient = GetSmtpClient() ) { foreach ( var recipientData in recipients ) { foreach ( var g in mergeFields ) { if ( recipientData.MergeFields.ContainsKey( g.Key ) ) { recipientData.MergeFields[g.Key] = g.Value; } } // Add the recipients from the template List<string> sendTo = SplitRecipient( template.To ); // Add the recipient from merge data ( if it's not null and not already added ) if ( !string.IsNullOrWhiteSpace( recipientData.To ) && !sendTo.Contains( recipientData.To, StringComparer.OrdinalIgnoreCase ) ) { sendTo.Add( recipientData.To ); } foreach ( string to in sendTo ) { message.To.Clear(); message.To.Add( to ); message.Headers.Clear(); string subject = template.Subject.ResolveMergeFields( recipientData.MergeFields ); string body = Regex.Replace( template.Body.ResolveMergeFields( recipientData.MergeFields ), @"\[\[\s*UnsubscribeOption\s*\]\]", string.Empty ); if ( !string.IsNullOrWhiteSpace( themeRoot ) ) { subject = subject.Replace( "~~/", themeRoot ); body = body.Replace( "~~/", themeRoot ); } if ( !string.IsNullOrWhiteSpace( appRoot ) ) { subject = subject.Replace( "~/", appRoot ); body = body.Replace( "~/", appRoot ); body = body.Replace( @" src=""/", @" src=""" + appRoot ); body = body.Replace( @" src='/", @" src='" + appRoot ); body = body.Replace( @" href=""/", @" href=""" + appRoot ); body = body.Replace( @" href='/", @" href='" + appRoot ); } message.Subject = subject; message.Body = body; AddAdditionalHeaders( message, null ); smtpClient.Send( message ); if ( createCommunicationHistory ) { var transaction = new SaveCommunicationTransaction( to, fromName, from, subject, body ); RockQueue.TransactionQueue.Enqueue( transaction ); } } } } } }