⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 notifyutility.cs

📁 一个ASP.NET下的中文内容管理和社区系统
💻 CS
字号:
namespace ASPNET.StarterKit.Communities {

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web.Mail;


    //*********************************************************************
    //
    // NotifyUtility Class
    //
    // Contains static methods for working with notification
    // emails.
    //
    //*********************************************************************

    public class NotifyUtility {



        //*********************************************************************
        //
        // GetNotificationStatus Method
        //
        // Returns true if the user is receiving notifications for
        // this section or content page. 
        //
        //*********************************************************************
        
        public static bool GetNotificationStatus(int contentPageID, int sectionID, string username) {
            SqlConnection conPortal = new SqlConnection(CommunityGlobals.ConnectionString);
            SqlCommand cmd = new SqlCommand("Community_NotifyGet", conPortal);
            cmd.CommandType = CommandType.StoredProcedure;
            
            cmd.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
            cmd.Parameters.Add("@sectionID", sectionID);
            cmd.Parameters.Add("@username", username);
            cmd.Parameters.Add("@contentPageID", contentPageID);
            cmd.Parameters.Add("@status", SqlDbType.Bit).Direction = ParameterDirection.Output;
            
            conPortal.Open();
            cmd.ExecuteNonQuery();
            bool result = (bool)cmd.Parameters["@status"].Value;
            conPortal.Close();
            
            return result;
        } 


        //*********************************************************************
        //
        // UpdateNotificationStatus Method
        //
        // Updates user notification status in the database. 
        //
        //*********************************************************************

        public static void UpdateNotificationStatus(int contentPageID, int sectionID, string username, bool status) {
            SqlConnection conPortal = new SqlConnection(CommunityGlobals.ConnectionString);
            SqlCommand cmd = new SqlCommand("Community_NotifyUpdate", conPortal);
            cmd.CommandType = CommandType.StoredProcedure;
            
            cmd.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
            cmd.Parameters.Add("@sectionID", sectionID);
            cmd.Parameters.Add("@username", username);
            cmd.Parameters.Add("@contentPageID", contentPageID);
            cmd.Parameters.Add("@status", status);
            
            conPortal.Open();
            cmd.ExecuteNonQuery();
            conPortal.Close();
        } 


        //*********************************************************************
        //
        // SendNotifications Method
        //
        // Sends notification emails to interested users. 
        //
        //*********************************************************************
        
        public static void SendNotifications(int sectionID, int contentPageID, string contentTitle, string username) {
            NotifyFormatInfo formatInfo = new NotifyFormatInfo();
            string mailSubject;
            string mailBody;
            MailMessage mailMessage = null;
            
            // Get the notification message
            MessageInfo notifyEmail = MessageUtility.GetMessage("Email Notification");
            
            // Get the Smtp server
            string smtpServer = CommunityGlobals.SmtpServer;
        
            // Get base url
            string baseUrl = CommunityGlobals.ResolveAbsoluteUrl(CommunityGlobals.AppPath);
        
            // Set common format info properties
            formatInfo.EditProfileLink = baseUrl + "/Users_EditProfile.aspx";
            formatInfo.SectionName = SectionUtility.GetSectionNameFromID(sectionID);
            formatInfo.ContentLink = CommunityGlobals.ResolveAbsoluteUrl(ContentPageUtility.CalculateContentPath(sectionID, contentPageID));
            formatInfo.ContentTitle = contentTitle;
        
            // Get the notifications
            SqlConnection conPortal = new SqlConnection(CommunityGlobals.ConnectionString);
            SqlCommand cmdGet = new SqlCommand("Community_NotifySendNotifications", conPortal);
            cmdGet.CommandType = CommandType.StoredProcedure;
            
            cmdGet.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
            cmdGet.Parameters.Add("@sectionID", sectionID);
            cmdGet.Parameters.Add("@contentPageID", contentPageID);

            conPortal.Open();
            SqlDataReader dr = cmdGet.ExecuteReader();
            while (dr.Read()) {
                mailSubject = notifyEmail.Title;
                mailBody = notifyEmail.Body;
                
                formatInfo.Username = (string)dr["user_username"]; 
                
                mailMessage = new MailMessage();    
                mailMessage.From = "notify@" + CommunityGlobals.PrimaryDomain;
                mailMessage.To = (string)dr["User_Email"];
                mailMessage.Subject = FormatEmail(formatInfo, mailSubject);
                mailMessage.Body = FormatEmail(formatInfo, mailBody);
                
                SmtpMail.SmtpServer = smtpServer;
                SmtpMail.Send(mailMessage); 
            }
            conPortal.Close();
        }



        //*********************************************************************
        //
        // FormatEmail Method
        //
        // Formats notification email. 
        //
        //*********************************************************************
        
        public static string FormatEmail(NotifyFormatInfo formatInfo, string text) {
            // Perform replacements
            text = text.Replace("<Username>", formatInfo.Username);
            text = text.Replace("<EditProfileLink>", formatInfo.EditProfileLink);            
            text = text.Replace("<SectionName>", formatInfo.SectionName);
            text = text.Replace("<ContentLink>", formatInfo.ContentLink);
            text = text.Replace("<ContentTitle>", formatInfo.ContentTitle);
            
            return text;
        }


        //*********************************************************************
        //
        // NotifyUtility Constructor
        //
        // Private constructor in the case of a class with static methods. 
        //
        //*********************************************************************
        
        private NotifyUtility() {}
        
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -