CreateEmailCommunication() public method

Creates the email communication.
public CreateEmailCommunication ( List recipientEmails, string fromName, string fromAddress, string replyTo, string subject, string htmlMessage, string textMessage, bool bulkCommunication, CommunicationRecipientStatus recipientStatus = CommunicationRecipientStatus.Delivered, int senderPersonAliasId = null ) : Communication
recipientEmails List The recipient emails.
fromName string From name.
fromAddress string From address.
replyTo string The reply to.
subject string The subject.
htmlMessage string The HTML message.
textMessage string The text message.
bulkCommunication bool if set to true [bulk communication].
recipientStatus CommunicationRecipientStatus The recipient status.
senderPersonAliasId int The sender person alias identifier.
return Communication
コード例 #1
0
ファイル: SMTPComponent.cs プロジェクト: azturner/Rock
        /// <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 globalConfigValues = Rock.Web.Cache.GlobalAttributesCache.GetMergeFields( null );
                from = from.ResolveMergeFields( globalConfigValues );
                fromName = fromName.ResolveMergeFields( globalConfigValues );

                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() )
                {
                    using ( var rockContext = new RockContext() )
                    {
                        int? senderPersonAliasId = null;
                        if ( createCommunicationHistory )
                        {
                            var sender = new PersonService( rockContext )
                                .Queryable().AsNoTracking()
                                .Where( p => p.Email == from )
                                .FirstOrDefault();
                            senderPersonAliasId = sender != null ? sender.PrimaryAliasId : (int?)null;
                        }

                        var communicationService = new CommunicationService( rockContext );

                        foreach ( var recipientData in recipients )
                        {
                            foreach ( var g in globalConfigValues )
                            {
                                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 );

                                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( @" href=""/", @" href=""" + appRoot );
                                }

                                message.Subject = subject;
                                message.Body = body;

                                smtpClient.Send( message );

                                if ( createCommunicationHistory )
                                {
                                    communicationService.CreateEmailCommunication(
                                        new List<string> { to }, fromName, from, from, subject, body, string.Empty, false,
                                        CommunicationRecipientStatus.Delivered, senderPersonAliasId );
                                }
                            }
                        }

                        if ( createCommunicationHistory )
                        {
                            rockContext.SaveChanges();
                        }
                    }
                }
            }
        }