Quantcast
Channel: ImapX 2
Viewing all articles
Browse latest Browse all 1952

Commented Unassigned: OutOfMemoryException when downloading some attachments [2152]

$
0
0
Trying to set something up to download and store PDF attachments, and I get the OutOfMemoryException. Here's my stack trace.

at System.Text.StringBuilder.ToString()
at System.String.Format(IFormatProvider provider, String format, Object[] args)
at System.String.Format(String format, Object arg0, Object arg1)
at ImapX.ImapBase.SendAndReceive(String command, IList`1& data, CommandProcessor processor, Encoding encoding, Boolean pushResultToDatadespiteProcessor)
at ImapX.MessageContent.Download()
at ImapX.Attachment.Download()
Comments: ** Comment from web user: supashurume **

Apologies in advance if I'm doing something really dumb and don't realize it.

private static int GetAttachments()
{
int numProcessed = 0;
string addr = ConfigurationManager.AppSettings["MailAddress"];
string pwd = ConfigurationManager.AppSettings["MailPassword"];
ImapClient client = new ImapClient("imap.gmail.com", true);
if (client.Connect() && client.Login(addr, pwd))
{
Folder inbox = client.Folders.Inbox;
inbox.Messages.Download();
List<Message> msgs = inbox.Messages.Where(p => !p.Seen && p.Attachments.Count() > 0).ToList();

if (msgs.Count() == 0)
{
WriteMessage("No new Messages with Attachments");
}
else
{
foreach (Message m in msgs)
{
// parse the file name
Match custNumMatch = new Regex(ParseExpCustNum, RegexOptions.IgnoreCase).Match(m.Subject);

int customerNumber;

// make sure the subject parses right
if (custNumMatch.Success && int.TryParse(custNumMatch.Value, out customerNumber))
{
foreach (Attachment a in m.Attachments)
{
if (a.ContentType.MediaType == MediaTypeNames.Application.Pdf)
{
a.Download();

try
{
// match the document
List<SalesAttempt> salesAttempts = SalesAttemptDL.SelectSalesAttemptsForDocs(customerNumber);

if (salesAttempts.Count == 0)
{
// can't be parced because it doesn't have a sales attempt
// OR it has too many sales attempts and we don't know which
// to use
m.MoveTo(client.Folders.Trash);
}
else if (salesAttempts.Count == 1)
{
// if there is only one matching sales attempt go ahead and add the file.
ProcessFile(client, m, a, a.FileName, salesAttempts[0].SANum, true);
numProcessed++;
}
else
{
// count the number of sales attempts that are less than six months old
int saCount = 0;

foreach (SalesAttempt sa in salesAttempts)
{
if (sa.SADt > DateTime.Now.AddDays(-180))
{
saCount++;
}
}

// add the file to every sales attempt that is less than 6 months old.
foreach (SalesAttempt sa in salesAttempts)
{
// 1. if there aren't any less than 6 months old add it to all of them
// 2. if there are more than 1 that is < 6 months old add it to the ones that are < 6 months old
if (saCount == 0 || (saCount > 0 && sa.SADt > DateTime.Now.AddDays(-180)))
{
bool deleteWhenDone = false;

if (saCount > 0 && numProcessed == (saCount - 1))
{
// this is the last time we need to process this file so delete it when done
deleteWhenDone = true;
}
else if (numProcessed == salesAttempts.Count - 1)
{
// we are processing all of them since they are all old
// delete after the last sales attempt is processed
deleteWhenDone = true;
}

// process the sole sales attempt
ProcessFile(client, m, a, a.FileName, sa.SANum, deleteWhenDone);

// record that we processed this file another time
numProcessed++;
}
}

}
}
catch (Exception ex)
{
m.MoveTo(client.Folders.Trash);

// report something happened while processing a file
// this catch allows the loop to keep running for all the other files
WriteMessage_Error(ex, "GetAttachments");
}
}
}
}
m.Seen = true;
}
}
}
else
{
WriteMessage("Connection Failed");
numProcessed = -1;
}
return numProcessed;
}


Viewing all articles
Browse latest Browse all 1952

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>