当前位置: 首页>>代码示例>>C#>>正文


C# CommunicationService.CreateEmailCommunication方法代码示例

本文整理汇总了C#中Rock.Model.CommunicationService.CreateEmailCommunication方法的典型用法代码示例。如果您正苦于以下问题:C# CommunicationService.CreateEmailCommunication方法的具体用法?C# CommunicationService.CreateEmailCommunication怎么用?C# CommunicationService.CreateEmailCommunication使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Rock.Model.CommunicationService的用法示例。


在下文中一共展示了CommunicationService.CreateEmailCommunication方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Send


//.........这里部分代码省略.........
                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();
                        }
                    }
                }
            }
        }
开发者ID:azturner,项目名称:Rock,代码行数:101,代码来源:SMTPComponent.cs


注:本文中的Rock.Model.CommunicationService.CreateEmailCommunication方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。