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

📄 mmessage.cs

📁 MMS(彩信)发送的一个实例
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;

namespace mmsxx
{
    public class MMessage
    {
        string Subject = "";
        int nSeconds = 0;   //   设置送达的时间,当前相对时间,以秒为单位   
        ArrayList lFile = new ArrayList();       //   彩信文件列表   
        ArrayList lDest = new ArrayList();       //   发送号码集合   

        static long nSeq = 0;

        public MMessage()
        {
            //   
            //   TODO:   在此处添加构造函数逻辑   
            //   
        }

        public void SetSubject(string szSubject)
        {
            Subject = szSubject;
        }
        public void SetDeliverTime(int nSec)
        {
            nSeconds = nSec;
        }
        //     
        public void AddTo(string Dest)
        {
            lDest.Add(Dest);
        }

        public void AddFile(string File)
        {
            lFile.Add(File);
        }

        public void ClearTo()
        {
            lDest.Clear();
        }

        //   得到二进制编码字节   
        public byte[] GetContent()
        {

            byte[] byMms = new byte[0];
            //   消息头开始   
            //X-Mms-Message-Type   
            byMms = AppendOct(new byte[] { 0x8C, 0x80 }, byMms);
            //X-Mms-Transaction-ID   
            byMms = AppendOct(new byte[] { 0x98 }, byMms);
            byMms = AppendOct(nSeq.ToString(), byMms);
            nSeq++;   //   序列号加1   

            byMms = AppendOct(new byte[] { 0x0 }, byMms);

            //X-Mms-MMS-Version   
            byMms = AppendOct(new byte[] { 0x8D, 0x90 }, byMms);
            //Date   

            //From,设置为   Insert-address-token   
            byMms = AppendOct(new byte[] { 0x89, 0x01, 0x81 }, byMms);

            //To   
            for (int i = 0; i < lDest.Count; i++)
            {
                byMms = AppendOct(new byte[] { 0x97 }, byMms);
                byMms = AppendOct("+86" + (string)lDest[i] + "/TYPE=PLMN", byMms);
                byMms = AppendOct(new byte[] { 0x0 }, byMms);
            }
            //Subject   
            if (Subject.Length > 0)     //   使用Utf8编码   
            {
                byMms = AppendOct(new byte[] { 0x96 }, byMms);
                //   Value-length   Char-set   Text   -string   
                byte[] byLen = new byte[1];
                byLen[0] = (byte)(Encoding.UTF8.GetByteCount(Subject) + 2);
                byMms = AppendOct(byLen, byMms);
                //   Char-set   为utf-8   
                byMms = AppendOct(new byte[] { 0xEA }, byMms);
                byMms = AppendOct(Encoding.UTF8.GetBytes(Subject), byMms);
                byMms = AppendOct(new byte[] { 0x0 }, byMms);
            }
            //   X-Mms-Delivery-Time,递送时间   =   Relative-token   Delta-seconds-value   
            //   Relative-token   =   0x81   
            //   Delta-seconds-value   =Long-integer   
            //   Long-integer   =   Short-length   Multi-octet-integer   
            if (nSeconds > 0)
            {
                byMms = AppendOct(new byte[] { 0x87 }, byMms);
                byte[] bfTime = BitConverter.GetBytes(nSeconds);     //   默认使用Big-endian,需用改为Little-endian   
                //   bfTime改为Little-endian   
                Array.Reverse(bfTime);
                byte[] bfTimeLen = new byte[3];
                bfTimeLen[0] = (byte)(bfTime.Length + 2);
                bfTimeLen[1] = 0x81;     //   相对时间格式   
                bfTimeLen[2] = (byte)bfTime.Length;
                byMms = AppendOct(bfTimeLen, byMms);
                byMms = AppendOct(bfTime, byMms);
            }

            //Content-Type:application/vnd.wap.multipart.mixed   
            byMms = AppendOct(new byte[] { 0x84, 0xA3 }, byMms);
            //   消息体开始(MIME   multipart)   
            //   8.5.2   Multipart   Header   
            //   nEntries   Uintvar   The   number   of   entries   in   the   multipart   entity   
            byte[] byFileCount = new byte[1];
            byFileCount[0] = (byte)lFile.Count;
            byMms = AppendOct(byFileCount, byMms);
            //   8.5.3   Multipart   Entry,逐个加入媒体文件   
            for (int j = 0; j < lFile.Count; j++)
            {
                byMms = AppendOct(GetMmsContent(lFile[j].ToString()), byMms);
            }
            return byMms;
        }

