Ops Ops - 12 days ago 12
C# Question

OpenPop Pop3 winform MaxMessage c#

I have problem with messages in mail program. If i have 20 mails it's ok load pretty fast but if i have 700 it's a problem it take a long of time. I can go make coffe and back. How I can set max messages to 50.

try
{

client.Connect(comboBox5.Text, 995, true);
client.Authenticate(textBox6.Text, textBox5.Text, OpenPop.Pop3.AuthenticationMethod.UsernameAndPassword);

int count = client.GetMessageCount();
string htmlContained = "";


for (int i = count; i >= 1; i -= 1)
{

OpenPop.Mime.Message message = client.GetMessage(i);

OpenPop.Mime.MessagePart html = message.FindFirstHtmlVersion();


if (html != null)
{

htmlContained = html.GetBodyAsText();


} else
{
html = message.FindFirstPlainTextVersion();
if (html != null)
{
htmlContained = html.GetBodyAsText();
}
}
string name = message.Headers.Subject;
if (name == "")
{
name = "Usigned";
}

dt.Rows.Add(new object[] { name.ToString(), message.Headers.From.DisplayName, message.Headers.From.Address, htmlContained, message.Headers.DateSent });

}

client.Disconnect();

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


}

Answer
for (int i = count; i >= 1; i -= 1) 

->

for (int i = 0; i < count && i < 50; i++)

Or (less redable according to me):

for (int i = count; i >= 1; && (count - i) < 50; i -= 1) 

You may also want to implement paging:

var pageSize = 50;
var startPage = x;

for (int i = x * pageSize; i < count && i < pageSize; i++)