0

Iam working on IMAP, reading mail content I got twice or third times of same mails content.

I had check out different different scenario so I comes to know Response() was gives me repeatitive content.

I am passing Command like this.

                byte[] commandBytes = System.Text.Encoding.ASCII.GetBytes(("$ UID FETCH " + index + " (BODY[HEADER.FIELDS (SUBJECT FROM DATE)])" + "\r\n").ToCharArray());
        _imapNs.Write(commandBytes, 0, commandBytes.Length);
        _imapNs.Flush();
        string strMsg = Response();

my member of Stream and TcpClient is.

private TcpClient _imapClient;
private Stream _imapNs;

and my Response method is here.

private string Response()
{
    byte[] data = new byte[_imapClient.ReceiveBufferSize];
    int ret = _imapNs.Read(data, 0, data.Length);
    return Encoding.ASCII.GetString(data);
}

once I had check this complete cycle then I comes to knows the Response() method gives me repeatitive content so is there any solution for that.....

Thanks...!!

Jitendra Jadav
  • 903
  • 3
  • 13
  • 27

3 Answers3

2

There is no way your code will work.

You have assumed that the server response is smaller than _imapClient.ReceiveBufferSize. Why?

Server response may be 50 MB long. There is no way you'll be able to read it with the single call to Stream.Read method.

Pawel Lesnikowski
  • 6,264
  • 4
  • 38
  • 42
  • Sorry to say but it's working some time's it give me repeatitive content. _imapClient.ReceiveBufferSize is the Fix size,I dont Know whether that would be the problem or not. – Jitendra Jadav Feb 23 '11 at 18:14
  • Well if it's working sometimes it is **NOT ** working. What happens if the response is 2025 bytes long and your buffer size (_imapClient.ReceiveBufferSize) is e.g. 1024? You need to use a loop over Read method. – Pawel Lesnikowski Feb 24 '11 at 11:04
0

I would recommend that you use one of the many libraries available for C# (I'm assuming that's what you're using due to the syntax of your example - you didn't specify).

Take a look at the existing StackOverflow questions.

Accessing IMAP in C#

Using C# .NET librarires to check for IMAP messages from GMail servers

Community
  • 1
  • 1
Jaco Pretorius
  • 24,380
  • 11
  • 62
  • 94
-1

I got the Solution, because I want to do all the thing inside the loop and I am doing all the thins outside the loop Thanks..!!

Jitendra Jadav
  • 903
  • 3
  • 13
  • 27