描述:應特殊需要,必須在很短時間內開發穩定的簡訊處理常式,採用了移動給的簡訊開發包,長簡訊發送一直沒有找到相關文檔解決,作為一個碼農只能找些非主流的方法解決問題了
api聲明
[DllImport("SMEIDLL.DLL",CharSet = CharSet.Unicode)] public static extern int CMPP3Submit( [MarshalAs(UnmanagedType.U1)] byte byPKTotal, [MarshalAs(UnmanagedType.U1)] byte byPKNumber, [MarshalAs(UnmanagedType.U1)] byte SRR, [MarshalAs(UnmanagedType.U1)] byte byMsglevel, [MarshalAs(UnmanagedType.LPStr)] StringBuilder ServiceSubType, [MarshalAs(UnmanagedType.U1)] byte byFeeUserType, [MarshalAs(UnmanagedType.LPStr)] StringBuilder sFeeAddr, [MarshalAs(UnmanagedType.U1)] byte byFeeTerminalType, [MarshalAs(UnmanagedType.U1)] byte PID, UInt32 Default_ID, [MarshalAs(UnmanagedType.U1)] byte UDHI, [MarshalAs(UnmanagedType.U1)] byte DCS, [MarshalAs(UnmanagedType.LPStr)] StringBuilder sSPID, [MarshalAs(UnmanagedType.LPStr)] StringBuilder sFeeType, [MarshalAs(UnmanagedType.LPStr)] StringBuilder sFeeCode, [MarshalAs(UnmanagedType.LPStr)] StringBuilder Schedule, [MarshalAs(UnmanagedType.LPStr)] StringBuilder Expire, [MarshalAs(UnmanagedType.LPStr)] StringBuilder OrgAddr, [MarshalAs(UnmanagedType.U1)] byte byUserNum, [MarshalAs(UnmanagedType.LPStr)] StringBuilder sDestAddrs, [MarshalAs(UnmanagedType.U1)] byte byDestTerminalType, UInt32 UDLen, String UserData, [MarshalAs(UnmanagedType.LPStr)] StringBuilder sLinkID, [MarshalAs(UnmanagedType.LPStr)] StringBuilder byMsgID);
長簡訊發送調用 ,仔細分析下吧
UDHI = 1; int HeadLenght = 7; //包頭長度 string MtMsg = _message.StrSMSContent; byte[] messageUCS2 = Encoding.BigEndianUnicode.GetBytes(MtMsg); int messageUCS2Len = messageUCS2.Length; int maxMessageLen = 139; if (messageUCS2Len > maxMessageLen) { int messageUCS2Count = messageUCS2Len / (maxMessageLen - HeadLenght) + 1; //長簡訊分為多少條發送 byPKTotal = (byte)messageUCS2Count; byte[] tp_udhiHead = new byte[HeadLenght]; tp_udhiHead[0] = 0x06; tp_udhiHead[1] = 0x08; tp_udhiHead[2] = 0x04; tp_udhiHead[3] = 0x00; tp_udhiHead[4] = 0x00; tp_udhiHead[5] = (byte)messageUCS2Count; tp_udhiHead[6] = 0x01; //預設為第一條 for (int i = 0; i< messageUCS2Count; i++) { byPKNumber = (byte)(i + 1); //同一條簡訊的第幾條 tp_udhiHead[6]=(byte)(i + 1); byte[] msgContent; if (i != messageUCS2Count - 1) { //不為最後一條 msgContent = BIConvert.byteAdd(tp_udhiHead,messageUCS2, i * (maxMessageLen - HeadLenght), (i + 1) * (maxMessageLen - HeadLenght)); UDLen = (uint)msgContent.Length; } else { msgContent = BIConvert.byteAdd(tp_udhiHead, messageUCS2, i * (maxMessageLen - HeadLenght), messageUCS2Len); msgContent = BIConvert.byteAdd(msgContent, new byte[] { 0 }, 0, 1); UDLen = (uint)msgContent.Length - 1; } string str_msgContent = Encoding.Unicode.GetString(msgContent); int isSub = SMEI.CMPP3Submit(byPKTotal, byPKNumber, SRR, byMsglevel, ServiceSubType, byFeeUserType, sFeeAddr, byFeeTerminalType, PID, Default_ID, UDHI, DCS, sSPID, sFeeType, sFeeCode, Schedule, Expire, OrgAddr, byUserNum, sDestAddrs, byDestTerminalType, UDLen, str_msgContent, sLinkID, byMsgID);}}
不是最好的方法,但是解決問題,超長簡訊也無壓力。