Monday, October 19, 2009

Retrieving inbox from mobile

For retrieving inbox details from a GSM mobile phone is not simple as the sending messages from phone using PC with the help of a AT Commands. Here we need to check whether your phone store your messages in the SIM memory or phone memory. We need to alter our messages according to the storage type of the messages.

Here are the example in the case of storing the messages inside the inbox.

AT Command

AT // For checking phone connected
AT+CMGF=1 // Setting message format
AT+CSCS="PCCP437" // Setting charset
AT+CPMS="SM" // Selecting sim message storage
AT+CMGL="ALL" // Reading all inbox messages

Function for reading message collection

public ShortMessageCollection ReadSMS(SerialPort port)
{

// Set up the phone and read the messages
ShortMessageCollection messages = null;
try
{

#region Execute Command
// Check connection
ExecCommand(port,"AT", 300, "No phone connected");
// Use message format "Text mode"
ExecCommand(port,"AT+CMGF=1", 300, "Failed to set message format.");
// Use character set "PCCP437"
ExecCommand(port,"AT+CSCS=\"PCCP437\"", 300, "Failed to set character set.");
// Select SIM storage
ExecCommand(port,"AT+CPMS=\"SM\"", 300, "Failed to select message storage.");
// Read the messages
string input = ExecCommand(port,"AT+CMGL=\"ALL\"", 5000, "Failed to read the messages.");
#endregion

#region Parse messages
messages = ParseMessages(input);
#endregion

}
catch (Exception ex)
{
throw new Exception(ex.Message);
}

if (messages != null)
return messages;
else
return null;

}

14 comments:

  1. Hi! The function can't work.
    ShortMessageCollection lost using namespace??
    Can u help me?
    Ur function is importment to me. Thank you
    this is my email : nelson_liu@msn.com

    ReplyDelete
  2. Okay, I will give you the whole class file. Please go to the link I provided.
    http://docs.google.com/View?id=dgc3t2gt_9gz4mc8hc

    Save this class file and add to your project.
    Please let us know if you have any more doubts.

    ReplyDelete
  3. Is there a reason for
    if (messages != null)
    return messages;
    else
    return null;
    Can't it just be
    return messages;

    ReplyDelete
  4. You need check your mobile phones set number, some nokia phones did not support the CGL AT commands.

    ReplyDelete
  5. hey Joseph
    I am making a project that needs to send and read messages using a GSM modem.
    I tried out the AT commands that you have posted here.
    But i had an error with the command AT+CPMS ="SM" and also with AT+CMGL="ALL"
    i am using my Nokia 6681 as a GSM modem.
    Can you pls help me with this ???
    I am able to send SMS but reading gives an error.

    ReplyDelete
  6. I think your mobile phone will not support the AT+CMGL="ALL" command , use little older nokia phone.

    ReplyDelete
  7. hi sir,
    i use the complete class file.but it create the following error :
    The type or namespace name 'ShortMessageCollection' could not be found (are you missing a using directive or an assembly reference?).
    please help me.
    my email address:cse.tanim@yahoo.com
    thank you.

    ReplyDelete
  8. Check your are correctly given the proper Namespaces , System.IO etc.

    ReplyDelete
  9. i use the following namespaces.but it contineously generate the error:
    The type or namespace name 'ShortMessageCollection' could not be found (are you missing a using directive or an assembly reference?).

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.IO.Ports;
    using System.Threading;
    using System.Text.RegularExpressions;
    plz help me an inform me that what the actual missing namespaces that can solve the error

    thank you.

    ReplyDelete
  10. okay , there are two extra clases called
    ShortMessage.cs and ShortMessageCollection.cs and it contains the code

    create new class ShortMessage.cs in your project and paste these code
    using System;
    using System.Collections.Generic;
    using System.Text;

    public class ShortMessage
    {

    #region Private Variables
    private string index;
    private string status;
    private string sender;
    private string alphabet;
    private string sent;
    private string message;
    #endregion

    #region Public Properties
    public string Index
    {
    get { return index;}
    set { index = value;}
    }
    public string Status
    {
    get { return status;}
    set { status = value;}
    }
    public string Sender
    {
    get { return sender;}
    set { sender = value;}
    }
    public string Alphabet
    {
    get { return alphabet;}
    set { alphabet = value;}
    }
    public string Sent
    {
    get { return sent;}
    set { sent = value;}
    }
    public string Message
    {
    get { return message;}
    set { message = value;}
    }
    #endregion

    }

    public class ShortMessageCollection : List
    {
    }


    create another class called ShortMessageCollection.cs and paste these code

    using System;
    using System.Collections.Generic;
    using System.Text;

    ReplyDelete
  11. The article has been copied from:
    http://69.10.233.10/KB/cs/Send_and_Read_SMS.aspx

    Check out this link for more details.

    ReplyDelete
  12. Yes you are right, I have alredy gave the reference link to the codeproject. My project is not the copy of codeproject's project combined app from many websites. I have given those websites link as reference at the home.

    ReplyDelete
  13. haii

    I got an Error
    "The name 'ExecCommand' does not exist in the current context"

    Please send the codes
    on praneeshpeeyar.codes@gmail.com if u dont mind

    ReplyDelete
  14. do you have the code for the same in c++

    ReplyDelete

Thanks for your valuable comments.