Email to SMS
Written by Harry Fairhead   
Monday, 27 July 2009
Article Index
Email to SMS
POP3
You have mail
Reading the mail
A Skype class

A Skype class

Now it’s time to move on to the Skype class.

Add another class to the project called CSkype and add a reference, (Project, Add Reference), to SKYPE4COM to the class. If you can’t find SKYPE4COM listed in the COM window then you haven’t installed it yet.

Installation is easy and is often performed along with the basic installation of Skype as long as the user hasn't unchecked the Extras Manager option.

If it isn't installed then download it, unzip the file to a suitable location, start the command prompt and change directory to where you unzipped the files and use the command:

msiexec /i Skype4COM.msm

or you can also use Regsrv32 to register the DLL

Adding references adds generated .NET classes that correspond to the ActiveX objects described in the documentation.

ref

Load a reference to Skype4COM

 

Now we can start building the methods we need. First add:

using SKYPE4COMLib;

to the start of the class. The constructor checks that the Skype client is running and checks that the user is online, and if not places the user online:

class Cskype
{
private SkypeClass Skype;
public Cskype()
{
Skype = new SkypeClass();
if (!Skype.Client.IsRunning)
{
Skype.Client.Start(false, true);
};
Skype.Attach(5, false);
if (Skype.CurrentUserStatus ==
Skype.Convert.
TextToUserStatus("OFFLINE"))
{
Skype.ChangeUserStatus(
Skype.Convert.
TextToUserStatus("ONLINE"));
}
}

You have to give your permission to the program to use Skype for security reasons – select “Allow this program to use Skype”.

The Attach command specifies the lowest version of Skype that is acceptable to the program. Also notice that no error handling is implemented and if the Skype client doesn’t respond in a reasonable amount of time a “time out” error is triggered.

skype1

You have to give permission for another program to use Skype

 

Once we have the Skype object created we can write a method that places a phone call, (although this isn’t actually used in this project) and one to send an SMS:

 public void PlaceCall(string TeleNum)
{
Skype.PlaceCall(TeleNum, "", "", "");
}
public void SendSMS(string TeleNum, string SMSText)
{
SmsMessageClass SMS =
(SmsMessageClass)Skype.SendSms(
TeleNum,
SMSText, "");
}
}

It really is that simple.

Two subtle points - the phone numbers have to be in the format +countrycode i.e. +44 for the UK, followed by the number without the leading zero to work.

That is to dial 01234 you would enter “+441234”.

The SMS object returned by the SendSms method can be used to monitor the progress of the sending of the SMS – Skype automatically keeps it updated.

The Timer loop

Now we have the POP3 and Skype classes, it’s time to tie them all together and make the SMS gateway work.

Place two buttons and a Timer on the form. The Timer is going to be used to trigger a POP3 lookup every so often and any emails found will be retrieved and sent using the Skype class as an SMS. We need a carriage return linefeed global variable:

private string CRLF = "\r\n";

The first button starts the timer off with a 60 second interval:

private void button1_Click(
object sender,EventArgs e)
{
timer1.Interval = 60 * 1000;
timer1.Enabled = true;
}

The second button simply disables the timer:

private void button2_Click(
object sender,EventArgs e)
{
timer1.Enabled = false;
}

All of the work is done in the timer’s Tick event handler:

private void timer1_Tick(
object sender,EventArgs e)
{

First create a POP3 connection to the server:

 POP3 p = new POP3(
"Pop3 server",
"SMSGateway",
 "abcdef");

with the parameters changed to the POP3 server, user name and password to log on that you are actually using.

Find out how many emails are waiting:

int num = p.NumEmails;

If there are some, create a Skype object and for each email create an SMS message (max 160 characters) using the From and Subject headers and as much of the email body text as fits:

if (num > 0)
{
Cskype Skype = new Cskype();
string pad = new string(' ', 160);
for (int i = 1; i <= num; i++)
{
EmailMessage message = p.getMail(1);
string SMStext = message.From +
CRLF + message.Subject +
CRLF + message.Body + pad;
SMStext = SMStext.Substring(1, 160);

As well as adding together the headers and body text we also add 160 space characters at the end to make sure that the text is always more than 160 characters. This ensures that the final instruction can always successfully extract the first 160 characters without generating an error.

Finally we send the email to the mobile phone number and mark the email for deletion:

 Skype.SendSMS("+44123456789", SMStext);
p.delMail(1);
}

All that remains is tidying up by closing the POP3 object which actually asks the server to delete the marked emails.

  p.close();
}
}

As it stands the program will pick up every email that is sent to the gateway mailbox. If you plan to use it for real then you have to add some sort of spam filter or put up with receiving lots of second hand spam SMS messages which you will have to pay for!

A simple solution is to create a list of “From” email addresses that are allowed to be sent as SMS, i.e. a white list of senders.

Another area of improvement is to try to pick out the best bits of the email to cram into the 160 character SMS message or use multiple SMS messages to send the whole thing.

There are a lot more possibilities in the Skype API - for example see  Recording Skype.

Also you can't ignore all the documentation and examples at the Skype developers site and the forum which will answer any question you might have.

If you would like the code for this project then register and click on CodeBin.

 

<ASIN:B000YL1946@UK>

<ASIN:0307351254>

<ASIN:B00158UZTW@COM>

<ASIN:0596101899>



Last Updated ( Wednesday, 18 November 2015 )