[C#/COMMON/OUTLOOK] MailItem 인터페이스 : EML 파일 저장하기
■ MailItem 인터페이스를 사용해 EML 파일을 저장하는 방법을 보여준다. ▶ RibbonContextMenu.xml
1 2 3 4 5 6 7 8 9 10 11 12 |
<?xml version="1.0" encoding="UTF-8"?> <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="customUI_onLoad"> <contextMenus> <contextMenu idMso="ContextMenuMailItem"> <button id="saveEMLFileButton" label="EML 파일 저장하기" onAction="contextMenu_onAction" /> </contextMenu> </contextMenus> </customUI> |
▶ RibbonContextMenu.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
using Microsoft.Office.Core; using Microsoft.Office.Interop.Outlook; using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Runtime.InteropServices; using System.Windows.Forms; using MimeKit; namespace TestProject { /// <summary> /// 리본 컨텍스트 메뉴 /// </summary> [ComVisible(true)] public class RibbonContextMenu : IRibbonExtensibility { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 리본 UI /// </summary> private IRibbonUI ribbonUI; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - RibbonContextMenu() /// <summary> /// 생성자 /// </summary> public RibbonContextMenu() { } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public //////////////////////////////////////////////////////////////////////////////// Event #region 커스텀 UI 로드시 처리하기 - customUI_onLoad(ribbonUI) /// <summary> /// 커스텀 UI 로드시 처리하기 /// </summary> /// <param name="ribbonUI">리본 UI</param> public void customUI_onLoad(IRibbonUI ribbonUI) { this.ribbonUI = ribbonUI; } #endregion #region 컨텍스트 메뉴 작업시 처리하기 - contextMenu_onAction(ribbonControl) /// <summary> /// 컨텍스트 메뉴 작업시 처리하기 /// </summary> /// <param name="ribbonControl">리본 컨트롤</param> public void contextMenu_onAction(IRibbonControl ribbonControl) { switch(ribbonControl.Id) { case "saveEMLFileButton" : try { dynamic itemCollection = ribbonControl.Context; foreach(dynamic item in itemCollection) { if(item is MailItem mailItem) { SaveEMLFile(mailItem, "d:\\test.eml"); } } } catch(System.Exception exception) { MessageBox.Show($"예외가 발생하였습니다.\r\n{exception.ToString()}"); } break; } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region (IRibbonExtensibility) 커스텀 UI 구하기 - GetCustomUI(ribbonID) /// <summary> /// 커스텀 UI 구하기 /// </summary> /// <param name="ribbonID">리본 ID</param> /// <returns>커스텀 UI</returns> public string GetCustomUI(string ribbonID) { return ribbonID.Equals("Microsoft.Outlook.Explorer") ? GetResourceText("TestProject.RibbonContextMenu.xml") : null; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Function #region 리소스 텍스트 구하기 - GetResourceText(resourceName) /// <summary> /// 리소스 텍스트 구하기 /// </summary> /// <param name="resourceName">리소스명</param> /// <returns>리소스 텍스트</returns> private static string GetResourceText(string resourceName) { Assembly assembly = Assembly.GetExecutingAssembly(); string[] resourceNameArray = assembly.GetManifestResourceNames(); int count = resourceNameArray.Length; for(int i = 0; i < count; ++i) { if(string.Compare(resourceName, resourceNameArray[i], StringComparison.OrdinalIgnoreCase) == 0) { using(StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(resourceNameArray[i]))) { if(reader != null) { return reader.ReadToEnd(); } } } } return null; } #endregion #region EML 파일 저장하기 - SaveEMLFile(mailItem, filePath) /// <summary> /// EML 파일 저장하기 /// </summary> /// <param name="mailItem">메일 항목</param> /// <param name="filePath">파일 경로</param> private void SaveEMLFile(MailItem mailItem, string filePath) { System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(); mailMessage.Subject = mailItem.Subject; mailMessage.From = new System.Net.Mail.MailAddress(mailItem.Sender.Address, mailItem.Sender.Name); mailMessage.IsBodyHtml = mailItem.BodyFormat == OlBodyFormat.olFormatHTML; mailMessage.Body = mailItem.BodyFormat == OlBodyFormat.olFormatHTML ? mailItem.HTMLBody : mailItem.Body; foreach(Recipient recipient in mailItem.Recipients) { if(recipient.Type == (int)OlMailRecipientType.olTo) { mailMessage.To.Add(new System.Net.Mail.MailAddress(recipient.Address, recipient.Name)); } else if(recipient.Type == (int)OlMailRecipientType.olCC) { mailMessage.CC.Add(new System.Net.Mail.MailAddress(recipient.Address, recipient.Name)); } else if(recipient.Type == (int)OlMailRecipientType.olBCC) { mailMessage.Bcc.Add(new System.Net.Mail.MailAddress(recipient.Address, recipient.Name)); } } List<string> temporaryDirectoryPathList = new List<string>(); try { foreach(Attachment attachment in mailItem.Attachments) { #region 임시 디렉토리 경로를 설정한다. string temporaryDirectoryPath = Path.Combine(Path.GetTempPath(), "Temp", Guid.NewGuid().ToString()); #endregion #region 임시 디렉토리 경로를 생성한다. if(!Directory.Exists(temporaryDirectoryPath)) { Directory.CreateDirectory(temporaryDirectoryPath); } #endregion temporaryDirectoryPathList.Add(temporaryDirectoryPath); #region 첨부 파일을 저장한다. string attachmentFilePath = Path.Combine(temporaryDirectoryPath, attachment.FileName); attachment.SaveAsFile(attachmentFilePath); #endregion #region MIME 타입을 설정한다. string fileExtension = Path.GetExtension(attachment.FileName); string mimeType = MIMETypeHelper.GetMIMEType(fileExtension); #endregion mailMessage.Attachments.Add(new System.Net.Mail.Attachment(attachmentFilePath, mimeType)); } MimeMessage mimeMessage = (MimeMessage)mailMessage; mimeMessage.Date = mailItem.CreationTime; mimeMessage.WriteTo(filePath); mailMessage.Dispose(); } finally { foreach(string path in temporaryDirectoryPathList) { if(Directory.Exists(path)) { Directory.Delete(path, true); } } } } #endregion } } |
▶ CustomAddIn.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
using Microsoft.Office.Core; using System; namespace TestProject { /// <summary> /// 커스텀 애드인 /// </summary> public partial class CustomAddIn { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 리본 확장성 객체 생성하기 - CreateRibbonExtensibilityObject() /// <summary> /// 리본 확장성 객체 생성하기 /// </summary> /// <returns>리본 확장성 인터페이스 객체</returns> protected override IRibbonExtensibility CreateRibbonExtensibilityObject() { return new RibbonContextMenu(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 커스텀 애드인 시작시 처리하기 - CustomAddIn_Startup(sender, e) /// <summary> /// 커스텀 애드인 시작시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void CustomAddIn_Startup(object sender, EventArgs e) { } #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 VSTO에서 생성한 코드 /// <summary> /// 디자이너 지원에 필요한 메서드입니다. /// 이 메서드의 내용을 코드 편집기로 수정하지 마세요. /// </summary> private void InternalStartup() { this.Startup += new EventHandler(CustomAddIn_Startup ); this.Shutdown += new EventHandler(CustomAddIn_Shutdown); } #endregion } } |
TestProject.zip