첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

▶ CustomAddIn.cs

using Microsoft.Office.Interop.Outlook;
using System;
using System.Diagnostics;

namespace TestProject
{
    /// <summary>
    /// 커스텀 애드인
    /// </summary>
    public partial class CustomAddIn
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 애플리케이션
        /// </summary>
        private Application application = null;

        /// <summary>
        /// 타겟 폴더
        /// </summary>
        private Folder targetFolder = null;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 커스텀 애드인 시작시 처리하기 - CustomAddIn_Startup(sender, e)

        /// <summary>
        /// 커스텀 애드인 시작시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void CustomAddIn_Startup(object sender, EventArgs e)
        {
            this.application = Application;

            // Outlook에서 계정 폴더 아래에 Parent\Child 폴더가 사전에 생성되어 있어야 한다.
            Folder parentFolder = this.application.ActiveExplorer().Session.Folders[1].Folders["Parent"] as Folder;
            
            this.targetFolder = parentFolder.Folders["Child"] as Folder;

            this.application.NewMailEx += application_NewMailEx;
        }

        #endregion
        #region 커스텀 애드인 셧다운시 처리하기 - CustomAddIn_Shutdown(sender, e)

        /// <summary>
        /// 커스텀 애드인 셧다운시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void CustomAddIn_Shutdown(object sender, EventArgs e)
        {
        }

        #endregion

        #region 애플리케이션 신규 메일 수신시 처리하기 (확장) - application_NewMailEx(entryIDCollection)

        /// <summary>
        /// 애플리케이션 신규 메일 수신시 처리하기 (확장)
        /// </summary>
        /// <param name="entryIDCollection">엔트리 ID 컬렉션</param>
        private void application_NewMailEx(string entryIDCollection)
        {
            Debug.WriteLine("BEGIN APPLICATIOn NEW MAIL EX");

            Debug.WriteLine($"ENTRY ID COLLECTION : {entryIDCollection}");

            string[] entryIDArray = entryIDCollection.Split(',');

            foreach(string entryID in entryIDArray)
            {
                MailItem mailItem = this.application.ActiveExplorer().Session.GetItemFromID(entryID);

                if(mailItem != null)
                {
                    Debug.WriteLine($"    {entryID}:{mailItem.Subject}");

                    try
                    {
                        mailItem.Move(this.targetFolder);
                    }
                    catch(System.Exception exception)
                    {
                        Debug.WriteLine(exception.ToString());
                    }
                }
            }

            Debug.WriteLine("END APPLICATION NEW MAIL EX");
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region VSTO에서 생성한 코드

        /// <summary>
        /// 디자이너 지원에 필요한 메서드입니다. 
        /// 이 메서드의 내용을 코드 편집기로 수정하지 마세요.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup  += new System.EventHandler(CustomAddIn_Startup );
            this.Shutdown += new System.EventHandler(CustomAddIn_Shutdown);
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요