Send MMS Message with VB .NET

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

For a C# .NET version of this example, please see the related article Send MMS Message with C# .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 VB.NET code example uses code from that library to send an MMS message:

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

Imports System.Text

Imports System.IO

Imports System.Net

Imports System.Web

Imports System.Web.Services

Imports ClassLibrary1

Imports ClassLibrary1.Krystalware

Imports ClassLibrary1.Krystalware.UploadHelper

Imports ClassLibrary1.Krystalware.UploadHelper.HttpUploadHelper

Imports ClassLibrary1.Krystalware.UploadHelper.MimePart

Imports ClassLibrary1.Krystalware.UploadHelper.StreamMimePart

Imports ClassLibrary1.Krystalware.UploadHelper.StringMimePart

Imports ClassLibrary1.Krystalware.UploadHelper.UploadFile

Imports System.Collections.Specialized



Public Class Form1

    Public Sub SendMMS()

        ' Set the following variables as appropriate for your system. 

        ' URL that points to NowSMS web interface 

        Dim url As New Uri(" http://127.0.0.1:8800/ ")



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

        Dim username As [String] = "testuser"

        Dim password As [String] = "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 



        Dim files As UploadFile() = New UploadFile() {

         New UploadFile("c:\mms\tts.mp3", "MMSFILE", "audio/mpeg"),

         New UploadFile("c:\mms\image.png", "MMSFILE", "image/png")}



        ' Create additional form parameters for sending MMS 

        Dim form As New NameValueCollection()



        ' "PhoneNumber" variable contains recipient phone number 

        form("PhoneNumber") = "09059999999"

        ' "MMSFROM" variable contains sender phone number or address

        '  (Note: Ignored when sending via modem) 

        form("MMSFROM") = "09058888888"

        ' "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 

        Dim req As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)



        Dim credCache = New CredentialCache()

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

        req.Credentials = credCache



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



        ' Read the HTTP response and echo to console 

        Using s As Stream = resp.GetResponseStream()

            Using sr As New StreamReader(s)

                Dim response As String = sr.ReadToEnd()

                MsgBox(response)

                System.Console.WriteLine(response)

            End Using

        End Using

    End Sub



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        SendMMS()

    End Sub

End Class

 

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.

System Requirements for using this script:

This script connects to a NowSMS server and posts a request to the NowSMS server to send a message.

If you do not have a NowSMS server installed, this script will not work.

NowSMS server software is installed on a Windows PC and to be able to send SMS or MMS messages, NowSMS also requires a GSM modem, Android phone, or SMS service provider connection.

Trial versions of the NowSMS software can be downloaded at https://nowsms.com/download-free-trial

Either the Now SMS/MMS Gateway or NowSMS Lite can be used. NowSMS Lite can send SMS and MMS messages using a single GSM modem or Android phone as a modem connection. The full product supports multiple modems and/or service provider connections.

Before attempting to interface with NowSMS using scripts, you should verify that NowSMS is configured correctly and can send SMS messages using its built in web interface. For Android phone or GSM modem connections, there are quick start guides that can be found here: https://nowsms.com/doc/quick-start-guide

Once you have verified that NowSMS is working on its own, it will be necessary to change IP addresses referenced in the script to point to your NowSMS server. Our sample scripts use an IP address of 127.0.0.1, which is a special loopback address for the current PC. If your script is running on a different system than the NowSMS server, change 127.0.0.1 to a host name or IP address that is valid for your environment. It is also necessary to define an SMS user account on the NowSMS server with account credentials that match with the user account in the script.