Hi @Micah Holmes , Welcome to Microsoft Q&A,
Make sure you are using the latest version of the Microsoft Office Interop library. Outlook may block external applications from accessing certain fields: 1. Open Outlook. 2. Go to File > Options > Trust Center > Trust Center Settings > Programmatic Access. 3. Check if "Warn me about suspicious activity" is selected. If it is, consider changing it to "Never warn me".
Accessing items by index (subFolder.Items[i]
) may cause problems with the interop library. You can iterate using a foreach
loop.
Outlook updates may introduce security measures that prevent direct access to sensitive fields. Explicitly use property access to avoid permission issues:
foreach (var obj in subFolder.Items)
{
if (obj is Microsoft.Office.Interop.Outlook.MailItem mailItem)
{
MailItem mi = new MailItem();
mi.DateReceived = mailItem.SentOn;
mi.Subject = mailItem.Subject;
mi.EmailBodyText = mailItem.Body;
mi.HTMLBody = mailItem.HTMLBody;
mi.FromAddress = mailItem.SenderEmailAddress;
foreach (Microsoft.Office.Interop.Outlook.Attachment att in mailItem.Attachments)
{
string attachedFile = string.Format("{0}{1:00#}_{2:0#}_{3}", processingFileDir, DateTime.Today.DayOfYear, subFolder.Items.IndexOf(obj), att.FileName);
if (Path.GetExtension(attachedFile).ToUpper().EndsWith("ZIP"))
{
att.SaveAsFile(attachedFile);
ZipFile zippedAttachment = new ZipFile(attachedFile);
zippedAttachment.ExtractAll(processingFileDir, true);
foreach (string extractedFile in zippedAttachment.EntryFileNames)
{
mi.AttachedFiles.Add(string.Concat(processingFileDir, extractedFile));
}
if (File.Exists(attachedFile))
{
zippedAttachment.Dispose();
File.Delete(attachedFile);
}
}
else
{
att.SaveAsFile(attachedFile);
mi.AttachedFiles.Add(attachedFile);
}
}
m_RetrievedMailItems.Add(mi);
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.