Send MMS from C# .NET

Posted by on Feb 9, 2012 in Support Blog

Topic Keywords: , , ,

A .NET C# example for sending MMS messages via NowSMS has been posted at the following link: https://nowsms.com/download/sendmms.c#.txt

For a VB.NET version of this example, please see the related article Send MMS from VB.NET.

MMS examples for other environments (PHP, Java, command line) can be found at https://nowsms.com/sending-mms-messages-with-nowsms.

As discussed in Sending MMS Messages with NowSMS, one of the easiest ways to interface with NowSMS for sending MMS messages is to use what we refer to as the Proprietary URL Submission interface.  This interface is the one used for sending messages via the forms in the built-in NowSMS web interface.  Sending an MMS message via this interface is a matter of using an HTTP POST to submit MMS content in the form using the MIME type “multipart/form-data”.  This technique is often referred to as “HTTP File Upload” and is associated with web pages that have a “Browse” button that allows files to be uploaded.

There’s a great C#.NET HTTP File Upload library at: http://aspnetupload.com/Upload-File-POST-HttpWebRequest-WebClient-RFC-1867.aspx

A direct link to download the library is here (only the UploadHelper component is needed): http://aspnetupload.com/AspNetUploadSamples.zip

In the event that this link does not work in the future, we have archived a copy of the library at the following link:

 

application/x-zip-compressedAspNetUploadSamples.zip
AspNetUploadSamples.zip(388.7 k)

 

The following code example uses code from that library to send an MMS message:

(Note: This example can be downloaded at https://nowsms.com/download/sendmms.c#.txt.)

// Set the following variables as appropriate for your system. 



// URL that points to NowSMS web interface 

Uri url = new Uri("http://127.0.0.1:8800/"); 



// Valid username/password for account defined in NowSMS under "SMS Users". 

String username = "testuser"; 

String password = "testpass"; 



// Images & Files to be included in the message. 

// First parameter is filename and path. 

// Second parameter must be MMSFILE 

// Third parameter should be MIME type 



UploadFile[] files = new UploadFile[] 

{ 

new UploadFile("f:\\temp\\image1.png", "MMSFILE", "image/png"), 

new UploadFile("f:\\temp\\image2.jpg", "MMSFILE", "image/jpeg"), 

}; 



// Create additional form parameters for sending MMS 

NameValueCollection form = new NameValueCollection(); 



// "PhoneNumber" variable contains recipient phone number 

form["PhoneNumber"] = "9999999999"; 

// "MMSFROM" variable contains sender phone number or address

//  (Note: Ignored when sending via modem) 

form["MMSFROM"] = "9999999999"; 

// "MMSSUBJECT" variable contains subject line for message 

form["MMSSUBJECT"] = "test subject"; 

// "MMSTEXT" variable contains test to appear in message (optional) 

form["MMSTEXT"] = "This is a test message"; 



// Send the request to NowSMS, creating an HttpWebRequest and 

// then use HttpUploadHelper to send files in multipart/form-data format 

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); 



var credCache = new CredentialCache(); 

credCache.Add(url, "Basic", new NetworkCredential(username, password)); 

req.Credentials = credCache; 



HttpWebResponse resp = HttpUploadHelper.Upload(req, files, form); 



// Read the HTTP response and echo to console 

using (Stream s = resp.GetResponseStream()) 

using (StreamReader sr = new StreamReader(s)) 

{ 

string response = sr.ReadToEnd(); 

System.Console.WriteLine(response); 

}

 

Additional MMS options can be included using the form[“variablename”] = “value” syntax shown in the above example. The other options/variables are described in more detail in https://nowsms.com/sending-mms-messages-with-nowsms.

For comments and further discussion, please click here to visit the NowSMS Technical Forums (Discussion Board)...