Sending text messages (via SMS) to a mobile phone from a C# Windows Application via GMail
A while ago I was thinking about different features for Todo 4.0, and one particular one crossed my mind: Have the application send a text message to my cell phone when a task is due.
I did a bit of digging around to see how it was done. Generally, you have something called an SMS Gateway that allows you to do exactly that. (SMS is the Short Message Service that allows you to send short text messages on the telecommunication network. The problem with this is that SMS Gateway hardware isn’t exactly your standard fare – you can’t pick up an SMS Gateway at your local big-box mart. The other option to owning your own equipment is to purchase bulk messages – usually at the cost of one tenth of a cent to a cent per message. Again, this costs money.
One of the wonderful folks over at GameDev.net posted a link to a wikipedia article that contained a list of email addresses that allow you to convert an email to a text message (Here is the list). What this does is allows you to send an email to the particular email address (depending on the phone number and the mobile carrier) which will get converted into a text message. Now in order to send emails from your application, you would normally need some sort of SMTP Gateway – a server set up so that it can send out emails. Again, there is a way around this.
With a bit of googling, I came across this wonderful but little known fact: At the time of writing, GMail accounts can actually be used as an SMTP server, in a manner of speaking. That’s exactly what we need – a way of sending an email to the mobile phone carrier, so they can in turn send a text message to the mobile phone. We send the email to GMail, GMail passes it on to the mobile phone carrier, which turns it into a text message and sends it to our cell phone. Yes, it does involve a few steps, but you can’t argue against the price (free!).
The SMTP server functionality isn’t exactly available for anyone to get access to – you have to have a GMail account (which is free). Because of this we will need to do a few special things. More on that later though. Let’s see how it’s done.
Here’s a code snippet on how to do it. First of all, in our Windows App, we need to include the following “using” statements:
Somewhere in the body of the program, we need to create a new MailMessage, give it some details, like so:
MailMessage message = new MailMessage();
message.From = new MailAddress("email@example.com", "App"); //See the note afterwards...
message.Body = "This is your cell phone. How was your day?";
At this point the email address that the message comes from really doesn’t matter – it won’t show up as GMail will override it when it gets passed on to the mobile phone carrier. The important one to get right is the email address that it is going to be sent to. This depends on your phone number and your mobile carrier. In this case, the phone number is (123)456-7890 and the carrier is Bell. Check out the previously mentioned wikipedia link to see what your carrier specific email address is. If you don’t get this right, the message will most certainly not get through.
Next, we need to create an SMTP client, and set it up:
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("firstname.lastname@example.org", "yourgmailpassword");
Here we create a new SMTP Client that will connect to the GMail SMTP server. Because we are connecting to a GMail secured server we need to enable SSL, as well as use a particular port – port 587. (There is apparently another port that can be used, but I haven’t heard of anyone getting it to work – port 465). Next, you need to put your own GMail email address where “yourgmailaddress.gmail.com” is, as well as replace “yourgmailpassword” with your actual GMail password.
After this, there isn’t much you need to do – send the email, like so:
catch (Exception ex)
It’s a good idea to put it in a throw/catch, as it could quite possibly explode.
If it all worked, you should recieve a message in 15-20 seconds. It is a bit of a delay, but considering it is done for free, I’m not going to complain. There will be a bit of a delay when you first do this.
There are a number of ways that this could be improved. It could be a good idea to do this on a separate worker thread to prevent the delay that you might experience when sending the message, for one. Another would be to set up the user interface so that the end user can put their own GMail account and cell phone number in, rather than having to recompile each time you want to change it.
It is important to remember that both the GMail SMTP functionality and the carrier specific email address could change. The service is offered for free, so it’s not like it is set in stone and will be around forever.
Here’s some other links to check out on the subject:
Wikipedia listing of email to SMS addresses
Source snippet on how to do exactly what was just done
LifeHacker article on how to use the GMail SMTP feature