Bỏ qua để tới nội dung chính
Đăng nhập với Microsoft
Đăng nhập hoặc tạo một tài khoản.
Xin chào,
Chọn một tài khoản khác.
Bạn có nhiều tài khoản
Chọn tài khoản bạn muốn đăng nhập.

Tóm tắt

Bài viết này mô tả cách sử dụng Thư viện Đối tượng Microsoft Outlook 10.0 để truy xuất thư cụ thể bằng phương pháp Tìm và phương pháp Hạn chế trong Microsoft Visual C#.

Thông tin Bổ sung

Để sử dụng Thư viện Đối tượng Microsoft Outlook 10.0 để truy xuất thư cụ thể, hãy làm theo các bước sau:

  1. Khởi động Microsoft Visual Studio .NET hoặc Microsoft Visual Studio 2005.

  2. Trên menu Tệp, trỏ đến Mới, rồi bấm vào Tệp Project.

  3. Trong danh sách kiểu Dự án Visual C# , bấm Ứng dụng Bảng điều khiển.

    Theo mặc định, tệp Class1.cs được tạo.

    Lưu ý Trong Microsoft Visual C# 2005, hãy bấm Visual C#" trong danh sách kiểu Dự án Visual C# . Theo mặc định, Program.cs được tạo.

  4. Thêm tham chiếu đến Thư viện Đối tượng Microsoft Outlook 10.0. Để thực hiện việc này, hãy làm theo các bước sau:

    1. Trên menu Project, bấm Thêm Tham chiếu.

    2. Bấm vào tab COM, định vị Thư viện Outlook 10.0 của Microsoft, rồi bấm Chọn.

      Lưu ý trong Microsoft Visual C# 2005. thì bạn không cần phải bấm vào Chọn.

    3. Trong hộp thoại Thêm Tham chiếu, bấm OK.

    4. If you are prompted to generate wrappers for the libraries that you selected, click Yes.

  5. Trong cửa sổ mã, thay thế mã bằng các nội dung sau:

    using System;
    using System.Reflection; // to use Missing.Value

    namespace FilterAppointments
    {
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    public static int Main(string[] args)
    {
    try
    {
    // Create an Outlook application.
    Outlook.Application oApp = new Outlook.Application();

    // Get the Mapi NameSpace and the Logon.
    Outlook.NameSpace oNS = (Outlook.NameSpace)oApp.GetNamespace("mapi");

    // Log on using dialog to choose a profile.
    oNS.Logon(Missing.Value, Missing.Value, true, true);

    // Alternate Logon using specific profile
    // TODO: Change the profile name where it is appropriate.
    //oNS.Logon("YourValidProfile", Missing.Value, false, true);

    // Get the Calendar folder.
    Outlook.MAPIFolder oCalendar = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);

    // Get the Items collection in the folder.
    Outlook.Items oItems = (Outlook.Items)oCalendar.Items;
    Console.WriteLine("Total Items (unrestricted): " + oItems.Count);

    //Include all occurrences of recurring items, and then sort them.
    oItems.Sort ("[Start]", false);
    oItems.IncludeRecurrences = true;

    // Define the string for the search criteria.
    String sCriteria;

    // Set the criteria for the Date fields.
    sCriteria = "[Start] <= '09/01/2002 08:00 AM' and [End] >= '2/15/2002 08:00 PM'";

    // Set the criteria for a string field.
    // sCriteria = "[Subject] = 'Weekly recurring meeting'";

    // Set the criteria for a numeric field.
    // Look for meetings that have not been responded to.
    // sCriteria = "[ResponseStatus] = " + (Int32)Outlook.OlResponseStatus.olResponseNotResponded)

    // Use the Restrict method to reduce the number of items to process.
    Outlook.Items oRestrictedItems = oItems.Restrict(sCriteria);
    oRestrictedItems.Sort ("[Start]", false);
    oRestrictedItems.IncludeRecurrences = true;

    Console.WriteLine("Total Items Unrestricted : " + oRestrictedItems.Count);

    Outlook.AppointmentItem oAppointment;

    //Get each item until item is null.
    Outlook.AppointmentItem oAppt;
    oAppt = (Outlook.AppointmentItem)oRestrictedItems.GetFirst();
    while (oAppt != null)
    {
    // Console.WriteLine(i.ToString());
    Console.WriteLine(" Subject: " + oAppt.Subject.ToString());
    Console.WriteLine(" Start time: " + oAppt.Start.ToString());
    Console.WriteLine(" End time: " + oAppt.End.ToString());
    Console.WriteLine(" Occurrences: " + oAppt.RecurrenceState.ToString());
    Console.WriteLine("\n\n");
    oAppt = (Outlook.AppointmentItem)oRestrictedItems.GetNext();
    }


    // Use the Find method to get single match.
    sCriteria = "[Start] >= '09/30/2001'";
    Console.WriteLine("Criteria: " + sCriteria.ToString());
    oAppointment = (Outlook.AppointmentItem)oItems.Find(sCriteria);
    Console.WriteLine("Used Find with Date fields");
    if (oAppointment is Outlook.AppointmentItem)
    Console.WriteLine("Found -> " + oAppointment.Subject.ToString());
    else
    Console.WriteLine("No object found");


    // Log off
    oNS.Logoff();

    // Clean up
    oAppointment = null;
    oRestrictedItems = null;
    oItems = null;
    oCalendar = null;
    oNS = null;
    oApp = null;
    }

    //Simple error handling
    catch (Exception e)
    {
    Console.WriteLine("{0} Exception caught.", e);
    }

    //Default return value
    return 0;
    }
    }
    }
  6. Tìm kiếm chuỗi văn bản TODO trong mã, rồi sửa đổi mã cho môi trường của bạn.

  7. Nhấn phím F5 để xây dựng và chạy chương trình.

Tham khảo

Để biết thêm thông tin về các tính năng bảo mật của Outlook 2002, hãy bấm vào số bài viết sau đây để xem bài viết trong Cơ sở Tri thức Microsoft:

290500 Mô tả về các tính năng bảo mật email liên quan đến nhà phát triển trong Outlook 2002Đ


đối với thông tin bổ sung về các sự cố có thể xảy ra khi bạn sử dụng thuộc tính Count của tuyển tập lớn, hãy bấm vào số bài viết sau đây để xem bài viết trong Cơ sở Tri thức Microsoft:

294385 OL2002: Thuộc tính Đếm Không chính xác bằng cuộc hẹn định kỳ

Bạn cần thêm trợ giúp?

Bạn muốn xem các tùy chọn khác?

Khám phá các lợi ích của gói đăng ký, xem qua các khóa đào tạo, tìm hiểu cách bảo mật thiết bị của bạn và hơn thế nữa.

Cộng đồng giúp bạn đặt và trả lời các câu hỏi, cung cấp phản hồi và lắng nghe ý kiến từ các chuyên gia có kiến thức phong phú.

Thông tin này có hữu ích không?

Bạn hài lòng đến đâu với chất lượng dịch thuật?
Điều gì ảnh hưởng đến trải nghiệm của bạn?
Khi nhấn gửi, phản hồi của bạn sẽ được sử dụng để cải thiện các sản phẩm và dịch vụ của Microsoft. Người quản trị CNTT của bạn sẽ có thể thu thập dữ liệu này. Điều khoản về quyền riêng tư.

Cảm ơn phản hồi của bạn!

×