天天看点

CimFax二次开发(C#)帮助类

public class CFXHelper
    {
        #region 常量

        public const int WM_CLIENT_MSG = 0X0400 + 500;
        public const int WM_CLN_DOWN_FILE_BEGIN = WM_CLIENT_MSG + 9;
        public const int WM_CLN_DOWN_FILE_PERCENT = WM_CLIENT_MSG + 10;
        public const int WM_CLN_DOWN_FILE_END = WM_CLIENT_MSG + 11;
        public const int WM_CLN_NOTIFY_RECV_FAX = WM_CLIENT_MSG + 23;
        public const int WM_CLN_NOTIFY_SEND_FAX = WM_CLIENT_MSG + 24;

        #endregion

        #region 私有变量

        private CFXCOMLib.Connection m_Connection;
        private IntPtr m_handle;

        #endregion

        #region 构造函数

        public CFXHelper()
        {
            try
            {
                m_Connection = new CFXCOMLib.Connection();
            }
            catch (Exception ex)
            {
                //未注册COM组件
                throw new Exception(ex.Message + "\r\nPlease register the COM.\r\nUsing \"regsvr32 CFXCOM.dll\"");
            }
        }

        #endregion

        #region 公共方法

        #region 登录相关

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="address">传真设备IP地址</param>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public bool Login(string address, string username, string password)
        {
            string strRet = m_Connection.Login(address, username, password);
            if (strRet == "SUCCESS")
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 注销
        /// </summary>
        /// <returns></returns>
        public bool Logout()
        {
            string strRet = m_Connection.Logout();
            if (strRet == "SUCCESS")
            {
                return true;
            }
            return false;
        }

        #endregion

        #region 发送传真

        /// <summary>
        /// 发送传真
        /// </summary>
        /// <param name="m_strReceiver">接收人传真号</param>
        /// <param name="m_strFaxFile">传真文件路径,必须是*.tif;*.tiff;*.jpg;*.jpeg;*.gif;*.tga;*.png;*.pcx;*.bmp;*.pdf;*.txt;*.rtf;*.doc;*.docx;*.xls;*.xlsx;*.ppt;*.pptx; *.htm;*.html;格式文件</param>
        /// <param name="m_strSender">发送人</param>
        /// <returns>发送成功:返回传真编号(大于0),发送失败:返回-1,出现错误抛出异常</returns>
        public int SendFaxFile(string m_strReceiver, string m_strSender, string m_strFaxFile)
        {
            if (m_Connection.GetLoginInfo(("STATUS")) != ("CONNECTED"))
            {
                throw new Exception(("WARNNING: Please login first."));
            }
            #region 判断文件是否为tif,不是则先转换
            string extension = Path.GetExtension(m_strFaxFile);
            if (string.IsNullOrEmpty(extension))
            {
                throw new Exception("传真文件路径不正确.");
            }
            string newFaxFilePath = string.Empty;
            if (extension.ToLower() != ".tif")
            {
                newFaxFilePath = Path.Combine(Path.GetDirectoryName(m_strFaxFile), Path.GetFileNameWithoutExtension(m_strFaxFile) + ".tif");
                if (!ConvertToTif(m_strFaxFile, newFaxFilePath))
                {
                    throw new Exception("传真文件转换Tif格式失败");
                }
            }
            #endregion

            int nTaskID = m_Connection.StartSendFax(
                m_strReceiver, //Sender, exp: "85566754"    "Jack<85566754>"
                m_strSender, //Sender, exp: "Helen Wang"
                string.IsNullOrEmpty(newFaxFilePath) ? m_strFaxFile : newFaxFilePath, //Fax file path, exp: "d:\\test.tif"
                0, //Task group ID
                2, //Priority  1"Low" 2"Normal" 3"High" 4"Critical"
                3, //Dial Total
                1, //Retry Time, Unit: Minute
                1, //Enable Page Header
                (""),//The time plan to send fax, exp: 2012-07-12 9:30:30
                (""),//Enable sending from this moment, exp: 2012-07-12 10:30:30
                ("")//Disable sending at this moment, exp: 2012-07-12 11:30:30
                );
            //MessageBox(m_Connection.GetLastError()); return;

            if (nTaskID > 0)
            {
                return nTaskID;
            }
            else
            {
                String strErr = m_Connection.GetLastError();
                if (strErr.Length > 0)
                {
                    throw new Exception(strErr);
                }
            }
            return -1;
        }

        /// <summary>
        /// 获取传真发送状态
        /// </summary>
        /// <param name="nTaskID"></param>
        /// <returns></returns>
        public string GetSendFaxStatus(int nTaskID)
        {
            string rel = m_Connection.GetSendFaxStatus(nTaskID);
            return m_Connection.GetValue(rel, "STATUS");
        }

        #endregion

        #region 接收传真

        /// <summary>
        /// 获取一小时前开始到现在的新传真
        /// </summary>
        /// <returns>
        /// 传真字符串,类似:“SERVER: B1238AE7\r\nTASK:123\r\nTIME:2012-07-16 15:26:17\r\nSTATUS:STOP\r\n”
        /// 可以通过m_Connection.GetValue(传真字符串,(参数))获取对应值。如m_Connection.GetValue(strRet, ("TASK")) 获取传真任务编号
        /// </returns>
        public string GetFax()
        {
            if (m_Connection.GetLoginInfo(("STATUS")) != ("CONNECTED"))
            {
                throw new Exception("WARNNING: Please login first.");
            }
            //指定时间:传真时间比指定时间晚,并且传真的任务编号比指定的任务编号更大,才会认作为新传真取得。
            DateTime m_dtFaxDate = DateTime.Now.AddHours(-1);
            //m_nFaxTaskID = 0;
            String strRet = m_Connection.GetNewFax(m_dtFaxDate.Year, m_dtFaxDate.Month, m_dtFaxDate.Day,
            m_dtFaxDate.Hour, m_dtFaxDate.Minute, m_dtFaxDate.Second, 0);
            if (strRet != null)
            {
                if (strRet == "NO NEW FAX")
                {
                    return null;
                }
                else
                {
                    //获取传真数量
                    int nNewFaxCnt = System.Int32.Parse(m_Connection.GetValue(strRet, ("COUNT")));
                    if (nNewFaxCnt > 0)
                    {
                        return strRet;
                    }
                }
            }
            String strErr = m_Connection.GetLastError();
            if (strErr.Length > 0)
            {
                throw new Exception(strErr);
            }

            return null;
        }

        /// <summary>
        /// 获取指定时间之后,并且传真任务编号大于给定整数的新传真
        /// </summary>
        /// <param name="m_dtFaxDate"></param>
        /// <param name="FaxTaskID"></param>
        /// <returns></returns>
        public string GetFax(DateTime m_dtFaxDate, int FaxTaskID)
        {
            if (m_Connection.GetLoginInfo(("STATUS")) != ("CONNECTED"))
            {
                throw new Exception("WARNNING: Please login first.");
            }
            //指定时间:传真时间比指定时间晚,并且传真的任务编号比指定的任务编号更大,才会认作为新传真取得。
            m_dtFaxDate = DateTime.Now.AddHours(-1);
            //m_nFaxTaskID = 0;
            String strRet = m_Connection.GetNewFax(m_dtFaxDate.Year, m_dtFaxDate.Month, m_dtFaxDate.Day,
            m_dtFaxDate.Hour, m_dtFaxDate.Minute, m_dtFaxDate.Second, FaxTaskID);
            if (strRet != null)
            {
                if (strRet == "NO NEW FAX")
                {
                    return null;
                }
                else
                {
                    return strRet;
                }
            }
            else
            {
                String strErr = m_Connection.GetLastError();
                if (strErr.Length > 0)
                {
                    throw new Exception(strErr);
                }
            }
            return null;
        }

        /// <summary>
        /// 下载传真
        /// </summary>
        /// <param name="strSrc">通过GetFax获取的传真信息</param>
        /// <param name="LocalPath">传真文件下载后存储的全路径</param>
        /// <returns>下载传真是否成功</returns>
        public bool DownloadFaxFile(string strSrc, string LocalPath)
        {
            if (string.IsNullOrWhiteSpace(strSrc))
            {
                throw new ArgumentNullException("strRet");
            }

            if (m_Connection.GetLoginInfo(("STATUS")) != ("CONNECTED"))
            {
                throw new Exception("WARNNING: Please login first.");
            }
            string m_strFaxFileID = m_Connection.GetValue(strSrc, "FILE");
            String strRet;
            strRet = m_Connection.StartDownFax(m_strFaxFileID, LocalPath, 0, 0, 0);
            if (strRet == ("DONE"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion

        #region 转换tif

        /// <summary>
        /// 转换Tif文件
        /// </summary>
        /// <param name="FileFromPath">待转换文件 C:\MyFaxFile.doc</param>
        /// <param name="FileToPath">转换后存储路径 C:\MyFaxFile.tif</param>
        public bool ConvertToTif(string FileFromPath, string FileToPath)
        {
            string strRet = m_Connection.ConvertToTiff(FileFromPath, FileToPath);
            if (strRet == "SUCCESS")
            {
                return true;
            }
            return false;
        }

        #endregion

        #region 获取服务器信息

        /// <summary>
        /// 获取服务器信息:返回字串值,格式如下:"Server SN: " 服务器序列号;"Hardware Version: "硬件版本;"BIOS Version: " BIOS 版本;"Fax Firmware Version: " 传真固件版本;"Software Version: " 传真软件版本;"Software Build Time: " 传真软件发布时间;"Total Storage Size (MB): " 总存储容量,单位:MB;"Free Storage Size (MB): " 空闲存储容量,单位:MB;"Line Status: " 传真线路状态;"Caller ID Type: " 线路的来电显示制式(需有电话呼入才能识别);"Authorized Users: " 授权用户数量;"Trial Expired: " 试用期信息。
        /// </summary>
        /// <returns></returns>
        public string GetServerInfo()
        {
            if (m_Connection.GetLoginInfo(("STATUS")) != ("CONNECTED"))
            {
                throw new Exception("WARNNING: Please login first.");
            }
            string strRet = m_Connection.GetServerInformation();
            return strRet;
        }

        /// <summary>
        /// 获取服务器序列号
        /// </summary>
        /// <returns></returns>
        public string GetServerId()
        {
            if (m_Connection.GetLoginInfo(("STATUS")) != ("CONNECTED"))
            {
                throw new Exception("WARNNING: Please login first.");
            }
            string strRet = m_Connection.GetServerID();
            return strRet;
        }

        /// <summary>
        /// 获取登录信息
        /// </summary>
        /// <param name="strValName">要获取的参数名:"ADDRESS" 返回服务器地址"USER" 返回用户名"PASSWORD" 返回密码"MD5PSW" 返回 MD5 编码后的密码"STATUS" 返回 COM 实例与服务器的连接状态。</param>
        /// <returns></returns>
        public string GetLoginInfo(string strValName)
        {
            if (strValName != "STATUS")
            {
                if (m_Connection.GetLoginInfo(("STATUS")) != ("CONNECTED"))
                {
                    throw new Exception("WARNNING: Please login first.");
                }
            }
            return m_Connection.GetLoginInfo(strValName);
        }

        /// <summary>
        /// 获取字符串中的值
        /// </summary>
        /// <param name="strSrc"></param>
        /// <param name="strFind"></param>
        /// <returns></returns>
        public string GetValue(string strSrc, string strFind)
        {
            return m_Connection.GetValue(strSrc, strFind);
        }

        public string GetReceiver(string strSrc)
        {
            return m_Connection.GetValue(strSrc, "RECEIVER");
        }

        public string GetSender(string strSrc)
        {
            return m_Connection.GetValue(strSrc, "SENDER");
        }

        public string GetTask(string strSrc)
        {
            return m_Connection.GetValue(strSrc, "TASK");
        }

        #endregion

        #endregion
    }
           

继续阅读