mm1介面也就是手機終端和mmsc(中國移動就是http://mmsc.monternet.com)用來發送mms message的介面, GPRS modem當然也是可以使用這一介面發送的.
using System;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.Text;
namespace MMSLib
{
/// <summary>
/// MMSender 的摘要說明。
///
/// </summary>
public class MMSender
{
// 設定參數
string sMmscUrl="http://mmsc.monternet.com";
string sProxyUrl="10.0.0.172:80";
public MMSender()
{
//
// TODO: 在此處添加建構函式邏輯
//
}
public void SetMMSC(string szUrl)
{
sMmscUrl =szUrl;
}
public void SetProxy(string szUrl)
{
sProxyUrl = szUrl;
}
/* 發送MMS的過程
1> 建立訊息發送介面
MMSender ms = new MMSender();
2> 設定參數屬性
預設屬性已經是中國移動參數,因此如果是中國移動使用者,以下兩個操作可以不需要
ms.SetMMSC("http://mmsc.monternet.com");
ms.SetProxy("10.0.0.172:80");
3> 建立訊息
MMessage mm= new MMessage();
4> 設定訊息內容
mm.SetSubject("標題"); // 設定標題
mm.AddTo("13825271511"); // 添加接收號碼,調用一次添加一個接收號碼
mm.AddFile("FileName"); // 添加傳送檔案,包含檔案路徑,調用一次添加一個傳送檔案
5> 發送訊息
string szReult =ms.Send(mm);
6> 繼續發送其他號碼
mm.ClearTo();
mm.AddTo("13812345678");
ms.Send(mm);
*/
/* 避免協議衝突的設定
<configuration>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true"/>
</settings>
</system.net>
</configuration>
*/
public string Send(MMessage mm)
{
try
{
// 驗證參數有效性
WebRequest wReq = WebRequest.Create(sMmscUrl);
HttpWebRequest hReq = (HttpWebRequest)wReq;
wReq.Headers.Clear();
if( sProxyUrl.Length >0)
wReq.Proxy = new WebProxy(sProxyUrl);
wReq.ContentType ="application/vnd.wap.mms-message";
hReq.Accept ="application/vnd.wap.mms-message,text/plain,*/*";
wReq.Method ="POST";
hReq.KeepAlive = false;
hReq.UserAgent = "Nokia6681/2.0 (4.00.15) SymbianOS/8.0
Series60/2.6 Profile/MIDP-2.0 Configuration/CLDC-1.1";
// Write Post Dat
byte[] byMM = mm.GetContent();
hReq.ContentLength = byMM.Length;
Stream sReq = wReq.GetRequestStream();
sReq.Write(byMM,0,byMM.Length);
sReq.Close();
// Http Request
WebResponse wRes= wReq.GetResponse();
HttpWebResponse hRes = (HttpWebResponse)wRes;
if ( hRes.StatusCode == HttpStatusCode.OK)
{
Stream sRes = wRes.GetResponseStream();
StreamReader sr = new StreamReader(sRes);
string szResult =sr.ReadToEnd(); // 發送結果
// Parse result sring
return szResult;
}
}
catch(Exception e)
{
throw new Exception(e.Message);
}
return string.Empty;
}
}
}
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);
}
}