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:

using System.Net;
using System.Net.Mail;

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.To.Add("1234567890@txt.bell.ca");
message.From = new MailAddress("yourgmailaccount@gmail.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("yourgmailaddress@gmail.com", "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:

try
{
smtp.Send(message);

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

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.

Improvements:

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

Advertisements

19 comments

  1. uttam

    Thanks,It works properly.

    But it cost some money to open the sms that i have send and it displays my gmail id also .
    Is there is any way that i will able to send sms with out spending the money.
    Please tell me any way to solve this problem
    Thanks,
    Uttam

  2. Alex

    Thanks, it works well!
    I am using smtp.com account usually. So I tried with that server:
    SmtpClient smtp = new SmtpClient(”smtp.com”);
    smtp.EnableSsl = false;
    smtp.Port = 2525;

  3. Kevin Grigsby

    Hello Joel,

    Great post. Just happened upon it since I am looking to get a free web to phone solution finished up. I am wondering if it is still working on your end. Or has Google turned it off? All I get is “Failure sending mail” when I run it.

    My code is 100% like your except with my credentials. I tried to post it here but it keeps being discarded.

    Thanks,
    Kevin

  4. Joel

    I haven’t tried it in quite a while. It wouldn’t surprise me if the GMail SMTP service is no longer being provided. Does the exception you get contain any more detail than “Failure sending mail”?

  5. Kevin Grigsby

    Yes. It has the following within the Inner Exception:

    “Unable to connect to the remote server”

    No connection could be made because the target machine actively refused it 74.125.93.111:587

    at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
    at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
    at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)

    Kevin

    • Joel

      An operation timing out could be caused by a few different things. It’s possible that gmail is no longer operating as an SMTP server. It’s also possible there are troubles connecting to gmail (e.g.: bad network connection, something has blocked it, etc).

  6. kumar

    The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

  7. omprasad deshmukh

    hi…
    Please tell me where i can find “1234567890@txt.bell.ca” carrier format details of my mobile number…
    if i have tata docomo mobile number
    then what should i write insetead of txt.bell.ca
    where i can find the details regarding this…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s