You are here:
ActiveXperts.com > SMS and MMS Toolkit > How to Use MMS Toolkit > SMTP (MM4) > Visual C++ 5.x/6.x
Quicklinks
The SMS and MMS Toolkit is a software development kit (SDK) to enhance an application or script with SMS, MMS and Pager functionality. SMS messages can be sent using a GSM/GPRS modem, an SMPP provider, an HTTP compliant SMS provider or using a standard dialup or fixed-line SMS modem. MMS messages can be sent via a GSM/GPRS modem (MM1), an SMTP server (MM4) or an XML/SOAP compliant provider (MM7).
SMS features:
MMS features:
Pager features:
This document describes how the SMS and MMS Toolkit can be integrated into Visual C++ projects.
Download the SMS and MMS Toolkit from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
Launch 'Microsoft Visual C++' from the Start menu, and choose 'New' from the 'File Menu'. The 'New' dialog appears.
Select the type of project (for instance: 'Win32 Console Application'), enter a 'Project name' and select the 'Location':
(Click on the picture to enlarge)
Select the kind of project, for instance a 'Hello, world!' application and click 'Finish':
(Click on the picture to enlarge)
A new Project is created now.
Before you can use the SMS and MMS Toolkit, you need to refer to the SMS and MMS Toolkit library. The actually reference files are shipped with the product and are located in the following directory:
C:\Program Files\ActiveXperts\xmstoolkit\Examples\Visual C++\Include
Copy all files in the above directory ('AXmsCtrl.h', 'AXmsCtrl_i.c' and 'AXmsConstants.h') to your project directory.
On top of your code, declare the following objects:
IMmsProtocolMm4 *m_pConnection = NULL; IMmsMessage *m_pMessage = NULL; IMmsSlide *m_pSlide = NULL;
Since the SMS and MMS Toolkit is a COM object, you must initialize the COM library before they can call COM library functions (e.g. SMS and MMS Toolkit functions):
CoInitialize(NULL);
Create the GSM objects in the following way:
CoCreateInstance ( CLSID_MmsProtocolMm4, NULL, CLSCTX_INPROC_SERVER, IID_IMmsProtocolMm4, (void**) &m_pConnection ); CoCreateInstance ( CLSID_MmsMessage, NULL, CLSCTX_INPROC_SERVER, IID_IMmsMessage, (void**) &m_pMessage ); CoCreateInstance ( CLSID_MmsSlide, NULL, CLSCTX_INPROC_SERVER, IID_IMmsSlide, (void**) &m_pSlide );
You can now send MMS messages using a MM4 (SMTP) MMSC provider.
The following code shows how to send a MMS using C++ :
#include "stdafx.h" #include "Demo.h" #include "DemoDlg.h" #include///////////////////////////////////////////////////////////////////////////// #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// #include "..\include\AXmsCtrl_i.c" #include "HyperLink.h" ///////////////////////////////////////////////////////////////////////////// #include "DialogLicensing.h" #include "DialogRegister.h" #include "DialogText.h" ///////////////////////////////////////////////////////////////////////////// CDemoDlg::CDemoDlg(CWnd* pParent /*=NULL*/) : CDialog(CDemoDlg::IDD, pParent) { //{{AFX_DATA_INIT(CDemoDlg) m_strHost = _T(""); m_strLogfile = _T(""); m_strResult = _T("N/A"); m_strSubject = _T("MMS Message"); m_strTo = _T("+31647134225"); m_strResponse = _T("N/A"); m_strFrom = _T(""); m_strPassword = _T(""); m_strAccount = _T(""); m_bAuthentication = FALSE; m_lPort = 25; //}}AFX_DATA_INIT m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_pConnection = NULL; m_pMessage = NULL; HRESULT hr = S_OK; CoInitialize ( NULL ); hr = CoCreateInstance(CLSID_MmsProtocolMm4, NULL, CLSCTX_INPROC_SERVER, IID_IMmsProtocolMm4, (void**) &m_pConnection ); if( ! SUCCEEDED( hr ) ) { m_pConnection = NULL; return; } hr = CoCreateInstance(CLSID_MmsMessage, NULL, CLSCTX_INPROC_SERVER, IID_IMmsMessage, (void**) &m_pMessage ); if( ! SUCCEEDED( hr ) ) { m_pMessage = NULL; return; } hr = CoCreateInstance(CLSID_MmsSlide, NULL, CLSCTX_INPROC_SERVER, IID_IMmsSlide, (void**) &m_pSlide ); if( ! SUCCEEDED( hr ) ) { m_pSlide = NULL; return; } } ///////////////////////////////////////////////////////////////////////////// void CDemoDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDemoDlg) DDX_Control(pDX, IDC_TREE, m_Tree); DDX_Text(pDX, IDC_EDIT_LOGFILE, m_strLogfile); DDX_Text(pDX, IDC_EDIT_RESULT, m_strResult); DDX_Text(pDX, IDC_EDIT_SUBJECT, m_strSubject); DDX_Text(pDX, IDC_EDIT_TO, m_strTo); DDX_Text(pDX, IDC_STATIC_RESPONSE, m_strResponse); DDX_Text(pDX, IDC_EDIT_FROM, m_strFrom); DDX_Text(pDX, IDC_EDIT_PASSWORD, m_strPassword); DDX_Text(pDX, IDC_EDIT_ACCOUNT, m_strAccount); DDX_Text(pDX, IDC_EDIT_HOST, m_strHost); DDX_Text(pDX, IDC_EDIT_PORT, m_lPort); DDX_Check(pDX, IDC_CHECK_AUTH, m_bAuthentication); //}}AFX_DATA_MAP } ///////////////////////////////////////////////////////////////////////////// BEGIN_MESSAGE_MAP(CDemoDlg, CDialog) //{{AFX_MSG_MAP(CDemoDlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON_VIEW, OnButtonView) ON_BN_CLICKED(IDC_BUTTON_BROWSE, OnButtonBrowse) ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend) ON_BN_CLICKED(IDC_BUTTON_LOADCFG, OnButtonLoadcfg) ON_BN_CLICKED(IDC_BUTTON_SAVECFG, OnButtonSavecfg) ON_BN_CLICKED(IDC_BUTTON_LICENSING, OnButtonLicensing) ON_BN_CLICKED(IDC_BUTTON_SLIDE, OnButtonSlide) ON_NOTIFY(TVN_SELCHANGED, IDC_TREE, OnSelchangedTree) ON_BN_CLICKED(IDC_BUTTON_TEXT, OnButtonText) ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete) ON_BN_CLICKED(IDC_CHECK_AUTH, OnCheckAuth) ON_WM_CLOSE() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// BOOL CDemoDlg::OnInitDialog() { CDialog::OnInitDialog(); LONG lDeviceCount = 0L; BSTR bstrDevice = NULL; TCHAR szTempPath [ MAX_PATH + 1 ]; SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon GetTempPath( MAX_PATH - 14, szTempPath ); if( szTempPath[ 0 ] == '\0' || szTempPath[ lstrlen( szTempPath ) - 1 ] != '\\' ) _tcscat( szTempPath, _T("\\") ); m_strLogfile = szTempPath; m_strLogfile += "MmsLog.txt"; m_ImageList.Create ( 16, 16, ILC_MASK , 4, 4 ); m_ImageList.Add ( AfxGetApp ()->LoadIcon ( IDI_ICON_IMAGE ) ); m_ImageList.Add ( AfxGetApp ()->LoadIcon ( IDI_ICON_TEXT ) ); m_ImageList.Add ( AfxGetApp ()->LoadIcon ( IDI_ICON_SLIDE ) ); m_Tree.SetImageList ( &m_ImageList, LVSIL_NORMAL ); m_Tree.EnsureVisible ( m_Tree.InsertItem ( TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE, L"Slide", 2, 2, 0, 0, 0 ,NULL, NULL ) ); m_Tree.EnsureVisible ( m_Tree.InsertItem ( TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE, L"Hello World!!!", 1, 1, 0, 0, 1 ,m_Tree.GetFirstVisibleItem (), NULL ) ); LoadSettings (); UpdateData ( FALSE ); EnableControls (); return TRUE; } ///////////////////////////////////////////////////////////////////////////// void CDemoDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } ///////////////////////////////////////////////////////////////////////////// HCURSOR CDemoDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } ///////////////////////////////////////////////////////////////////////////// void CDemoDlg::OnButtonView() { UpdateData(); if (!m_strLogfile.GetLength()) return; if (ShellExecute(NULL,_T("open"),m_strLogfile,NULL,NULL,SW_SHOWDEFAULT ) == INVALID_HANDLE_VALUE) { AfxMessageBox (_T("Can't run viewer"),MB_OK); } } ///////////////////////////////////////////////////////////////////////////// void CDemoDlg::OnButtonLoadcfg() { BSTR bstrTemp = NULL; LONG lTemp = 0L; BOOL bTemp = FALSE; UpdateData (); CString strDirectory = ""; GetConfigDirectory ( strDirectory ); strDirectory += L"\\Providers\\Mms\\Mm4"; CFileDialog cfd ( TRUE, L"MM4", NULL, 0, L"MM4 Connection Files (*.MM4)|*.MM4||\0\0"); cfd.m_ofn.lpstrInitialDir = strDirectory; if ( cfd.DoModal () == IDOK ) { bstrTemp = m_strLogfile.AllocSysString (); m_pConnection->put_LogFile ( bstrTemp ); SysFreeString ( bstrTemp ); bstrTemp = cfd.GetPathName ().AllocSysString (); m_pConnection->ProviderProviderLoadConfig ( bstrTemp ); SysFreeString ( bstrTemp ); if ( GetResult () == 0L ) { m_pConnection->get_ProviderHost ( &bstrTemp ); m_strHost = bstrTemp; SysFreeString ( bstrTemp ); m_pConnection->get_ProviderPort ( &lTemp ); m_lPort = lTemp; m_pConnection->get_ProviderAccount ( &bstrTemp ); m_strAccount = bstrTemp; if ( SysStringLen ( bstrTemp ) ) { m_bAuthentication = TRUE; } SysFreeString ( bstrTemp ); m_pConnection->get_ProviderPassword ( &bstrTemp ); m_strPassword = bstrTemp; SysFreeString ( bstrTemp ); UpdateData ( FALSE ); EnableControls (); } } } ///////////////////////////////////////////////////////////////////////////// void CDemoDlg::OnButtonSavecfg() { UpdateData (); BSTR bstrTemp = NULL; CString strDirectory = ""; GetConfigDirectory ( strDirectory ); strDirectory += L"\\Providers\\Mms\\Mm4"; CFileDialog cfd ( FALSE, L"MM4", 0, NULL, L"MM4 Connection Files (*.MM4)|*.MM4||\0\0"); if ( cfd.DoModal () == IDOK ) { bstrTemp = m_strLogfile.AllocSysString (); m_pConnection->put_LogFile ( bstrTemp ); SysFreeString ( bstrTemp ); bstrTemp = m_strHost.AllocSysString (); m_pConnection->put_ProviderHost ( bstrTemp ); SysFreeString ( bstrTemp ); m_pConnection->put_ProviderPort ( m_lPort ); if ( m_bAuthentication ) { bstrTemp = m_strAccount.AllocSysString (); m_pConnection->put_ProviderAccount ( bstrTemp ); SysFreeString ( bstrTemp ); bstrTemp = m_strPassword.AllocSysString (); m_pConnection->put_ProviderPassword ( bstrTemp ); SysFreeString ( bstrTemp ); } bstrTemp = cfd.GetPathName ().AllocSysString (); m_pConnection->ProviderSaveConfig ( bstrTemp ); SysFreeString ( bstrTemp ); GetResult (); } } ///////////////////////////////////////////////////////////////////////////// void CDemoDlg::OnButtonSend() { UpdateData (); BSTR bstrTemp = NULL; m_pMessage->Clear (); bstrTemp = m_strTo.AllocSysString (); m_pMessage->AddRecipient ( bstrTemp ); SysFreeString ( bstrTemp ); bstrTemp = m_strFrom.AllocSysString (); m_pMessage->put_From ( bstrTemp ); SysFreeString ( bstrTemp ); bstrTemp = m_strSubject.AllocSysString (); m_pMessage->put_Subject ( bstrTemp ); SysFreeString ( bstrTemp ); HTREEITEM hItem = m_Tree.GetRootItem (); while (hItem != NULL) { HTREEITEM hItemItem = m_Tree.GetChildItem ( hItem ); m_pSlide->Clear (); while ( hItemItem ) { if ( m_Tree.GetItemData ( hItemItem ) == 0 ) { bstrTemp = m_Tree.GetItemText ( hItemItem ).AllocSysString (); m_pSlide->AddAttachment ( bstrTemp ); SysFreeString ( bstrTemp ); } else { bstrTemp = m_Tree.GetItemText ( hItemItem ).AllocSysString (); m_pSlide->AddText ( bstrTemp ); SysFreeString ( bstrTemp ); } hItemItem = m_Tree.GetNextItem (hItemItem, TVGN_NEXT ); } m_pMessage->AddSlide ( &_variant_t ( ( IDispatch*) m_pSlide ) ); hItem = m_Tree.GetNextSiblingItem(hItem); } // Set MMS Server Parameters bstrTemp = m_strHost.AllocSysString (); m_pConnection->put_ProviderHost ( bstrTemp ); SysFreeString ( bstrTemp ); m_pConnection->put_ProviderPort ( m_lPort ); if ( m_bAuthentication ) { bstrTemp = m_strAccount.AllocSysString (); m_pConnection->put_ProviderAccount ( bstrTemp ); SysFreeString ( bstrTemp ); bstrTemp = m_strPassword.AllocSysString (); m_pConnection->put_ProviderPassword ( bstrTemp ); SysFreeString ( bstrTemp ); } bstrTemp = m_strLogfile.AllocSysString (); m_pConnection->put_LogFile ( bstrTemp ); SysFreeString ( bstrTemp ); AfxGetApp()->DoWaitCursor ( 1 ); SaveSettings (); m_pConnection->Send ( &_variant_t ( ( IDispatch* ) m_pMessage ) ); GetResult (); AfxGetApp()->DoWaitCursor ( 0 ); } ///////////////////////////////////////////////////////////////////////////// LONG CDemoDlg::GetResult() { LONG lLastError = 0L; BSTR bstrResult = NULL; BSTR bstrResponse = NULL; m_pConnection->get_LastError ( &lLastError ); m_pConnection->GetErrorDescription ( lLastError, &bstrResult ); m_strResult.Format ( L"ERROR %ld (%ls)", lLastError, bstrResult ); SysFreeString ( bstrResult ); m_pConnection->get_ProviderResponse ( &bstrResponse ); m_strResponse = bstrResponse; SysFreeString (bstrResponse ); UpdateData ( FALSE ); return lLastError; } ///////////////////////////////////////////////////////////////////////////// VOID CDemoDlg::SaveSettings() { AfxGetApp ()->WriteProfileString ( _T ( "MM4" ), _T ("ProviderHost" ), m_strHost ); if ( m_bAuthentication ) { AfxGetApp ()->WriteProfileString ( _T ( "MM4" ), _T ("ProviderAccount" ), m_strAccount ); AfxGetApp ()->WriteProfileString ( _T ( "MM4" ), _T ("ProviderPassword" ), m_strPassword ); } else { AfxGetApp ()->WriteProfileString ( _T ( "MM4" ), _T ("ProviderAccount" ), _T("") ); AfxGetApp ()->WriteProfileString ( _T ( "MM4" ), _T ("ProviderPassword" ), _T("") ); } AfxGetApp ()->WriteProfileInt ( _T ( "MM4" ), _T ("ProviderPort" ), m_lPort ); } ///////////////////////////////////////////////////////////////////////////// VOID CDemoDlg::LoadSettings() { m_strHost = AfxGetApp ()->GetProfileString ( _T ( "MM4" ), _T ( "ProviderHost" ), _T ( "" ) ); m_strAccount = AfxGetApp ()->GetProfileString ( _T ( "MM4" ), _T ( "ProviderAccount" ), _T ( "" ) ); m_strPassword = AfxGetApp ()->GetProfileString ( _T ( "MM4" ), _T ( "ProviderPassword" ), _T ( "" ) ); m_lPort = AfxGetApp ()->GetProfileInt ( _T ( "MM4" ), _T ( "ProviderPort" ), 25L ); if ( m_strAccount.IsEmpty () == FALSE ) { m_bAuthentication = TRUE; } } ///////////////////////////////////////////////////////////////////////////// void CDemoDlg::OnButtonLicensing() { CDialogLicensing Dialog; Dialog.DoModal (); } ///////////////////////////////////////////////////////////////////////////// void CDemoDlg::OnButtonSlide() { HTREEITEM hTreeItem = m_Tree.InsertItem ( TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE, L"Slide", 2, 2, 0, 0, 0 ,NULL, NULL ); if ( hTreeItem ) { m_Tree.EnsureVisible ( hTreeItem ); m_Tree.SelectItem ( hTreeItem ); } } ///////////////////////////////////////////////////////////////////////////// void CDemoDlg::OnButtonText() { CDialogText Dialog; if ( Dialog.DoModal () == IDOK ) { m_Tree.EnsureVisible ( m_Tree.InsertItem ( TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE, Dialog.m_strText, 1, 1, 0, 0, 1 ,m_Tree.GetSelectedItem (), NULL ) ); } } ///////////////////////////////////////////////////////////////////////////// void CDemoDlg::OnButtonBrowse() { UpdateData (); CFileDialog cfd ( TRUE, NULL, 0, NULL, L"All Files (GetAdsiDatabase)|GetAdsiDatabase||\0\0"); if ( cfd.DoModal () == IDOK ) { CString strImage = cfd.GetPathName (); m_Tree.EnsureVisible ( m_Tree.InsertItem ( TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE, strImage, 0, 0, 0, 0, 0 ,m_Tree.GetSelectedItem (), NULL ) ); UpdateData ( FALSE ); } } ///////////////////////////////////////////////////////////////////////////// void CDemoDlg::OnButtonDelete() { m_Tree.DeleteItem ( m_Tree.GetSelectedItem () ); if ( m_Tree.GetCount () == 0 ) { GetDlgItem ( IDC_BUTTON_BROWSE )->EnableWindow ( FALSE ); GetDlgItem ( IDC_BUTTON_TEXT )->EnableWindow ( FALSE ); GetDlgItem ( IDC_BUTTON_DELETE )->EnableWindow ( FALSE ); } } ///////////////////////////////////////////////////////////////////////////// void CDemoDlg::OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult) { NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR; GetDlgItem ( IDC_BUTTON_BROWSE )->EnableWindow ( FALSE ); GetDlgItem ( IDC_BUTTON_TEXT )->EnableWindow ( FALSE ); GetDlgItem ( IDC_BUTTON_DELETE )->EnableWindow ( FALSE ); if ( pNMTreeView->itemNew.hItem ) { CString strLabel = m_Tree.GetItemText ( pNMTreeView->itemNew.hItem ); if ( strLabel.Find ( L"Slide" ) == 0 ) { GetDlgItem ( IDC_BUTTON_BROWSE )->EnableWindow ( TRUE ); GetDlgItem ( IDC_BUTTON_TEXT )->EnableWindow ( TRUE ); GetDlgItem ( IDC_BUTTON_DELETE )->EnableWindow ( TRUE ); } } *pResult = 0; } ///////////////////////////////////////////////////////////////////////////// void CDemoDlg::GetConfigDirectory(CString & strDirectory) { HKEY hKey; DWORD dwSize = MAX_PATH; CHAR lpszData [ MAX_PATH + 1 ]; DWORD dwType; if ( RegOpenKeyExA ( HKEY_LOCAL_MACHINE, "Software\\ActiveXperts\\SMS and MMS Toolkit", 0, KEY_READ, &hKey ) != ERROR_SUCCESS ) { return; } RegQueryValueExA ( hKey, "InstallRoot", NULL, &dwType, ( unsigned char * ) lpszData, &dwSize ); strDirectory = lpszData; RegCloseKey( hKey ); } ///////////////////////////////////////////////////////////////////////////// VOID CDemoDlg::EnableControls() { UpdateData ( TRUE ); GetDlgItem ( IDC_EDIT_ACCOUNT )->EnableWindow ( m_bAuthentication ); GetDlgItem ( IDC_EDIT_PASSWORD )->EnableWindow ( m_bAuthentication ); } ///////////////////////////////////////////////////////////////////////////// void CDemoDlg::OnCheckAuth() { EnableControls (); } ///////////////////////////////////////////////////////////////////////////// void CDemoDlg::OnClose() { UpdateData ( TRUE ); SaveSettings (); CDialog::OnClose(); } /////////////////////////////////////////////////////////////////////////////
There are many working samples included with the product.
You can also find them on the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/mobile-messaging-component.