sendcommentmail.cs

来自「个人博客系统」· CS 代码 · 共 48 行

CS
48
字号
#region using

using System;
using System.Web;
using BlogEngine.Core.Web.Controls;
using BlogEngine.Core;
using System.Net.Mail;
using System.Threading;

#endregion

/// <summary>
/// Sends an e-mail to the blog owner whenever a comment is added.
/// </summary>
[Extension("Sends an e-mail to the blog owner whenever a comment is added", "", "")]
public class SendCommentMail
{

  /// <summary>
  /// Hooks up an event handler to the Post.CommentAdded event.
  /// </summary>
  public SendCommentMail()
  {
    Post.CommentAdded += new EventHandler<EventArgs>(Post_CommentAdded);
  }

  private void Post_CommentAdded(object sender, EventArgs e)
  {
    Post post = ((Comment)sender).Post;
    if (post != null && BlogSettings.Instance.SendMailOnComment && !Thread.CurrentPrincipal.Identity.IsAuthenticated)
    {
      Comment comment = post.Comments[post.Comments.Count - 1];
      // Trackback and pingback comments don't have a '@' symbol in the e-mail field.
      string from = comment.Email.Contains("@") ? comment.Email : BlogSettings.Instance.Email;

      MailMessage mail = new MailMessage();
      mail.From = new MailAddress(from, comment.Author);
      mail.To.Add(BlogSettings.Instance.Email);
      mail.Subject = "Weblog comment on " + post.Title;
      mail.Body = "Comment by " + comment.Author + " (" + comment.Email + ")" + Environment.NewLine + Environment.NewLine;
      mail.Body += comment.Content + "\n\n" + post.PermaLink + "#id_" + comment.Id;

      Utils.SendMailMessageAsync(mail);
    }
  }

}

⌨️ 快捷键说明

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