将我的未读邮件按接收时间排序 [英] Sort my unread mail by received time

查看:139
本文介绍了将我的未读邮件按接收时间排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
Microsoft.Office.Interop.Outlook.MailItem item = null;
Microsoft.Office.Interop.Outlook.MAPIFolder inBox = null;
Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;
ns = app.GetNamespace("MAPI");
ns.Logon(null, null, false, false);
inBox = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

//Microsoft.Office.Interop.Outlook.MAPIFolder inBox = Microsoft.Office.Interop.Outlook.MAPIFolder.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Microsoft.Office.Interop.Outlook.Items inBoxItems = inBox.Items;
Microsoft.Office.Interop.Outlook.MailItem newEmail = null;
inBoxItems = inBoxItems.Restrict("[Unread] = true");

try
{
    int k = 0;
    //******inBoxItems.Sort(, true);****////
    foreach (object collectionItem in inBoxItems)
    {
            newEmail = collectionItem as Microsoft.Office.Interop.Outlook.MailItem;
            if (newEmail != null)
            {
                if (newEmail.Attachments.Count > 0)
                {
                    for (int i = 1; i <= newEmail.Attachments.Count; i++)
                    {
                        if (newEmail.Attachments[i].FileName == "Call Letter.doc" || newEmail.Attachments[i].FileName == "VINEET SEHEJU_DOC.doc")
                        {
                            newEmail.Attachments[i].SaveAsFile(@"C:\1001\" + newEmail.Attachments[i].FileName);
                            //goto xLbl;
                        }
                    }
                }
            }
    }
    xLbl:
    MessageBox.Show("Done");
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
finally
{
    ns = null;
    app = null;
    inBox = null;
    item = null;
}


我编写的上述代码可以正常工作,但是如果找到2封具有相同附件的电子邮件,则它将保存最新的一封电子邮件.


The above code I wrote is working, but if 2 emails with same attachment are found then it saves the latest one.

推荐答案

如果您在阅读它们时必须存储attachmenst,那么这很简单-您必须做Windows资源管理器为您做的事情:

检查文件是否存在.
如果没有,请编写.
如果是这样,请读取所有与yourfilename +(" + number +)"相匹配的文件.
找到最高的数字
将其递增一个,然后尝试以此方式写入文件.

您最终将得到:
If you must store the attachmenst when you read them, then it''s pretty simple - you have to do what Windows Explorer does for you:

Check if the file exists.
If it doesn''t, write it.
If it does, read all the files which match yourfilename + " (" + number + ")".
Find the highest number
Increment it by one, and try writing your file as that.

You will end up with:
MyFile.txt
MyFile (1).txt
MyFile (2).txt
...

如果您不这样做,那么您的代码只会覆盖每个文件.

其他替代方案包括不将附件保留在文件中,而是将其保留在数据库中,或者将每封邮件及其附件一起保留在单独的文件夹中.

就个人而言,我会将电子邮件和附件详细信息保存在数据库中,并将实际的附件文件以临时名称存储在专用于此的文件夹中.

If you don''t do this, then your code will just overwrite each file as it comes in.

Other alternatives include not to keep the attachments in files, but to keep them in a database instead, or to keep each message in a separate folder, together with it''s attachments.

Personally, I would keep emails and attachment details in a database, and store the actual attachment files under a temporary name in a folder specifically for that.


如果您只想保存最新的附件:

将带有附件的每封电子邮件添加到列表,然后使用Linq进行OrderBy.

varorderedList = list.OrderBy(i => i.Date,SortDirection.Descending)

当您浏览电子邮件时,将保存的文件添加到列表并在保存之前搜索列表(您还可以检查文件是否退出,但是您如何处理现有文件?)
If you only want to save the latest one:

Would add each email with an attachment to a List and then use Linq to do an OrderBy.

var orderedList = list.OrderBy (i => i.Date, SortDirection.Descending)

As you go through the emails add saved files to a list and search list before saving (you could also check to see if file exits, but then what do you do about existing files?)


这篇关于将我的未读邮件按接收时间排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