        //   Tools   
        //   加入媒体文件到彩信内容中去   
        private byte[] GetMmsContent(string FileName)
        {
            //   每一个Multipart   Entry由5个部分组成   
            /*   HeadersLen   
              *   DataLen   
              *   ContentType   
              *   Headers   
              *   Data   
              *   */
            byte[] byHeaders = new byte[0];       //   ContentType和Headers组合   
            byte[] byData = ReadFromFile(FileName);

            string FileID = getContentId(FileName);
            //   Set   content-type   
            if (FileName.EndsWith(".txt"))
            {
                byHeaders = new byte[1];
                byHeaders[0] = (byte)(Encoding.ASCII.GetByteCount(FileID) + 5);
                byHeaders = AppendOct(new byte[] { 0x83, 0x85 }, byHeaders);       //   Utf-8   
                byHeaders = AppendOct(Encoding.ASCII.GetBytes(FileID), byHeaders);
                byHeaders = AppendOct(new byte[] { 0x00 }, byHeaders);
                byHeaders = AppendOct(new byte[] { 0x81, 0xEA }, byHeaders);
            }
            else if (FileName.EndsWith(".gif"))
            {
                byHeaders = new byte[] { 0x9D };
            }
            else if (FileName.EndsWith(".mid") || FileName.EndsWith(".midi"))
            {
                byHeaders = Encoding.ASCII.GetBytes("audio/midi");
                byHeaders = AppendOct(new byte[] { 0x00 }, byHeaders);     //   文本需要以0x00结尾   
            }

            //   加入Content-ID和Content-Location   
            byHeaders = AppendOct(new byte[] { 0xC0, 0x22, 0x3C }, byHeaders);
            byHeaders = AppendOct(Encoding.ASCII.GetBytes(FileID), byHeaders);
            byHeaders = AppendOct(new byte[] { 0x3E, 0x00 }, byHeaders);
            //加入Content-Location   
            byHeaders = AppendOct(new byte[] { 0x8E }, byHeaders);
            byHeaders = AppendOct(Encoding.ASCII.GetBytes(FileID), byHeaders);
            byHeaders = AppendOct(new byte[] { 0x00 }, byHeaders);

            byte[] byHeaderLen = encodeUintvar(byHeaders.Length);
            byte[] byDataLen = encodeUintvar(byData.Length);

            byte[] byMmc = new byte[byHeaderLen.Length + byDataLen.Length + byHeaders.Length + byData.Length];
            Array.Copy(byHeaderLen, byMmc, byHeaderLen.Length);
            Array.Copy(byDataLen, 0, byMmc, byHeaderLen.Length, byDataLen.Length);
            Array.Copy(byHeaders, 0, byMmc, byHeaderLen.Length + byDataLen.Length, byHeaders.Length);
            Array.Copy(byData, 0, byMmc, byHeaderLen.Length + byDataLen.Length + byHeaders.Length, byData.Length);

            return byMmc;
        }

        private byte[] encodeUintvar(int n)
        {
            byte[] buf = new byte[8];
            int l = 0;
            while (n >= 128)
            {
                byte b = (byte)(n & 0x7F);
                n = n >> 7;
                buf[l++] = b;
            }
            buf[l++] = (byte)n;

            byte[] retBys = new byte[l];
            for (int i = 0; i < l; ++i)
            {
                retBys[i] = (byte)(buf[l - i - 1] | 0x80);
            }
            retBys[l - 1] &= 0x7F;
            return retBys;

        }
        //   从文件中读取字节   
        private byte[] ReadFromFile(string FileName)
        {
            byte[] bf = new byte[0];
            FileStream fs = null;
            try
            {
                fs = new FileStream(FileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);     //   没有设定Buffsize   
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            if (fs != null)
            {
                bf = new byte[fs.Length];
                fs.Read(bf, 0, (int)fs.Length);
                fs.Close();
            }
            return bf;
        }
        //   得到文件名(不包含文件夹部分)   
        private string getContentId(string FileName)
        {
            int at = FileName.LastIndexOf("\\");
            if (at < 0)
                return FileName;
            else
                return FileName.Substring(at + 1);
        }
        //   增加字节   
        private byte[] AppendOct(byte[] bys, byte[] byDest)
        {
            byte[] bysNew = new byte[byDest.Length + bys.Length];
            try
            {
                Array.Copy(byDest, bysNew, byDest.Length);
                Array.Copy(bys, 0, bysNew, byDest.Length, bys.Length);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return bysNew;
        }
        //   增加字符串   
        private byte[] AppendOct(string sz, byte[] byDest)
        {
            return AppendOct(Encoding.Default.GetBytes(sz), byDest);
        }

    }
}

⌨️ 快捷键说明

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