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

 

You have mail

Moving on from making the connection and logging on to the server the first real task is to find out how many emails are waiting to be collected.

To do this the client sends “STAT” and the server responses with “+OK n m” where n is the number of emails and m is the amount of storage used in bytes. Once you know this a NumEmails method is easy:

public int NumEmails
{
get
{
try
{
SendData("STAT");
if (!WaitFor("+OK")) POPerror();
string[] nums = data.Split(' ');
return int.Parse(nums[1]);
}
catch
{
return 0;
}
}
}

Once again we use a try-catch because if the constructor failed for any reason there isn’t a server open to query.

In this case the catch clause simply returns 0 as the number of emails waiting. Notice the use of Split to divide the response text into three parts using spaces as the separator – “+Ok”, “n” and “m” and so the number of emails is in the second array element.

We need a small method to mark emails for deletion. The POP3 command is “DELE n” where n is the number of the email to mark for deletion. Emails don’t actually get deleted unit the server receives the command “QUIT”:

public void delMail(int n)
{
try
{
SendData("DELE " + n.ToString());
if (!WaitFor("+OK")) POPerror();
}
catch { }
}

public void close()
{
try
{
SendData("QUIT");
InStream.Close();
POP3Stream.Close();
catch { }
}

<ASIN:1597490326>

<ASIN:0596101899>

<ASIN:0977057526>



Last Updated ( Wednesday, 18 November 2015 )