Send MMS via GPRS modem-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-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 parts
/* 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 byte
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 );
}
}

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.