CS Outlook DataSet class

#region Outlook DataSet
private class OutlookDataSet
{
	#region DataSet Table Names
	private const string TableAppointment = "Appointment";
	private const string TableContact = "Contact";
	private const string TableMail = "Inbox";
	private const string TableTasks = "Tasks";
	private const string TableNotes = "Notes";
	#endregion

	#region Private Variables
	private Outlook.Application outLook = null;
	private Outlook.NameSpace nameSpace = null;
	private Outlook.MAPIFolder mapiFolder = null;
	private DataSet dataSet;
    #endregion

    public OutlookDataSet(Outlook.MAPIFolder currentFolder)
    {
        if (currentFolder == null)
        {
            outLook = new Outlook.Application();
            nameSpace = outLook.GetNamespace("MAPI");
        }
        else
        {
            mapiFolder = currentFolder;
        }

		initialize();
	}

	#region Properties
	protected internal DataTable this[string tableName] { get { return dataSet.Tables[tableName]; } }
	protected internal DataTable Appointment { get { return this[TableAppointment]; } }
	protected internal DataTable Contact { get { return this[TableContact]; } }
	protected internal DataTable Mails { get { return this[TableMail]; } }
	protected internal DataTable Tasks { get { return this[TableTasks]; } }
	protected internal DataTable Notes { get { return this[TableNotes]; } }
	#endregion

	private void initialize()
	{
		dataSet = new DataSet();

		#region Appointment
		dataSet.Tables.Add(TableAppointment);
		Appointment.Columns.Add("Subject");
		Appointment.Columns.Add("Location");
		Appointment.Columns.Add("Start");
		Appointment.Columns.Add("End");
		Appointment.Columns.Add("AllDayEvent");
		Appointment.Columns.Add("Duration");
		Appointment.Columns.Add("Organizer");
		Appointment.Columns.Add("Importance");
		Appointment.Columns.Add("Sensitivity");
		Appointment.Columns.Add("Body");
		#endregion

		#region Contact
		dataSet.Tables.Add(TableContact);
		Contact.Columns.Add("FirstName");
		Contact.Columns.Add("LastName");
		Contact.Columns.Add("CompanyName");
		Contact.Columns.Add("Email");
		Contact.Columns.Add("HomePhone");
		Contact.Columns.Add("WorkPhone");
		#endregion

		#region Mail
		dataSet.Tables.Add(TableMail);
		Mails.Columns.Add("From");
		Mails.Columns.Add("To");
		Mails.Columns.Add("Cc");
		Mails.Columns.Add("Subject");
		Mails.Columns.Add("Received");
		Mails.Columns.Add("Message");
		#endregion

		#region Tasks
		dataSet.Tables.Add(TableTasks);
		Tasks.Columns.Add("Subject");
		Tasks.Columns.Add("Categories");
		Tasks.Columns.Add("CreationTime");
		Tasks.Columns.Add("LastModificationTime");
		Tasks.Columns.Add("Contents");
		#endregion

		#region Notes
		dataSet.Tables.Add(TableNotes);
		Notes.Columns.Add("Subject");
		Notes.Columns.Add("Categories");
		Notes.Columns.Add("CreationTime");
		Notes.Columns.Add("LastModificationTime");
		Notes.Columns.Add("Contents");
		#endregion
	}

	public int GetFolderCount(Outlook.OlDefaultFolders folder)
	{
        if (mapiFolder == null)
		    mapiFolder = nameSpace.GetDefaultFolder(folder);
		return mapiFolder.Items.Count;
	}

	public void fillAppointments()
	{
		try
		{
            if (mapiFolder == null)
                mapiFolder = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
			foreach (var item in mapiFolder.Items)
			{
				Outlook.AppointmentItem _item = item as Outlook.AppointmentItem;
				Appointment.Rows.Add(new object[]
				{
					_item.Subject,
					_item.Location,
					_item.Start,
					_item.End,
					_item.AllDayEvent,
					_item.Duration,
					_item.Organizer,
					_item.Importance,
					_item.Sensitivity,
					_item.Body
				});
			}
		}
		catch (System.Exception ex)
		{
			throw ex;
		}
	}

	public void fillContacts()
	{
		try
		{
            if (mapiFolder == null)
                mapiFolder = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
			foreach (var item in mapiFolder.Items)
			{
				Outlook.ContactItem _item = item as Outlook.ContactItem;
				Contact.Rows.Add(new object[]
				{
					_item.FirstName,
					_item.LastName,
					_item.CompanyName,
					_item.Email1Address,
					_item.HomeTelephoneNumber,
					_item.BusinessTelephoneNumber
				});
			}
		}
		catch (System.Exception ex)
		{
			throw ex;
		}
	}

	public void fillInbox(string filter = null)
	{
		try
		{
            if (mapiFolder == null)
                mapiFolder = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            Mails.Clear();
            if (filter != null)
            {
                object folderItem = mapiFolder.Items.Find(filter);
                while (folderItem != null)
                {
                    Outlook.MailItem mailItem = folderItem as Outlook.MailItem;
                    if (mailItem != null)
                    {
                        MessageBox.Show(mailItem.Subject);
                    }
                    folderItem = mapiFolder.Items.FindNext();
                }
            }
            else
            {
                foreach (var item in mapiFolder.Items)
                {
                    if (item is Outlook.MailItem)
                    {
                        Outlook.MailItem _item = item as Outlook.MailItem;
                        Mails.Rows.Add(new object[]
                        {
                            (String.IsNullOrEmpty(_item.SenderName) ? _item.SenderEmailAddress : _item.SenderName),
                            _item.To,
                            _item.CC,
                            _item.Subject,
                            _item.ReceivedTime,
                            _item.Body
                        });
                    }
                }
            }
		}
		catch (System.Exception ex)
		{
            MessageBox.Show(ex.Message);
			throw ex;
		}
	}

	public void fillTasks()
	{
		try
		{
            if (mapiFolder == null)
                mapiFolder = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);
			foreach (var item in mapiFolder.Items)
			{
				Outlook.TaskItem _item = item as Outlook.TaskItem;
				Tasks.Rows.Add(new object[]
				{
					_item.Subject,
					_item.StartDate,
					_item.DueDate,
					_item.Status,
					_item.Body
				});
			}
		}
		catch (System.Exception ex)
		{
			throw ex;
		}
	}

	public void fillNotes()
	{
		try
		{
            if (mapiFolder == null)
                mapiFolder = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderNotes);
			foreach (var item in mapiFolder.Items)
			{
				Outlook.NoteItem _item = item as Outlook.NoteItem;
				Notes.Rows.Add(new object[]
				{
					_item.Subject,
					_item.Categories,
					_item.CreationTime,
					_item.LastModificationTime,
					_item.Body
				});
			}
		}
		catch (System.Exception ex)
		{
			throw ex;
		}
	}
}
#endregion

답글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다.