private void MailItem_BeforeAttachmentAdd(Outlook.Attachment Attachment, ref bool Cancel) { //Getting attachment path string filePath = Attachment.GetTemporaryFilePath(); //Convert to Byte arraty to read file data byte[] fileInBytes = System.IO.File.ReadAllBytes(filePath); //Getting the type (extension) of the attachment file string fileExtension = Path.GetExtension(filePath); //File data in Base64 String string fileContent = Convert.ToBase64String(fileInBytes, 0, fileInBytes.Length); //Getting the Attachment's file name without extension string fileDisplayName = Path.GetFileNameWithoutExtension(filePath); //Making the display name unqiue - comes handy when deleting the attachments Attachment.DisplayName = fileDisplayName + Attachment.Index.ToString() + fileExtension; //Adding the files to the attachment list attachmentList.Add(new Attachments() { filename = Attachment.FileName, content = fileContent, type = Path.GetExtension(filePath), disposition = "attachment" }); //Adding the attachments to the Outlook mailItems olAttachments.Add(filePath, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, 1, Attachment.DisplayName); //Replica of Attachments object used when attachment is removed from the list tempAttachmentList.Add(new TempAttachments() { displayName = Attachment.DisplayName, filename = Attachment.FileName, content = fileContent, type = fileExtension, disposition = "attachment" }); }
/// <summary> /// 添付ファイルの取得 /// </summary> /// <param name="item">添付されたファイル</param> /// <returns>true:成功、false:失敗</returns> private bool GetAttachments(Outlook.Attachments item) { bool zipError = false; if (m_tempPath == "") { Outlook.Attachment attachment = item[1]; Zip.zipFilePath = ""; string path = attachment.GetTemporaryFilePath(); string[] arr = path.Split('\\'); for (int i = 0; i < arr.Length - 1; i++) { m_tempPath += arr[i] + "\\"; if (i < arr.Length - 2) { Zip.zipFilePath += arr[i] + "\\"; } } } try { foreach (var AttachmentObj in item) { Outlook.Attachment Attachment = (Outlook.Attachment)AttachmentObj; string path = Attachment.PathName; string attachmentFileName = Attachment.FileName; path = Path.Combine(m_tempPath, attachmentFileName); string extension = Path.GetExtension(path); string[] tempfolderList = Directory.GetFiles(m_tempPath); if (!attachmentList.Contains(Attachment)) { attachmentList.Add(Attachment); if (extension != ".zip") { File.Copy(path, Path.Combine(m_copyFolderPath, attachmentFileName), true); m_fileList.Add(new ClsFilePropertyList { attachment = Attachment, fileName = attachmentFileName, filePath = Path.Combine(m_tempPath, attachmentFileName), fileExtension = extension }); } // zipの場合は解凍して中身のファイルを確認する else { m_fileList.Add(new ClsFilePropertyList { attachment = Attachment, fileName = attachmentFileName, filePath = path, fileExtension = extension, file_list = zip.UnZip(path, m_tempPath, m_fileList, ref zipError) }); string sourceFilePath = Path.Combine(m_tempPath, attachmentFileName.Substring(0, attachmentFileName.Length - 4)); string copyDirectoryPath = Path.Combine(m_copyFolderPath, attachmentFileName.Substring(0, attachmentFileName.Length - 4)); File.Copy(path, Path.Combine(m_copyFolderPath, attachmentFileName), true); int count = m_fileList.Count(); } } } // 正常に取得できているか確認用表示部分 string attachmentFile = ""; for (int i = 0; i < m_fileList.Count; i++) { attachmentFile += m_fileList[i].fileName + "\r\n"; } MessageBox.Show(attachmentFile, "添付ファイル名"); } catch (Exception e) { MessageBox.Show(e.ToString()); } return(zipError); }