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

📄 mail.cs

📁 自己做的的一个小的邮件系统,上传上去大家一起分享一下
💻 CS
字号:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using System.Data.SqlClient;
public interface IMail
{ 
    //获取所有的邮件
    SqlDataReader GetMailsByFolder(int nFolderID);

    //删除邮件
    void DeleteMail(int nMailID);

    //移动邮件到某个邮箱里
    void MoveMail(int nMailID,int nFolderID);

    //发送mail
    bool SendMail(string nTitle,string nBody, string nFromAddress, string nToAddress, bool nAttachmentFlag,bool nReaderFlag,int nContain,int nFolderID);

    //删除垃圾箱里面的邮件彻底删除。。。
  void DeleteMailT(int nMailID); 

    //得到MAIL的详细信息
    SqlDataReader GetMailDetails(int nMailID,int nFolderID);

}

/// <summary>
/// Mail 的摘要说明
/// </summary>
public class Mail:IMail
{
    public SqlDataReader GetMailsByFolder(int nFolderID)
    {

        string connStr = ConfigurationManager.ConnectionStrings["DBConnectionStrings"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        string SqlStr = "";
        switch (nFolderID)
        {
            case 8:
                SqlStr = "select * from Mails where FolderID=" + nFolderID + " and Trash=1 order by MailID desc";
                break;
            default:
                 SqlStr = "select * from Mails where FolderID=" + nFolderID + " and Trash=0 order by MailID desc";
                break;

         }
        

        SqlDataReader dr = null;
        try
        {
            if (conn.State.ToString() == "Closed")
                conn.Open();
            SqlCommand comm = new SqlCommand(SqlStr, conn);
            dr = comm.ExecuteReader(CommandBehavior.CloseConnection);




        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message,ex);
        }

        return dr;
    }


    public void DeleteMail(int nMailID)
    {
        string connStr = ConfigurationManager.ConnectionStrings["DBConnectionStrings"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);

        string SqlStr = "update Mails set Trash=1,FolderID=8 where MailID=" + nMailID;

        try
        {
            if (conn.State.ToString() == "Closed")
                conn.Open();

            SqlCommand comm = new SqlCommand(SqlStr, conn);
            comm.ExecuteNonQuery();
            comm.Dispose();

        }
        catch (Exception ex)
        {
            if (conn.State.ToString() == "Open")
                conn.Close();
            throw new Exception(ex.Message,ex);
        }
    }

    public void MoveMail(int nMailID, int nFolderID)
    {
        string connStr = ConfigurationManager.ConnectionStrings["DBConnectionStrings"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);

        string SqlStr = "update Mails set FolderID="+nFolderID+"where MailID="+nMailID;

        try
        {
            if (conn.State.ToString() == "Closed")
                conn.Open();
            SqlCommand comm = new SqlCommand(SqlStr, conn);
            comm.ExecuteNonQuery();
            comm.Dispose();
            if (conn.State.ToString() == "Open")
                conn.Close();

        }
        catch (Exception ex)
        {
           
            throw new Exception(ex.Message, ex);
        }
    }


    public bool SendMail(string nTitle,string nBody, string nFromAddress, string nToAddress,bool nAttachmentFlag, bool nReaderFlag,int nContain, int nFolderID)
    {
        string connStr = ConfigurationManager.ConnectionStrings["DBConnectionStrings"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        if (conn.State.ToString() == "Closed")
            conn.Open();
        bool isOk;
        SqlCommand comm = new SqlCommand("insert into Mails (Title,Body,FromAddress,ToAddress,AttachmentFlag,ReaderFlag,Contain,FolderID) values (@nTitle,@nBody,@nFromAddress,@nToAddress,@nAttachmentFlag,@nReaderFlag,@nContain,@nFolderID)",conn);
        comm.Parameters.Add("@nTitle", SqlDbType.VarChar, 255);
        comm.Parameters.Add("@nBody",SqlDbType.Text,2147483647);
        comm.Parameters.Add("@nFromAddress", SqlDbType.Text, 2147483647);
        comm.Parameters.Add("@nToAddress", SqlDbType.Text, 2147483647);
        comm.Parameters.Add("@nAttachmentFlag", SqlDbType.Bit, 1);
        comm.Parameters.Add("@nReaderFlag", SqlDbType.Bit, 1);
        comm.Parameters.Add("@nContain",SqlDbType.Int,4);
        comm.Parameters.Add("@nFolderID", SqlDbType.Int, 4);
        comm.Parameters["@nTitle"].Value = nTitle;
        comm.Parameters["@nBody"].Value=nBody;
        comm.Parameters["@nFromAddress"].Value = nFromAddress;
        comm.Parameters["@nToAddress"].Value = nToAddress;
        comm.Parameters["@nAttachmentFlag"].Value = nAttachmentFlag;
        comm.Parameters["@nReaderFlag"].Value = nReaderFlag;
        comm.Parameters["@nContain"].Value=nContain;
        comm.Parameters["@nFolderID"].Value = nFolderID;

         try
        {
             int ok = comm.ExecuteNonQuery();
              comm.Dispose();

              if (ok > 0)
              {
                  isOk = true;
              }
              else
              {
                  isOk = false;
              }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message, ex);
        }
        return isOk;
    }

    public void DeleteMailT(int nMailID)
    {
        string connStr = ConfigurationManager.ConnectionStrings["DBConnectionStrings"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);

        string SqlStr = "delete Mails where MailID=" + nMailID;

        try
        {
            if (conn.State.ToString() == "Closed")
                conn.Open();

            SqlCommand comm = new SqlCommand(SqlStr, conn);
            comm.ExecuteNonQuery();
            comm.Dispose();

        }
        catch (Exception ex)
        {
            if (conn.State.ToString() == "Open")
                conn.Close();
            throw new Exception(ex.Message, ex);
        }
    }


//该源码下载自www.51aspx.com(51aspx.com)

    public SqlDataReader GetMailDetails(int nMailID, int nFolderID)
    {
        string connStr = ConfigurationManager.ConnectionStrings["DBConnectionStrings"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        string SqlGetMail = "select * from Mails where MailID="+nMailID+"and FolderID="+nFolderID;

        SqlDataReader dr = null;
        try
        {
            if (conn.State.ToString() == "Closed")
                conn.Open();
            SqlCommand comm = new SqlCommand(SqlGetMail, conn);
            dr = comm.ExecuteReader();
            comm.Dispose();

        }
        catch (Exception ex)
        {
            if (conn.State.ToString() == "Open")
                conn.Close();
            throw new Exception(ex.Message, ex);
        }
        return dr;
    }
}
//该源码下载自www.51aspx.com(51aspx.com)

⌨️ 快捷键说明

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