본문 바로가기

블로그Tip

Outlook Add-In (macOS 한글 자소분리 해결)

반응형

Outlook Add In

 

macOC에서 메일 첨부파일이 자소 분리되는 불편함이 있어

Outlook 프로그램을 찾아보았지만 보이지 않아 Outlook Add-In을 만들어 봤습니다.

[메일에서 파일명 깨져보이는 이유]

macOS에서 한글 인코딩 : NFD(Normalization Form Canonical Decomposition) = 조합형

윈도우에서 한글 인코딩 :  NFC(Normalizaiton Form Canonical Compostion) = 완성형

 

C#으로 조합형 -> 완성형으로 바꾸는 코드

파일.FileName.Normalize(NormalizationForm.FormC)

 

첨부파일 찾아 변환후 메일을 다시 저장하는 방식으로 작성되었습니다.

 

public partial class ThisAddIn
    {
        private Outlook.Inspectors inspectors;

        private Outlook.Explorer currentExplorer;
        public object selectedObject = null;


        private void ThisAddIn_Startup(object sender, EventArgs e)
        {
           currentExplorer = this.Application.ActiveExplorer();
            currentExplorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(CurrentExplorer_Event);

        }

        public void CurrentExplorer_Event()
        {
            string PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001F";
            string cid = "";

            try
            {
                if (this.Application.ActiveExplorer().Selection.Count == 1
            && this.Application.ActiveExplorer().Selection[1] is Outlook.MailItem)
                {

                    selectedObject = this.Application.ActiveExplorer().Selection[1];
                    Outlook.MailItem mailItem = selectedObject as Outlook.MailItem;
                    string strTitle = mailItem.Subject;


                    if (mailItem != null)
                    {
                        if (mailItem.Attachments.Count > 0)
                        {
                            for (int i = mailItem.Attachments.Count; i > 0; i--)
                            {
                                cid = mailItem.Attachments[i].PropertyAccessor.GetProperty(PR_ATTACH_CONTENT_ID);
                                if (cid != "" && mailItem.HTMLBody.Contains(cid) == true) continue;
                                if (IsMacFile(mailItem.Attachments[i].FileName) == true)
                                {

                                    string newFileName = mailItem.Attachments[i].FileName.Normalize(NormalizationForm.FormC);
                                    string tempFilePath = Path.Combine(Path.GetTempPath(), newFileName);
                                    mailItem.Attachments[i].SaveAsFile(Path.Combine(Path.GetTempPath(), newFileName));
                                    Outlook.Attachment newAttachment = mailItem.Attachments.Add(tempFilePath);
                                    newAttachment.DisplayName = newFileName;
                                    mailItem.Attachments[i].Delete();
                                    mailItem.Save();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
           
        }

        private bool IsMacFile(string str)
        {
            string newstr = str.Normalize(NormalizationForm.FormC);

            if (str != newstr)
            {
                return true;
            }
            return false;
        }


        private void ThisAddIn_Shutdown(object sender, EventArgs e)
        {
        }

        #region VSTO 디자이너 생성 코드

        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
반응형

'블로그Tip' 카테고리의 다른 글

ChatGPT 자주하는 질문  (0) 2023.07.14
Youtube 로얄티 프리 음악채널  (0) 2023.07.08
무료 이미지 추천 Best5  (0) 2023.07.08