Send a text msg from C#

Saturday, February 14, 2009

I've finally found out how to send any instant updates that I want, to my cell phone.
It's a pretty simple concept although the toughest part has always been finding a free or available smtp server to use. Look no further, Google lets you use their smtp server free of charge! You only need to sign up for a free gmail account. The only draw back is that you can't change the sender name, it must always be the email address @gmail.
Thus far I've been able to email all sorts of attachments out!

The concept within a web application:
  • Gather the data you want to send
  • Send it to a C# web service
  • Receive the data in C#, create a new mail object.
  • enter the 10 digit phone number and the cell suffix into the email sender property
  • send the text message!
The cell suffix is different per carrier. A comprehensive list can be found here
for example 1112223333@vtext.com (verizon subscriber).

Update: It's very easy to do if you use RC's GmailHelper.dll

Now for the code! (Old way of doing it, I recommend RC's GmailHelper.dll)

System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();

myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.gmail.com");

myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");

myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");

myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");

//Use 0 for anonymous

myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "myEmailAddress@gmail.com");
//this is the gmail account that allows us to use the google smtp server

myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "emailPassword");

myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");

myMail.From = "myEmailAddress@gmail.com"; //this is the gmail account that allows us to use the google smtp server

myMail.To = "recipientEmailAddress";

myMail.Subject = "emailSubject";

myMail.BodyFormat = System.Web.Mail.MailFormat.Text;

myMail.Body = "email body text goes here";

//if (pAttachmentPath.Trim() != "")

//{

//System.Web.Mail.MailAttachment MyAttachment = new System.Web.Mail.MailAttachment(Server.MapPath("pathToAttachmentFile goes here"));
//may be able to send images in text messages, not sure, haven't tried yet.

//myMail.Attachments.Add(MyAttachment);

//myMail.Priority = System.Web.Mail.MailPriority.High;


System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";

System.Web.Mail.SmtpMail.Send(myMail);

0 comments:

Post a Comment