在VB.NET中查找并返回outlook中最旧的电子邮件的ReceivedTime

时间:2015-09-23 01:18:39

标签: vb.net outlook mapi

好的到目前为止,我能够显示最旧的未读但不是最旧的电子邮件。当我这样做时,当然会停止程序。

#include <stdio.h>
#include "ldap.h"

#define SERVERIP 172.0.1.4
#define ROOTDN "cn=root,dc=jc,dc=com"
#define ROOTPW "secret"

int main()
{
    int protocol_version = LDAP_VERSION3;
    int ret;
    LDAP *ld;

    if((ld = ldap_init(SERVERIP,LDAP_PORT) == NULL)
    {
            perror("ldap_init failed\n");
            return 1;
    }
    ret = ldap_set_option(ld,LDAP_OPT_PROTOCOL_VERSION,&protocol_version);
    if(ret != LDAP_SUCCESS)
    {
            fprintf(stderr,"ldap_select_version:%s\n",ldap_err2string(ret));
            return 1;
    }
    ret = ldap_simple_bind_s(ld,ROOTDN,ROOTPW);
    if(ret != LDAP_SUCCESS)
    {
            fprintf(stderr,"ldap_simple_bind_s:%s\n",ldap_err2string(ret));
            return 1;
    }
    printf("add base dn\n");
    char baseDN[] = "dc=jc,dc=com";
    char *objVals1[] = {"organization",NULL};
    char *objVals2[] = {"dcObject",NULL};
    char *dcVals[] = {"jc",NULL};
    char *oVals[] = {"jc Inc",NULL};

    LDAPMod add1 = {LDAP_MOD_ADD,"objectClass",objVals1};
    LDAPMod add2 = {LDAP_MOD_ADD,"objectClass",objVals2};
    LDAPMod add3 = {LDAP_MOD_ADD,"dc",dcVals};
    LDAPMod add4 = {LDAP_MOD_ADD,"o",oVals};
    LDAPMod *addBase[] = {&add1,&add2,&add3,&add4,NULL};
    if(ldap_add_ext_s(ld,baseDN,addBase,NULL,NULL) != LDAP_SUCCESS)
            ldap_perror(ld,"ldap_add");
    else
            printf("ldap_add success\n");
    ldap_unbind(ld);        
}

我已根据提供的更改进行了编辑。似乎它没有抓住日期,但是程序不会因为循环而挂起。

2 个答案:

答案 0 :(得分:1)

首先,不要遍历文件夹中的所有项目。这是非常低效的。使用Items.Find / FindNext / Restrict。

在您的情况下(最早的电子邮件),请使用Items.Sort:

set vItems = objFolder.Items
vItems.Sort "ReceivedTime", false 'ascending order
set vOlderItems = vItems.GetFirst

如果您只想要最旧的未读电子邮件,请使用限制:

set vItems = objFolder.Items.Restrict("[Unread] = true ")
vItems.Sort "ReceivedTime", false 'ascending order
set vOlderItems = vItems.GetFirst

答案 1 :(得分:0)

感谢德米特里,它正在进行以下更改

        Dim objOutlook As Outlook._Application
    objOutlook = New Outlook.Application()
    Dim objNS As Outlook._NameSpace = objOutlook.Session
    Dim objRecipient As Outlook.Recipient = _
        objNS.CreateRecipient("John Smith")
    '  Dim eMail As Outlook.MailItem
    ' Dim travelunreadold As String
    Dim vItems
    Dim vvItems
    Dim vOlderItems
    Dim vOlderunreadItems

    If objRecipient.Resolve Then
        Dim objFolder As Outlook.MAPIFolder = _
            objNS.GetSharedDefaultFolder(objRecipient, _
            Outlook.OlDefaultFolders.olFolderInbox)
        txtTravUnread.Text = objFolder.UnReadItemCount
        txtTravEmails.Text = objFolder.Items.Count
        'For Each eMail In objFolder.Items
        '    If eMail.UnRead = True Then
        '        travelunreadold = eMail.ReceivedTime
        '        Txttravundate.Text = travelunreadold
        '    End If

        'Next eMail
        vvItems = objFolder.Items
        vvItems.Sort("ReceivedTime", False) 'ascending order
        vOlderItems = vvItems.GetFirst
        Txttravemdate.Text = FormatDateTime(vOlderItems.ReceivedTime, 0)


        vItems = objFolder.Items.Restrict("[Unread] = true")
        vItems.Sort("ReceivedTime", False) 'ascending order
        vOlderunreadItems = vItems.GetFirst
        Txttravundate.Text = FormatDateTime(vOlderunreadItems.ReceivedTime, 0)


        'Txttravemdate.Text = oldest.recievedtime

    Else
        Console.Write("Recipient could not be resolved.")
    End If