Вызываю данный код из потоков вот так:
new Imap(hostname, port, login, pass, true);
, после большого кол-ва раз начинается грузиться память. Я думаю что дело в методе MailboxCheck(). В чем может быть проблема?
class Imap
{
Form1 form1 = new Form1();
object conn = new object();
public Imap(string hostname, int port, string login, string pass, bool ssl)
{
Main(hostname, port, login, pass, ssl);
}
void Main(string hostname, int port, string login, string pass, bool ssl)
{
using (ImapClient client = new ImapClient(hostname, port, login, pass, AuthMethod.Login, true))
{
lock (conn)
{
form1.Connected(String.Format("{0}:{1}", login, pass));
}
var mailBoxList = client.ListMailboxes();
foreach (var mailBox in mailBoxList)
{
MailboxCheck(client, mailBox, login);
}
client.Dispose();
GC.Collect();
}
}
void MailboxCheck(ImapClient client, String mailBox, String username = null)
{
client.DefaultMailbox = mailBox;
IEnumerable<uint> uids = client.Search(SearchCondition.All());
IEnumerable<MailMessage> messages = client.GetMessages(uids);
foreach (MailMessage message in messages)
{
var attachments = message.Attachments;
string path = form1._folder;
if (!Directory.Exists(path))
{
DirectoryInfo di = Directory.CreateDirectory(path);// Try to create the directory.
}
if (form1._checktextBox && !form1.wordFind(message.Subject, message.Body, message.From.ToString(), message.To)) { continue; }
foreach (Attachment attachment in attachments)
{
path = form1._folder + username;
if (!Directory.Exists(path))
{
DirectoryInfo di = Directory.CreateDirectory(path);// Try to create the directory.
}
string destinationFile = path + "\\" + attachment.Name;
try
{
byte[] allBytes = new byte[attachment.ContentStream.Length];
int bytesRead = attachment.ContentStream.Read(allBytes, 0, (int)attachment.ContentStream.Length);
BinaryWriter writer = new BinaryWriter(new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
writer.Write(allBytes);
writer.Close();
}
catch
{
continue;
}
}
}
}
}