Enable the MM1 interface to send MMS (that is, GPRS Modem sends MMS) code C #

Source: Internet
Author: User
MM1 interface is the mobile terminal and mmsc (China Mobile is the http://mmsc.monternet.com) used to send MMS Message interface, GPRS modem of course can also use this interface to send.

Using system;
Using system. net;
Using system. IO;
Using system. diagnostics;
Using system. Threading;
Using system. collections;
Using system. text;

Namespace mmslib
{
/// <Summary>
/// Summary of mmsender.
///
/// </Summary>
Public class mmsender
{
// Set parameters
String smmscurl = "http://mmsc.monternet.com ";
String sproxyurl = "10.0.0.172: 80 ";

Public mmsender ()
{
//
// Todo: add the constructor logic here
//
}
Public void setmmsc (string szurl)
{
Smmscurl = szurl;
}
Public void setproxy (string szurl)
{
Sproxyurl = szurl;
}


/* Process of sending MMS
1> Create a message sending Interface
Mmsender MS = new mmsender ();
2> set parameter attributes
The default attribute is already the China Mobile parameter. Therefore, if you are a China Mobile user, the following two operations are not required:
Ms. setmmsc ("http://mmsc.monternet.com ");
Ms. setproxy ("10.0.0.172: 80 ");
3> Create a message
Mmessage Mm = new mmessage ();
4> set the message content
Mm. setsubject ("title"); // set the title
Mm. addto ("13825271511"); // Add a receiving number. Call this operation to add a receiving number at a time.
Mm. AddFile ("FILENAME"); // Add the sending file, which contains the file path. Call this operation to add a sending file at a time.
5> send messages
String szreult = Ms. Send (mm );

6> continue sending other numbers
Mm. clearto ();
Mm. addto( "13812345678 ");
Ms. Send (mm );
*/


/* Setting to avoid protocol conflicts
<Configuration>
<System.net>
<Settings>
<Httpwebrequest useunsafeheaderparsing = "true"/>
</Settings>
</System.net>
</Configuration>
*/
Public String send (mmessage mm)
{
Try
{
// Verify the parameter Validity
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 (); // send the result
// Parse result sring
Return szresult;
}
}
Catch (exception E)
{
Throw new exception (E. Message );
}
Return string. empty;
}
}
}

Public class mmessage
{
String subject = "";
Int nseconds = 0; // set the delivery time, current relative time, in seconds
Arraylist lfile = new arraylist (); // a list of MMS files
Arraylist ldest = new arraylist (); // a set of sending numbers

Static long nseq = 0;

Public mmessage ()
{
//
// Todo: add the constructor logic here
//
}

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 ();
}

// Obtain binary encoded bytes
Public byte [] getcontent ()
{

Byte [] bymms = new byte [0];
// Start the Message Header
// 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 ++; // Add 1 to the serial number

Bymms = appendoct (New byte [] {0x0}, bymms );

// X-MMS-version
Bymms = appendoct (New byte [] {0x8d, 0x90}, bymms );
// Date

// From, set to insert-address-token
Bymms = appendoct (New byte [] {0x89,0x01,0x81}, bymms );

//
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) // use utf8 Encoding
{
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 is 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, 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 is used by default. You need to change it to little-Endian.
// Change bftime to little-Endian
Array. Reverse (bftime );
Byte [] bftimelen = new byte [3];
Bftimelen [0] = (byte) (bftime. Length + 2 );
Bftimelen [1] = 0x81; // relative time format
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 );
// Start message body (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: Add media files one by one
For (Int J = 0; j <lfile. Count; j ++)
{
Bymms = appendoct (getmmscontent (lfile [J]. tostring (), bymms );
}
Return bymms;
}

// Tools
// Add a media file to the MMS content
Private byte [] getmmscontent (string filename)
{
// Each multipart entry consists of five Components
/* Headerslen
* Datalen
* Contenttype
* Headers
* Data
**/
Byte [] byheaders = new byte [0]; // combination of contenttype and 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); // The text must end with 0x00
}

// Add content-ID and content-location
Byheaders = appendoct (New byte [] {0xc0, 0x22, 0x3c}, byheaders );
Byheaders = appendoct (encoding. ASCII. getbytes (fileid), byheaders );
Byheaders = appendoct (New byte [] {0x3e, 0x00}, byheaders );
// Add 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;

}
// Read bytes from the file
Private byte [] readfromfile (string filename)
{
Byte [] BF = new byte [0];
Filestream FS = NULL;
Try
{
FS = new filestream (filename, filemode. Open, fileaccess. readwrite, fileshare. None); // No buffsize is set
}
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;
}
// Get the file name (excluding the folder)
Private string getcontentid (string filename)
{
Int at = filename. lastindexof ("\\");
If (at <0)
Return filename;
Else
Return filename. substring (at + 1 );
}
// Add Bytes
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;
}
// Add a string
Private byte [] appendoct (string SZ, byte [] bydest)
{
Return appendoct (encoding. Default. getbytes (sz), bydest );
}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.