├── ewsManage ├── Microsoft.Exchange.WebServices.dll ├── App.config ├── Properties │ └── AssemblyInfo.cs ├── ewsManage.csproj └── Program.cs ├── ewsManage.sln ├── LICENSE └── README.md /ewsManage/Microsoft.Exchange.WebServices.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/3gstudent/ewsManage/HEAD/ewsManage/Microsoft.Exchange.WebServices.dll -------------------------------------------------------------------------------- /ewsManage/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /ewsManage/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | [assembly: AssemblyTitle("ewsManage")] 6 | [assembly: AssemblyDescription("")] 7 | [assembly: AssemblyConfiguration("")] 8 | [assembly: AssemblyCompany("")] 9 | [assembly: AssemblyProduct("ewsManage")] 10 | [assembly: AssemblyCopyright("Copyright © 2018")] 11 | [assembly: AssemblyTrademark("")] 12 | [assembly: AssemblyCulture("")] 13 | 14 | [assembly: ComVisible(false)] 15 | 16 | [assembly: Guid("048a0f9d-082c-47d5-9005-2dc4520cf803")] 17 | 18 | [assembly: AssemblyVersion("1.0.0.0")] 19 | [assembly: AssemblyFileVersion("1.0.0.0")] 20 | -------------------------------------------------------------------------------- /ewsManage.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25420.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ewsManage", "ewsManage\ewsManage.csproj", "{048A0F9D-082C-47D5-9005-2DC4520CF803}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {048A0F9D-082C-47D5-9005-2DC4520CF803}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {048A0F9D-082C-47D5-9005-2DC4520CF803}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {048A0F9D-082C-47D5-9005-2DC4520CF803}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {048A0F9D-082C-47D5-9005-2DC4520CF803}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2018, 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | * Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /ewsManage/ewsManage.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {048A0F9D-082C-47D5-9005-2DC4520CF803} 8 | Exe 9 | Properties 10 | ewsManage 11 | ewsManage 12 | v3.5 13 | 512 14 | true 15 | 16 | 17 | 18 | 19 | AnyCPU 20 | true 21 | full 22 | false 23 | bin\Debug\ 24 | DEBUG;TRACE 25 | prompt 26 | 4 27 | 28 | 29 | AnyCPU 30 | pdbonly 31 | true 32 | bin\Release\ 33 | TRACE 34 | prompt 35 | 4 36 | 37 | 38 | 39 | False 40 | .\Microsoft.Exchange.WebServices.dll 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ewsManage 2 | My exercise of using Exchange Web Service(EWS) 3 | 4 | Author: 3gstudent 5 | 6 | License: BSD 3-Clause 7 | 8 | More details: [Exchange Web Service(EWS)开发指南](https://3gstudent.github.io/Exchange-Web-Service(EWS)%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97) 9 | 10 | --- 11 | 12 | ### Update(2020.06.20): 13 | 14 | Add new Mode: 15 | 16 | - CreateTestMail,create a test mail and save it to the target folder. 17 | - CreateFolderofInbox,create a folder in Inbox. 18 | - CreateHiddenFolderofInbox,create a hidden folder in Inbox. 19 | - ListHiddenFolder,list the hidden folders of the target folder. 20 | 21 | --- 22 | Usage: 23 | 24 | ``` 25 | ews_tool.exe -CerValidation -ExchangeVersion -u -p -ewsPath -Mode - 26 | 27 | ews_tool.exe -CerValidation -ExchangeVersion -u -p -AutodiscoverUrl -Mode - 28 | 29 | ews_tool.exe -CerValidation -ExchangeVersion -use the default credentials -ewsPath -Mode - 30 | ``` 31 | 32 | -CerValidation: 33 | 34 | Yes/No 35 | 36 | -ExchangeVersion: 37 | 38 | Exchange2007_SP1/Exchange2010/Exchange2010_SP1/Exchange2010_SP2/Exchange2013/Exchange2013_SP1 39 | 40 | -ewsPath/-AutodiscoverUrl: 41 | 42 | You should choose one 43 | 44 | -Mode: 45 | 46 | ListMail/ListUnreadMail/ListFolder/ListHiddenFolder -Foler: 47 | Inbox/Drafts/SentItems/DeletedItems/Outbox/JunkEmail 48 | 49 | SaveAttachment/ClearAllAttachment/DeleteMail/ViewMail -Id 50 | You can get the Id by using ListMail/ListUnreadMail 51 | 52 | AddAttachment/DeleteAttachment -Id -AttachmentFile 53 | You need set 2 parameters 54 | 55 | ListMailofFolder/ListUnreadMailofFolder/CreateTestMail -Id 56 | You can get the Id by using ListFolder 57 | 58 | SearchMail -String 59 | search folder:Inbox/Drafts/SentItems/DeletedItems/Outbox/JunkEmail 60 | search location:Subject/Attachment name/MessageBody 61 | 62 | ReadXML -Path 63 | use EWS SOAP to send command 64 | 65 | CreateFolderofInbox/CreateHiddenFolderofInbox -Name 66 | You need set the folder name 67 | 68 | The following functions are currently supported: 69 | 70 | - Support EWS Managed API and EWS SOAP XML message 71 | - Use the default credentials of the logged on user or another user 72 | - Accept all certificates, regardless of why they are invalid(optional) 73 | - List mails at specified locations, including file names in attachments and mail body 74 | (Judging the length of mail content, if more than 100 characters, only the first 100 characters will be displayed.) 75 | - List unread messages at specified locations, including file names in attachments and mail body 76 | (Judging the length of mail content, if more than 100 characters, only the first 100 characters will be displayed.) 77 | - List custom folders in the specified location (traverse all subfolders) 78 | - View all messages under custom files 79 | - View unread messages under custom files 80 | - Save all messages in the specified location (in EML format) 81 | - Save attachments in specified mail (specified ID) 82 | - Add attachments (specified ID) to the specified message 83 | - Delete attachments to specified messages (specified ID) 84 | - Delete all attachments to a specified message 85 | - Search for mail with specified keywords (common location, search title, attachment name and mail body) 86 | - Delete the specified message (specified ID) 87 | - View the specific content of a message (specified ID) 88 | - Send mail (using EWS SOAP) 89 | - Read the XML file and send commands through EWS SOAP 90 | 91 | Location to support queries and operations: 92 | 93 | - Inbox 94 | - Drafts 95 | - Sent Items 96 | - Deleted Items 97 | - Outbox 98 | - Junk Email 99 | 100 | eg. 101 | 102 | ewsManage.exe -CerValidation Yes -ExchangeVersion Exchange2013_SP1 -u test1 -p test123! -ewsPath https://test.com/ews/Exchange.asmx -Mode ListUnreadMail -Folder Inbox 103 | 104 | ewsManage.exe -CerValidation No -ExchangeVersion Exchange2013_SP1 -use the default credentials -AutodiscoverUrl test1@test.com -Mode ListMail -Folder SentItems 105 | 106 | ewsManage.exe -CerValidation No -ExchangeVersion Exchange2013_SP1 -u test1 -p test123! -ewsPath https://test.com/ews/Exchange.asmx -Mode ListFolder -Folder Inbox 107 | 108 | ewsManage.exe -CerValidation No -ExchangeVersion Exchange2013_SP1 -u test1 -p test123! -ewsPath https://test.com/ews/Exchange.asmx -Mode ListMailofFolder -Id AAMaADFlMjRjMdM2LTgxZTUtNGRmZC05ZDQyLTMzNDFlMzBmZWY1NwAzAAAAAAAR9UOK286vT6HjUgukBQGmAQBHzR2O8KNmTcffGwlY0A76AAAAADfqAAA= 109 | 110 | ewsManage.exe -CerValidation No -ExchangeVersion Exchange2013_SP1 -u test1 -p test123! -ewsPath https://test.com/ews/Exchange.asmx -Mode ExportMail -Folder Inbox 111 | 112 | ewsManage.exe -CerValidation No -ExchangeVersion Exchange2013_SP1 -u test1 -p test123! -ewsPath https://test.com/ews/Exchange.asmx -Mode SaveAttachment -Id AAMzADFlMjRjMzM3LTgxZTUzNGRmZC25ZDQyLTMaNDFlMzBwZWY1NwBGAAAAAAAR8UOK236vT6HjUnujBQGmBwBHzR1O8KNmTrjfGwlY0A56AAAAAAEKAABHzR1O8KNmTrjfGzlY2A75AAAAABxFAAA= 113 | 114 | ewsManage.exe -CerValidation No -ExchangeVersion Exchange2013_SP1 -u test1 -p test123! -ewsPath https://test.com/ews/Exchange.asmx -Mode AddAttachment -Id AAMzADFlMjRjMzM3LTgxZTUzNGRmZC25ZDQyLTMaNDFlMzBwZWY1NwBGAAAAAAAR8UOK236vT6HjUnujBQGmBwBHzR1O8KNmTrjfGwlY0A56AAAAAAEKAABHzR1O8KNmTrjfGzlY2A75AAAAABxFAAA= -AttachmentFile 1.txt 115 | 116 | ewsManage.exe -CerValidation No -ExchangeVersion Exchange2013_SP1 -u test1 -p test123! -ewsPath https://test.com/ews/Exchange.asmx -Mode DeleteAttachment -Id AAMzADFlMjRjMzM3LTgxZTUzNGRmZC25ZDQyLTMaNDFlMzBwZWY1NwBGAAAAAAAR8UOK236vT6HjUnujBQGmBwBHzR1O8KNmTrjfGwlY0A56AAAAAAEKAABHzR1O8KNmTrjfGzlY2A75AAAAABxFAAA= -AttachmentFile 1.txt 117 | 118 | ewsManage.exe -CerValidation No -ExchangeVersion Exchange2013_SP1 -u test1 -p test123! -ewsPath https://test.com/ews/Exchange.asmx -Mode ClearAllAttachment -Id AAMzADFlMjRjMzM3LTgxZTUzNGRmZC25ZDQyLTMaNDFlMzBwZWY1NwBGAAAAAAAR8UOK236vT6HjUnujBQGmBwBHzR1O8KNmTrjfGwlY0A56AAAAAAEKAABHzR1O8KNmTrjfGzlY2A75AAAAABxFAAA= 119 | 120 | ewsManage.exe -CerValidation No -ExchangeVersion Exchange2013_SP1 -u test1 -p test123! -ewsPath https://test.com/ews/Exchange.asmx -Mode SearchMail -String vpn 121 | 122 | ewsManage.exe -CerValidation No -ExchangeVersion Exchange2013_SP1 -u test1 -p test123! -ewsPath https://test.com/ews/Exchange.asmx -Mode DeleteMail -Id AAMzADFlMjRjMzM3LTgxZTUzNGRmZC25ZDQyLTMaNDFlMzBwZWY1NwBGAAAAAAAR8UOK236vT6HjUnujBQGmBwBHzR1O8KNmTrjfGwlY0A56AAAAAAEKAABHzR1O8KNmTrjfGzlY2A75AAAAABxFAAA= 123 | 124 | ewsManage.exe -CerValidation No -ExchangeVersion Exchange2013_SP1 -u test1 -p test123! -ewsPath https://test.com/ews/Exchange.asmx -Mode ViewMail -Id AAMzADFlMjRjMzM3LTgxZTUzNGRmZC25ZDQyLTMaNDFlMzBwZWY1NwBGAAAAAAAR8UOK236vT6HjUnujBQGmBwBHzR1O8KNmTrjfGwlY0A56AAAAAAEKAABHzR1O8KNmTrjfGzlY2A75AAAAABxFAAA= 125 | 126 | ewsManage.exe -CerValidation No -ExchangeVersion Exchange2013_SP1 -u test1 -p test123! -ewsPath https://test.com/ews/Exchange.asmx -Mode ReadXML -Path ews.xml 127 | -------------------------------------------------------------------------------- /ewsManage/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.Exchange.WebServices.Data; 3 | using System.Net; 4 | using System.IO; 5 | using System.Text; 6 | 7 | namespace ewsManage 8 | { 9 | class Program 10 | { 11 | public static string UserString; 12 | public static string PwdString; 13 | public static string ewsPathString; 14 | public static string AutodiscoverUrlString; 15 | public static string FolderString; 16 | public static string IdString; 17 | public static string SearchString; 18 | public static void ShowUsage() 19 | { 20 | string Usage = @" 21 | Author: 3gstudent 22 | Usage: 23 | ews_tool.exe -CerValidation -ExchangeVersion -u -p -ewsPath -Mode - 24 | ews_tool.exe -CerValidation -ExchangeVersion -u -p -AutodiscoverUrl -Mode - 25 | ews_tool.exe -CerValidation -ExchangeVersion -use the default credentials -ewsPath -Mode - 26 | -CerValidation: 27 | Yes/No 28 | -ExchangeVersion: 29 | Exchange2007_SP1/Exchange2010/Exchange2010_SP1/Exchange2010_SP2/Exchange2013/Exchange2013_SP1 30 | -ewsPath/-AutodiscoverUrl: 31 | You should choose one 32 | -Mode: 33 | ListMail/ListUnreadMail/ListFolder/ListHiddenFolder -Foler: 34 | Inbox/Drafts/SentItems/DeletedItems/Outbox/JunkEmail 35 | SaveAttachment/ClearAllAttachment/DeleteMail/ViewMail -Id 36 | You can get the Id by using ListMail/ListUnreadMail 37 | AddAttachment/DeleteAttachment -Id -AttachmentFile 38 | You need set 2 parameters 39 | ListMailofFolder/ListUnreadMailofFolder/CreateTestMail -Id 40 | You can get the Id by using ListFolder 41 | SearchMail -String 42 | search folder:Inbox/Drafts/SentItems/DeletedItems/Outbox/JunkEmail 43 | search location:Subject/Attachment name/MessageBody 44 | ReadXML -Path 45 | use EWS SOAP to send command 46 | CreateFolderofInbox/CreateHiddenFolderofInbox -Name 47 | You need set the folder name 48 | 49 | eg. 50 | ewsManage.exe -CerValidation Yes -ExchangeVersion Exchange2013_SP1 -u test1 -p test123! -ewsPath https://test.com/ews/Exchange.asmx -Mode ListUnreadMail -Folder Inbox 51 | ewsManage.exe -CerValidation No -ExchangeVersion Exchange2013_SP1 -use the default credentials -AutodiscoverUrl test1@test.com -Mode ListMail -Folder SentItems 52 | ewsManage.exe -CerValidation No -ExchangeVersion Exchange2013_SP1 -u test1 -p test123! -ewsPath https://test.com/ews/Exchange.asmx -Mode ListFolder -Folder Inbox 53 | ewsManage.exe -CerValidation No -ExchangeVersion Exchange2013_SP1 -u test1 -p test123! -ewsPath https://test.com/ews/Exchange.asmx -Mode ListMailofFolder -Id AAMaADFlMjRjMdM2LTgxZTUtNGRmZC05ZDQyLTMzNDFlMzBmZWY1NwAzAAAAAAAR9UOK286vT6HjUgukBQGmAQBHzR2O8KNmTcffGwlY0A76AAAAADfqAAA= 54 | ewsManage.exe -CerValidation No -ExchangeVersion Exchange2013_SP1 -u test1 -p test123! -ewsPath https://test.com/ews/Exchange.asmx -Mode ReadXML -Path ews.xml 55 | "; 56 | Console.WriteLine(Usage); 57 | } 58 | 59 | static void Main(string[] args) 60 | { 61 | if (args.Length < 14) 62 | { 63 | ShowUsage(); 64 | Environment.Exit(0); 65 | } 66 | ExchangeService service = null; 67 | if ((args[0] != "-CerValidation")) 68 | { 69 | ShowUsage(); 70 | Environment.Exit(0); 71 | } 72 | if ((args[1] == "No")) 73 | { 74 | ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => { return true; }; 75 | } 76 | Console.WriteLine("[+]CerValidation:{0}", args[1]); 77 | 78 | if ((args[2] != "-ExchangeVersion")) 79 | { 80 | ShowUsage(); 81 | Environment.Exit(0); 82 | } 83 | if ((args[3] == "Exchange2007_SP1")) 84 | { 85 | service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); 86 | } 87 | else if ((args[3] == "Exchange2010")) 88 | { 89 | service = new ExchangeService(ExchangeVersion.Exchange2010); 90 | } 91 | else if ((args[3] == "Exchange2010_SP1")) 92 | { 93 | service = new ExchangeService(ExchangeVersion.Exchange2010_SP1); 94 | } 95 | else if ((args[3] == "Exchange2010_SP2")) 96 | { 97 | service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); 98 | } 99 | else if ((args[3] == "Exchange2013")) 100 | { 101 | service = new ExchangeService(ExchangeVersion.Exchange2013); 102 | } 103 | else if ((args[3] == "Exchange2013_SP1")) 104 | { 105 | service = new ExchangeService(ExchangeVersion.Exchange2013_SP1); 106 | } 107 | else 108 | { 109 | ShowUsage(); 110 | Environment.Exit(0); 111 | } 112 | Console.WriteLine("[+]ExchangeVersion:{0}", args[3]); 113 | if (args[4] == "-u") 114 | { 115 | UserString = args[5]; 116 | Console.WriteLine("[+]User:{0}", args[5]); 117 | if ((args[6] != "-p")) 118 | { 119 | ShowUsage(); 120 | Environment.Exit(0); 121 | } 122 | PwdString = args[7]; 123 | Console.WriteLine("[+]Password:{0}", args[7]); 124 | service.Credentials = new WebCredentials(UserString, PwdString); 125 | } 126 | else if (args[4] == "-use") 127 | { 128 | if (args[5] == "the") 129 | { 130 | if (args[6] == "default") 131 | { 132 | if (args[7] == "credentials") 133 | { 134 | service.UseDefaultCredentials = true; 135 | Console.WriteLine("[+]Use the default credentials"); 136 | } 137 | } 138 | } 139 | else 140 | { 141 | ShowUsage(); 142 | Environment.Exit(0); 143 | } 144 | } 145 | else 146 | { 147 | ShowUsage(); 148 | Environment.Exit(0); 149 | } 150 | if ((args[8] == "-ewsPath")) 151 | { 152 | Console.WriteLine("[+]ewsPath:{0}", args[9]); 153 | ewsPathString = args[9]; 154 | try 155 | { 156 | service.Url = new Uri(ewsPathString); 157 | } 158 | catch (Exception e) 159 | { 160 | Console.WriteLine("{0}", e.Message); 161 | Environment.Exit(0); 162 | } 163 | } 164 | else if ((args[8] == "-AutodiscoverUrl")) 165 | { 166 | Console.WriteLine("[+]AutodiscoverUrl:{0}", args[9]); 167 | AutodiscoverUrlString = args[9]; 168 | try 169 | { 170 | service.AutodiscoverUrl(AutodiscoverUrlString, RedirectionUrlValidationCallback); 171 | Console.WriteLine("[+]ewsPath:{0}", service.Url); 172 | } 173 | catch (Exception e) 174 | { 175 | Console.WriteLine("{0}", e.Message); 176 | Environment.Exit(0); 177 | } 178 | } 179 | else 180 | { 181 | ShowUsage(); 182 | Environment.Exit(0); 183 | } 184 | if ((args[10] != "-Mode")) 185 | { 186 | ShowUsage(); 187 | Environment.Exit(0); 188 | } 189 | 190 | if ((args[11] == "ListMail")) 191 | { 192 | Console.WriteLine("[+]Mode:{0}", args[11]); 193 | try 194 | { 195 | ItemView view = new ItemView(int.MaxValue); 196 | PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties); 197 | itempropertyset.RequestedBodyType = BodyType.Text; 198 | view.PropertySet = itempropertyset; 199 | if ((args[12] != "-Folder")) 200 | { 201 | ShowUsage(); 202 | Environment.Exit(0); 203 | } 204 | FindItemsResults findResults = null; 205 | FolderString = args[13]; 206 | Console.WriteLine("[+]Folder:{0}", args[13]); 207 | if (args[13] == "Inbox") 208 | { 209 | findResults = service.FindItems(WellKnownFolderName.Inbox, view); 210 | } 211 | else if (args[13] == "Outbox") 212 | { 213 | findResults = service.FindItems(WellKnownFolderName.Outbox, view); 214 | } 215 | else if (args[13] == "DeletedItems") 216 | { 217 | findResults = service.FindItems(WellKnownFolderName.DeletedItems, view); 218 | } 219 | else if (args[13] == "Drafts") 220 | { 221 | findResults = service.FindItems(WellKnownFolderName.Drafts, view); 222 | } 223 | else if (args[13] == "SentItems") 224 | { 225 | findResults = service.FindItems(WellKnownFolderName.SentItems, view); 226 | } 227 | else if (args[13] == "JunkEmail") 228 | { 229 | findResults = service.FindItems(WellKnownFolderName.JunkEmail, view); 230 | } 231 | else 232 | { 233 | ShowUsage(); 234 | Environment.Exit(0); 235 | } 236 | foreach (Item item in findResults.Items) 237 | { 238 | Console.WriteLine("\r\n"); 239 | if (item.Subject != null) 240 | { 241 | Console.WriteLine("[*]Subject:{0}", item.Subject); 242 | } 243 | else 244 | { 245 | Console.WriteLine("[*]Subject:"); 246 | } 247 | Console.WriteLine("[*]HasAttachments:{0}", item.HasAttachments); 248 | if (item.HasAttachments) 249 | { 250 | EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(ItemSchema.Attachments)); 251 | foreach (Attachment attachment in message.Attachments) 252 | { 253 | FileAttachment fileAttachment = attachment as FileAttachment; 254 | fileAttachment.Load(); 255 | Console.WriteLine(" - Attachments:{0}", fileAttachment.Name); 256 | } 257 | } 258 | Console.WriteLine("[*]ItemId:{0}", item.Id); 259 | Console.WriteLine("[*]DateTimeCreated:{0}", item.DateTimeCreated); 260 | Console.WriteLine("[*]DateTimeReceived:{0}", item.DateTimeReceived); 261 | Console.WriteLine("[*]DateTimeSent:{0}", item.DateTimeSent); 262 | Console.WriteLine("[*]DisplayCc:{0}", item.DisplayCc); 263 | Console.WriteLine("[*]DisplayTo:{0}", item.DisplayTo); 264 | Console.WriteLine("[*]InReplyTo:{0}", item.InReplyTo); 265 | Console.WriteLine("[*]Size:{0}", item.Size); 266 | item.Load(itempropertyset); 267 | if (item.Body.ToString().Length > 100) 268 | { 269 | item.Body = item.Body.ToString().Substring(0, 100); 270 | Console.WriteLine("[*]MessageBody(too big,only output 100):{0}", item.Body); 271 | } 272 | else 273 | { 274 | Console.WriteLine("[*]MessageBody:{0}", item.Body); 275 | } 276 | } 277 | } 278 | catch (Exception e) 279 | { 280 | Console.WriteLine("[!]{0}", e.Message); 281 | Environment.Exit(0); 282 | } 283 | } 284 | 285 | else if ((args[11] == "ListUnreadMail")) 286 | { 287 | Console.WriteLine("[+]Mode:{0}", args[11]); 288 | try 289 | { 290 | SearchFilter sf = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false); 291 | ItemView view = new ItemView(int.MaxValue); 292 | PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties); 293 | itempropertyset.RequestedBodyType = BodyType.Text; 294 | view.PropertySet = itempropertyset; 295 | if ((args[12] != "-Folder")) 296 | { 297 | ShowUsage(); 298 | Environment.Exit(0); 299 | } 300 | FindItemsResults findResults = null; 301 | FolderString = args[13]; 302 | Console.WriteLine("[+]Folder:{0}", args[13]); 303 | if (args[13] == "Inbox") 304 | { 305 | findResults = service.FindItems(WellKnownFolderName.Inbox, sf, view); 306 | } 307 | else if (args[13] == "Outbox") 308 | { 309 | findResults = service.FindItems(WellKnownFolderName.Outbox, sf, view); 310 | } 311 | else if (args[13] == "DeletedItems") 312 | { 313 | findResults = service.FindItems(WellKnownFolderName.DeletedItems, sf, view); 314 | } 315 | else if (args[13] == "Drafts") 316 | { 317 | findResults = service.FindItems(WellKnownFolderName.Drafts, sf, view); 318 | } 319 | else if (args[13] == "SentItems") 320 | { 321 | findResults = service.FindItems(WellKnownFolderName.SentItems, sf, view); 322 | } 323 | else if (args[13] == "JunkEmail") 324 | { 325 | findResults = service.FindItems(WellKnownFolderName.JunkEmail, sf, view); 326 | } 327 | else 328 | { 329 | ShowUsage(); 330 | Environment.Exit(0); 331 | } 332 | foreach (Item item in findResults.Items) 333 | { 334 | Console.WriteLine("\r\n"); 335 | EmailMessage email = EmailMessage.Bind(service, item.Id); 336 | if (email.Subject != null) 337 | { 338 | Console.WriteLine("[*]Subject:{0}", email.Subject); 339 | } 340 | else 341 | { 342 | Console.WriteLine("[*]Subject:"); 343 | } 344 | Console.WriteLine("[*]HasAttachments:{0}", email.HasAttachments); 345 | if (email.HasAttachments) 346 | { 347 | EmailMessage message = EmailMessage.Bind(service, email.Id, new PropertySet(ItemSchema.Attachments)); 348 | foreach (Attachment attachment in message.Attachments) 349 | { 350 | FileAttachment fileAttachment = attachment as FileAttachment; 351 | fileAttachment.Load(); 352 | Console.WriteLine(" - Attachments:{0}", fileAttachment.Name); 353 | } 354 | } 355 | Console.WriteLine("[*]ItemId:{0}", email.Id); 356 | Console.WriteLine("[*]DateTimeCreated:{0}", email.DateTimeCreated); 357 | Console.WriteLine("[*]DateTimeReceived:{0}", email.DateTimeReceived); 358 | Console.WriteLine("[*]DateTimeSent:{0}", email.DateTimeSent); 359 | Console.WriteLine("[*]DisplayCc:{0}", email.DisplayCc); 360 | Console.WriteLine("[*]DisplayTo:{0}", email.DisplayTo); 361 | Console.WriteLine("[*]InReplyTo:{0}", email.InReplyTo); 362 | Console.WriteLine("[*]Size:{0}", item.Size); 363 | email.Load(itempropertyset); 364 | if (email.Body.ToString().Length > 100) 365 | { 366 | email.Body = email.Body.ToString().Substring(0, 100); 367 | Console.WriteLine("[*]MessageBody(too big,only output 100):{0}", email.Body); 368 | } 369 | else 370 | { 371 | Console.WriteLine("[*]MessageBody:{0}", email.Body); 372 | } 373 | } 374 | } 375 | catch (Exception e) 376 | { 377 | Console.WriteLine("[!]{0}", e.Message); 378 | Environment.Exit(0); 379 | } 380 | } 381 | 382 | else if ((args[11] == "ListFolder")) 383 | { 384 | Console.WriteLine("[+]Mode:{0}", args[11]); 385 | try 386 | { 387 | 388 | if ((args[12] != "-Folder")) 389 | { 390 | ShowUsage(); 391 | Environment.Exit(0); 392 | } 393 | FolderString = args[13]; 394 | Console.WriteLine("[+]Folder:{0}", args[13]); 395 | 396 | FindFoldersResults findResults = null; 397 | FolderView view = new FolderView(int.MaxValue) { Traversal = FolderTraversal.Deep }; 398 | 399 | if (args[13] == "Inbox") 400 | { 401 | findResults = service.FindFolders(WellKnownFolderName.Inbox, view); 402 | } 403 | else if (args[13] == "Outbox") 404 | { 405 | findResults = service.FindFolders(WellKnownFolderName.Outbox, view); 406 | } 407 | else if (args[13] == "DeletedItems") 408 | { 409 | findResults = service.FindFolders(WellKnownFolderName.DeletedItems, view); 410 | } 411 | else if (args[13] == "Drafts") 412 | { 413 | findResults = service.FindFolders(WellKnownFolderName.Drafts, view); 414 | } 415 | else if (args[13] == "SentItems") 416 | { 417 | findResults = service.FindFolders(WellKnownFolderName.SentItems, view); 418 | } 419 | else if (args[13] == "JunkEmail") 420 | { 421 | findResults = service.FindFolders(WellKnownFolderName.JunkEmail, view); 422 | } 423 | else 424 | { 425 | ShowUsage(); 426 | Environment.Exit(0); 427 | } 428 | foreach (Folder folder in findResults.Folders) 429 | { 430 | Console.WriteLine("\r\n"); 431 | Console.WriteLine("[*]DisplayName:{0}", folder.DisplayName); 432 | Console.WriteLine("[*]Id:{0}", folder.Id); 433 | } 434 | } 435 | catch (Exception e) 436 | { 437 | Console.WriteLine("[!]{0}", e.Message); 438 | Environment.Exit(0); 439 | } 440 | } 441 | 442 | else if ((args[11] == "ListHiddenFolder")) 443 | { 444 | Console.WriteLine("[+]Mode:{0}", args[11]); 445 | try 446 | { 447 | 448 | if ((args[12] != "-Folder")) 449 | { 450 | ShowUsage(); 451 | Environment.Exit(0); 452 | } 453 | FolderString = args[13]; 454 | Console.WriteLine("[+]Folder:{0}", args[13]); 455 | 456 | ExtendedPropertyDefinition isHiddenProp = new ExtendedPropertyDefinition(0x10f4, MapiPropertyType.Boolean); 457 | FolderView folderView = new FolderView(100); 458 | folderView.PropertySet = new PropertySet(isHiddenProp, FolderSchema.DisplayName); 459 | folderView.Traversal = FolderTraversal.Deep; 460 | FindFoldersResults findFolder = null; 461 | if (args[13] == "Inbox") 462 | { 463 | findFolder = service.FindFolders(WellKnownFolderName.Inbox, new SearchFilter.IsEqualTo(isHiddenProp, true), folderView); 464 | } 465 | else if (args[13] == "Outbox") 466 | { 467 | findFolder = service.FindFolders(WellKnownFolderName.Outbox, new SearchFilter.IsEqualTo(isHiddenProp, true), folderView); 468 | } 469 | else if (args[13] == "DeletedItems") 470 | { 471 | findFolder = service.FindFolders(WellKnownFolderName.DeletedItems, new SearchFilter.IsEqualTo(isHiddenProp, true), folderView); 472 | } 473 | else if (args[13] == "Drafts") 474 | { 475 | findFolder = service.FindFolders(WellKnownFolderName.Drafts, new SearchFilter.IsEqualTo(isHiddenProp, true), folderView); 476 | } 477 | else if (args[13] == "SentItems") 478 | { 479 | findFolder = service.FindFolders(WellKnownFolderName.SentItems, new SearchFilter.IsEqualTo(isHiddenProp, true), folderView); 480 | } 481 | else if (args[13] == "JunkEmail") 482 | { 483 | findFolder = service.FindFolders(WellKnownFolderName.JunkEmail, new SearchFilter.IsEqualTo(isHiddenProp, true), folderView); 484 | } 485 | else 486 | { 487 | ShowUsage(); 488 | Environment.Exit(0); 489 | } 490 | foreach (Folder folder in findFolder) 491 | { 492 | Console.WriteLine("\r\n"); 493 | Console.WriteLine("[*]DisplayName:{0}", folder.DisplayName); 494 | Console.WriteLine("[*]Id:{0}", folder.Id); 495 | } 496 | } 497 | catch (Exception e) 498 | { 499 | Console.WriteLine("[!]{0}", e.Message); 500 | Environment.Exit(0); 501 | } 502 | } 503 | 504 | else if ((args[11] == "CreateTestMail")) 505 | { 506 | Console.WriteLine("[+]Mode:{0}", args[11]); 507 | try 508 | { 509 | 510 | if ((args[12] != "-Id")) 511 | { 512 | ShowUsage(); 513 | Environment.Exit(0); 514 | } 515 | FolderString = args[13]; 516 | Console.WriteLine("[+]FolderId:{0}", args[13]); 517 | 518 | EmailMessage msg = new EmailMessage(service); 519 | msg.Subject = "test mail"; 520 | msg.Save(FolderString); 521 | } 522 | catch (Exception e) 523 | { 524 | Console.WriteLine("[!]{0}", e.Message); 525 | Environment.Exit(0); 526 | } 527 | } 528 | 529 | else if ((args[11] == "CreateFolderofInbox")) 530 | { 531 | Console.WriteLine("[+]Mode:{0}", args[11]); 532 | try 533 | { 534 | 535 | if ((args[12] != "-Name")) 536 | { 537 | ShowUsage(); 538 | Environment.Exit(0); 539 | } 540 | FolderString = args[13]; 541 | Console.WriteLine("[+]Folder Name:{0}", args[13]); 542 | Folder folder = new Folder(service); 543 | folder.DisplayName = FolderString; 544 | folder.Save(WellKnownFolderName.Inbox); 545 | Console.WriteLine("\r\n"); 546 | Console.WriteLine("[+]New FolderId:" + folder.Id); 547 | } 548 | catch (Exception e) 549 | { 550 | Console.WriteLine("[!]{0}", e.Message); 551 | Environment.Exit(0); 552 | } 553 | } 554 | 555 | else if ((args[11] == "CreateHiddenFolderofInbox")) 556 | { 557 | Console.WriteLine("[+]Mode:{0}", args[11]); 558 | try 559 | { 560 | 561 | if ((args[12] != "-Name")) 562 | { 563 | ShowUsage(); 564 | Environment.Exit(0); 565 | } 566 | FolderString = args[13]; 567 | Console.WriteLine("[+]Hidden Folder Name:{0}", args[13]); 568 | Folder folder = new Folder(service); 569 | folder.DisplayName = FolderString; 570 | folder.Save(WellKnownFolderName.Inbox); 571 | Console.WriteLine("\r\n"); 572 | Console.WriteLine("[+]New Hidden FolderId:" + folder.Id); 573 | ExtendedPropertyDefinition isHiddenProp = new ExtendedPropertyDefinition(0x10f4, MapiPropertyType.Boolean); 574 | PropertySet propSet = new PropertySet(isHiddenProp); 575 | Folder folderhidden = Folder.Bind(service, folder.Id, propSet); 576 | folderhidden.SetExtendedProperty(isHiddenProp, true); 577 | folderhidden.Update(); 578 | } 579 | catch (Exception e) 580 | { 581 | Console.WriteLine("[!]{0}", e.Message); 582 | Environment.Exit(0); 583 | } 584 | } 585 | 586 | else if ((args[11] == "ListMailofFolder")) 587 | { 588 | Console.WriteLine("[+]Mode:{0}", args[11]); 589 | try 590 | { 591 | if ((args[12] != "-Id")) 592 | { 593 | ShowUsage(); 594 | Environment.Exit(0); 595 | } 596 | IdString = args[13]; 597 | Console.WriteLine("[+]Id:{0}", args[13]); 598 | 599 | Folder Folders = Folder.Bind(service, IdString); 600 | FindItemsResults findResults = null; 601 | ItemView view = new ItemView(int.MaxValue); 602 | PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties); 603 | itempropertyset.RequestedBodyType = BodyType.Text; 604 | view.PropertySet = itempropertyset; 605 | findResults = Folders.FindItems(view); 606 | foreach (Item item in findResults.Items) 607 | { 608 | Console.WriteLine("\r\n"); 609 | if (item.Subject != null) 610 | { 611 | Console.WriteLine("[*]Subject:{0}", item.Subject); 612 | } 613 | else 614 | { 615 | Console.WriteLine("[*]Subject:"); 616 | } 617 | 618 | Console.WriteLine("[*]HasAttachments:{0}", item.HasAttachments); 619 | if (item.HasAttachments) 620 | { 621 | EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(ItemSchema.Attachments)); 622 | foreach (Attachment attachment in message.Attachments) 623 | { 624 | FileAttachment fileAttachment = attachment as FileAttachment; 625 | fileAttachment.Load(); 626 | Console.WriteLine(" - Attachments:{0}", fileAttachment.Name); 627 | } 628 | } 629 | Console.WriteLine("[*]ItemId:{0}", item.Id); 630 | Console.WriteLine("[*]DateTimeCreated:{0}", item.DateTimeCreated); 631 | Console.WriteLine("[*]DateTimeReceived:{0}", item.DateTimeReceived); 632 | Console.WriteLine("[*]DateTimeSent:{0}", item.DateTimeSent); 633 | Console.WriteLine("[*]DisplayCc:{0}", item.DisplayCc); 634 | Console.WriteLine("[*]DisplayTo:{0}", item.DisplayTo); 635 | Console.WriteLine("[*]InReplyTo:{0}", item.InReplyTo); 636 | Console.WriteLine("[*]Size:{0}", item.Size); 637 | item.Load(itempropertyset); 638 | if (item.Body.ToString().Length > 100) 639 | { 640 | item.Body = item.Body.ToString().Substring(0, 100); 641 | Console.WriteLine("[*]MessageBody(too big,only output 100):{0}", item.Body); 642 | } 643 | else 644 | { 645 | Console.WriteLine("[*]MessageBody:{0}", item.Body); 646 | } 647 | } 648 | } 649 | catch (Exception e) 650 | { 651 | Console.WriteLine("[!]{0}", e.Message); 652 | Environment.Exit(0); 653 | } 654 | } 655 | 656 | else if ((args[11] == "ListUnreadMailofFolder")) 657 | { 658 | Console.WriteLine("[+]Mode:{0}", args[11]); 659 | try 660 | { 661 | if ((args[12] != "-Id")) 662 | { 663 | ShowUsage(); 664 | Environment.Exit(0); 665 | } 666 | IdString = args[13]; 667 | Console.WriteLine("[+]Id:{0}", args[13]); 668 | 669 | SearchFilter sf = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false); 670 | Folder Folders = Folder.Bind(service, IdString); 671 | FindItemsResults findResults = null; 672 | ItemView view = new ItemView(int.MaxValue); 673 | PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties); 674 | itempropertyset.RequestedBodyType = BodyType.Text; 675 | view.PropertySet = itempropertyset; 676 | findResults = Folders.FindItems(sf, view); 677 | foreach (Item item in findResults.Items) 678 | { 679 | Console.WriteLine("\r\n"); 680 | EmailMessage email = EmailMessage.Bind(service, item.Id); 681 | if (email.Subject != null) 682 | { 683 | Console.WriteLine("[*]Subject:{0}", email.Subject); 684 | } 685 | else 686 | { 687 | Console.WriteLine("[*]Subject:"); 688 | } 689 | 690 | Console.WriteLine("[*]HasAttachments:{0}", email.HasAttachments); 691 | if (email.HasAttachments) 692 | { 693 | EmailMessage message = EmailMessage.Bind(service, email.Id, new PropertySet(ItemSchema.Attachments)); 694 | foreach (Attachment attachment in message.Attachments) 695 | { 696 | FileAttachment fileAttachment = attachment as FileAttachment; 697 | fileAttachment.Load(); 698 | Console.WriteLine(" - Attachments:{0}", fileAttachment.Name); 699 | } 700 | } 701 | Console.WriteLine("[*]ItemId:{0}", email.Id); 702 | Console.WriteLine("[*]DateTimeCreated:{0}", email.DateTimeCreated); 703 | Console.WriteLine("[*]DateTimeReceived:{0}", email.DateTimeReceived); 704 | Console.WriteLine("[*]DateTimeSent:{0}", email.DateTimeSent); 705 | Console.WriteLine("[*]DisplayCc:{0}", email.DisplayCc); 706 | Console.WriteLine("[*]DisplayTo:{0}", email.DisplayTo); 707 | Console.WriteLine("[*]InReplyTo:{0}", email.InReplyTo); 708 | Console.WriteLine("[*]Size:{0}", email.Size); 709 | email.Load(itempropertyset); 710 | if (email.Body.ToString().Length > 100) 711 | { 712 | email.Body = email.Body.ToString().Substring(0, 100); 713 | Console.WriteLine("[*]MessageBody(too big,only output 100):{0}", email.Body); 714 | } 715 | else 716 | { 717 | Console.WriteLine("[*]MessageBody:{0}", email.Body); 718 | } 719 | } 720 | } 721 | catch (Exception e) 722 | { 723 | Console.WriteLine("[!]{0}", e.Message); 724 | Environment.Exit(0); 725 | } 726 | } 727 | 728 | else if ((args[11] == "SaveAttachment")) 729 | { 730 | Console.WriteLine("[+]Mode:{0}", args[11]); 731 | try 732 | { 733 | if ((args[12] != "-Id")) 734 | { 735 | ShowUsage(); 736 | Environment.Exit(0); 737 | } 738 | IdString = args[13]; 739 | Console.WriteLine("[+]Id:{0}", args[13]); 740 | EmailMessage message = EmailMessage.Bind(service, IdString, new PropertySet(ItemSchema.Attachments)); 741 | foreach (Attachment attachment in message.Attachments) 742 | { 743 | FileAttachment fileAttachment = attachment as FileAttachment; 744 | Console.WriteLine("[+]Attachments:{0}", fileAttachment.Name); 745 | fileAttachment.Load(fileAttachment.Name); 746 | Console.WriteLine("\r\n[+]SaveAttachment success"); 747 | } 748 | } 749 | catch (Exception e) 750 | { 751 | Console.WriteLine("[!]{0}", e.Message); 752 | Environment.Exit(0); 753 | } 754 | } 755 | 756 | else if ((args[11] == "AddAttachment")) 757 | { 758 | Console.WriteLine("[+]Mode:{0}", args[11]); 759 | try 760 | { 761 | if ((args[12] != "-Id")) 762 | { 763 | ShowUsage(); 764 | Environment.Exit(0); 765 | } 766 | IdString = args[13]; 767 | Console.WriteLine("[+]Id:{0}", args[13]); 768 | if ((args[14] != "-AttachmentFile")) 769 | { 770 | ShowUsage(); 771 | Environment.Exit(0); 772 | } 773 | Console.WriteLine("[+]AttachmentFile:{0}", args[15]); 774 | EmailMessage message = EmailMessage.Bind(service, IdString); 775 | message.Attachments.AddFileAttachment(args[15]); 776 | message.Update(ConflictResolutionMode.AlwaysOverwrite); 777 | Console.WriteLine("\r\n[+]AddAttachment success"); 778 | } 779 | catch (Exception e) 780 | { 781 | Console.WriteLine("[!]{0}", e.Message); 782 | Environment.Exit(0); 783 | } 784 | } 785 | 786 | else if ((args[11] == "ClearAllAttachment")) 787 | { 788 | Console.WriteLine("[+]Mode:{0}", args[11]); 789 | try 790 | { 791 | if ((args[12] != "-Id")) 792 | { 793 | ShowUsage(); 794 | Environment.Exit(0); 795 | } 796 | IdString = args[13]; 797 | Console.WriteLine("[+]Id:{0}", args[13]); 798 | EmailMessage message = EmailMessage.Bind(service, IdString, new PropertySet(ItemSchema.Attachments)); 799 | message.Attachments.Clear(); 800 | message.Update(ConflictResolutionMode.AlwaysOverwrite); 801 | Console.WriteLine("\r\n[+]ClearAllAttachment success"); 802 | } 803 | catch (Exception e) 804 | { 805 | Console.WriteLine("[!]{0}", e.Message); 806 | Environment.Exit(0); 807 | } 808 | } 809 | 810 | else if ((args[11] == "DeleteAttachment")) 811 | { 812 | Console.WriteLine("[+]Mode:{0}", args[11]); 813 | try 814 | { 815 | if ((args[12] != "-Id")) 816 | { 817 | ShowUsage(); 818 | Environment.Exit(0); 819 | } 820 | IdString = args[13]; 821 | Console.WriteLine("[+]Id:{0}", args[13]); 822 | if ((args[14] != "-AttachmentFile")) 823 | { 824 | ShowUsage(); 825 | Environment.Exit(0); 826 | } 827 | Console.WriteLine("[+]AttachmentFile:{0}", args[15]); 828 | EmailMessage message = EmailMessage.Bind(service, IdString, new PropertySet(ItemSchema.Attachments)); 829 | foreach (Attachment attachment in message.Attachments) 830 | { 831 | if (attachment.Name == args[15]) 832 | { 833 | message.Attachments.Remove(attachment); 834 | break; 835 | } 836 | } 837 | message.Update(ConflictResolutionMode.AlwaysOverwrite); 838 | Console.WriteLine("\r\n[+]DeleteAttachment success"); 839 | } 840 | catch (Exception e) 841 | { 842 | Console.WriteLine("[!]{0}", e.Message); 843 | Environment.Exit(0); 844 | } 845 | } 846 | 847 | else if ((args[11] == "DeleteMail")) 848 | { 849 | Console.WriteLine("[+]Mode:{0}", args[11]); 850 | try 851 | { 852 | if ((args[12] != "-Id")) 853 | { 854 | ShowUsage(); 855 | Environment.Exit(0); 856 | } 857 | IdString = args[13]; 858 | Console.WriteLine("[+]Id:{0}", args[13]); 859 | EmailMessage message = EmailMessage.Bind(service, IdString); 860 | message.Delete(DeleteMode.SoftDelete); 861 | Console.WriteLine("\r\n[+]DeleteMail success"); 862 | } 863 | catch (Exception e) 864 | { 865 | Console.WriteLine("[!]{0}", e.Message); 866 | Environment.Exit(0); 867 | } 868 | } 869 | 870 | else if ((args[11] == "SearchMail")) 871 | { 872 | Console.WriteLine("[+]Mode:{0}", args[11]); 873 | try 874 | { 875 | if ((args[12] != "-String")) 876 | { 877 | ShowUsage(); 878 | Environment.Exit(0); 879 | } 880 | SearchString = args[13]; 881 | Console.WriteLine("[+]SearchString:{0}", args[13]); 882 | SearchMail(service, WellKnownFolderName.Inbox, SearchString); 883 | SearchMail(service, WellKnownFolderName.Outbox, SearchString); 884 | SearchMail(service, WellKnownFolderName.DeletedItems, SearchString); 885 | SearchMail(service, WellKnownFolderName.Drafts, SearchString); 886 | SearchMail(service, WellKnownFolderName.SentItems, SearchString); 887 | SearchMail(service, WellKnownFolderName.JunkEmail, SearchString); 888 | Console.WriteLine("\r\n[+]SearchMail done"); 889 | } 890 | catch (Exception e) 891 | { 892 | Console.WriteLine("[!]{0}", e.Message); 893 | Environment.Exit(0); 894 | } 895 | } 896 | 897 | else if ((args[11] == "ExportMail")) 898 | { 899 | Console.WriteLine("[+]Mode:{0}", args[11]); 900 | try 901 | { 902 | if ((args[12] != "-Folder")) 903 | { 904 | ShowUsage(); 905 | Environment.Exit(0); 906 | } 907 | IdString = args[13]; 908 | Console.WriteLine("[+]Folder:{0}", args[13]); 909 | Console.WriteLine("[+]SavePath:{0}.eml", args[13]); 910 | Folder inbox = null; 911 | if (args[13] == "Inbox") 912 | { 913 | inbox = Folder.Bind(service, WellKnownFolderName.Inbox); 914 | } 915 | else if (args[13] == "Outbox") 916 | { 917 | inbox = Folder.Bind(service, WellKnownFolderName.Outbox); 918 | } 919 | else if (args[13] == "DeletedItems") 920 | { 921 | inbox = Folder.Bind(service, WellKnownFolderName.DeletedItems); 922 | } 923 | else if (args[13] == "Drafts") 924 | { 925 | inbox = Folder.Bind(service, WellKnownFolderName.Drafts); 926 | } 927 | else if (args[13] == "SentItems") 928 | { 929 | inbox = Folder.Bind(service, WellKnownFolderName.SentItems); 930 | } 931 | else if (args[13] == "JunkEmail") 932 | { 933 | inbox = Folder.Bind(service, WellKnownFolderName.JunkEmail); 934 | } 935 | else 936 | { 937 | Console.WriteLine("[!]Don't support this folder"); 938 | Environment.Exit(0); 939 | } 940 | ItemView view = new ItemView(int.MaxValue); 941 | view.PropertySet = new PropertySet(BasePropertySet.IdOnly); 942 | FindItemsResults results = inbox.FindItems(view); 943 | int i = 0; 944 | foreach (var item in results) 945 | { 946 | i++; 947 | PropertySet props = new PropertySet(EmailMessageSchema.MimeContent); 948 | var email = EmailMessage.Bind(service, item.Id, props); 949 | string emlFileName = args[13] + ".eml"; 950 | using (FileStream fs = new FileStream(emlFileName, FileMode.Append, FileAccess.Write)) 951 | { 952 | fs.Write(email.MimeContent.Content, 0, email.MimeContent.Content.Length); 953 | } 954 | } 955 | Console.WriteLine("\r\n[+]ExportMail done,total number:{0}", i); 956 | } 957 | catch (Exception e) 958 | { 959 | Console.WriteLine("[!]{0}", e.Message); 960 | Environment.Exit(0); 961 | } 962 | } 963 | 964 | else if ((args[11] == "ViewMail")) 965 | { 966 | Console.WriteLine("[+]Mode:{0}", args[11]); 967 | try 968 | { 969 | if ((args[12] != "-Id")) 970 | { 971 | ShowUsage(); 972 | Environment.Exit(0); 973 | } 974 | IdString = args[13]; 975 | Console.WriteLine("[+]Id:{0}", args[13]); 976 | ViewMail(service, WellKnownFolderName.Inbox, IdString); 977 | ViewMail(service, WellKnownFolderName.Outbox, IdString); 978 | ViewMail(service, WellKnownFolderName.DeletedItems, IdString); 979 | ViewMail(service, WellKnownFolderName.Drafts, IdString); 980 | ViewMail(service, WellKnownFolderName.SentItems, IdString); 981 | ViewMail(service, WellKnownFolderName.JunkEmail, IdString); 982 | Console.WriteLine("\r\n[+]ViewMail done"); 983 | } 984 | catch (Exception e) 985 | { 986 | Console.WriteLine("[!]{0}", e.Message); 987 | Environment.Exit(0); 988 | } 989 | } 990 | 991 | else if ((args[11] == "ReadXML")) 992 | { 993 | Console.WriteLine("[+]Mode:{0}", args[11]); 994 | if ((args[12] != "-Path")) 995 | { 996 | ShowUsage(); 997 | Environment.Exit(0); 998 | } 999 | Console.WriteLine("[+]XML path:{0}", args[13]); 1000 | Console.WriteLine("[+]Out path:out.xml"); 1001 | StreamReader sendData = new StreamReader(args[13], Encoding.Default); 1002 | byte[] sendDataByte = Encoding.UTF8.GetBytes(sendData.ReadToEnd()); 1003 | sendData.Close(); 1004 | try 1005 | { 1006 | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(service.Url); 1007 | request.Method = "POST"; 1008 | request.ContentType = "text/xml"; 1009 | request.ContentLength = sendDataByte.Length; 1010 | request.AllowAutoRedirect = false; 1011 | if (args[4] == "-use") 1012 | { 1013 | request.Credentials = CredentialCache.DefaultCredentials; 1014 | } 1015 | else 1016 | { 1017 | request.Credentials = new NetworkCredential(UserString, PwdString); 1018 | } 1019 | 1020 | Stream requestStream = request.GetRequestStream(); 1021 | requestStream.Write(sendDataByte, 0, sendDataByte.Length); 1022 | requestStream.Close(); 1023 | 1024 | HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 1025 | if (response.StatusCode != HttpStatusCode.OK) 1026 | { 1027 | throw new WebException(response.StatusDescription); 1028 | } 1029 | Stream receiveStream = response.GetResponseStream(); 1030 | StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); 1031 | String receiveString = readStream.ReadToEnd(); 1032 | response.Close(); 1033 | readStream.Close(); 1034 | StreamWriter receiveData = new StreamWriter("out.xml"); 1035 | receiveData.Write(receiveString); 1036 | receiveData.Close(); 1037 | } 1038 | 1039 | catch (WebException e) 1040 | { 1041 | Console.WriteLine("[!]{0}", e.Message); 1042 | Environment.Exit(0); 1043 | } 1044 | Console.WriteLine("\r\n[+]ReadXML done"); 1045 | } 1046 | 1047 | } 1048 | 1049 | private static void ViewMail(ExchangeService service, WellKnownFolderName folder, string IdString) 1050 | { 1051 | ItemView view = new ItemView(int.MaxValue); 1052 | PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties); 1053 | itempropertyset.RequestedBodyType = BodyType.Text; 1054 | view.PropertySet = itempropertyset; 1055 | FindItemsResults findResults = null; 1056 | findResults = service.FindItems(folder, view); 1057 | foreach (Item item in findResults.Items) 1058 | { 1059 | if (item.Id.ToString() == IdString) 1060 | { 1061 | Console.WriteLine("\r\n"); 1062 | Console.WriteLine("[+]Mail found"); 1063 | Console.WriteLine("[*]Folder:{0}", folder); 1064 | if (item.Subject != null) 1065 | { 1066 | Console.WriteLine("[*]Subject:{0}", item.Subject); 1067 | } 1068 | else 1069 | { 1070 | Console.WriteLine("[*]Subject:"); 1071 | } 1072 | Console.WriteLine("[*]HasAttachments:{0}", item.HasAttachments); 1073 | if (item.HasAttachments) 1074 | { 1075 | EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(ItemSchema.Attachments)); 1076 | foreach (Attachment attachment in message.Attachments) 1077 | { 1078 | FileAttachment fileAttachment = attachment as FileAttachment; 1079 | fileAttachment.Load(); 1080 | Console.WriteLine(" - Attachments:{0}", fileAttachment.Name); 1081 | } 1082 | } 1083 | Console.WriteLine("[*]ItemId:{0}", item.Id); 1084 | Console.WriteLine("[*]DateTimeCreated:{0}", item.DateTimeCreated); 1085 | Console.WriteLine("[*]DateTimeReceived:{0}", item.DateTimeReceived); 1086 | Console.WriteLine("[*]DateTimeSent:{0}", item.DateTimeSent); 1087 | Console.WriteLine("[*]DisplayCc:{0}", item.DisplayCc); 1088 | Console.WriteLine("[*]DisplayTo:{0}", item.DisplayTo); 1089 | Console.WriteLine("[*]InReplyTo:{0}", item.InReplyTo); 1090 | Console.WriteLine("[*]Categories:{0}", item.Categories); 1091 | Console.WriteLine("[*]Culture:{0}", item.Culture); 1092 | Console.WriteLine("[*]IsFromMe:{0}", item.IsFromMe); 1093 | Console.WriteLine("[*]ItemClass:{0}", item.ItemClass); 1094 | Console.WriteLine("[*]Size:{0}", item.Size); 1095 | item.Load(itempropertyset); 1096 | Console.WriteLine("[*]MessageBody:{0}", item.Body); 1097 | } 1098 | } 1099 | } 1100 | 1101 | private static void SearchMail(ExchangeService service, WellKnownFolderName folder, string SearchString) 1102 | { 1103 | ItemView view = new ItemView(int.MaxValue); 1104 | PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties); 1105 | itempropertyset.RequestedBodyType = BodyType.Text; 1106 | view.PropertySet = itempropertyset; 1107 | FindItemsResults findResults = null; 1108 | findResults = service.FindItems(folder, view); 1109 | foreach (Item item in findResults.Items) 1110 | { 1111 | if (item.Subject.Contains(SearchString)) 1112 | { 1113 | Console.WriteLine("\r\n"); 1114 | Console.WriteLine("[+]Match mail found,Subject match"); 1115 | Console.WriteLine("[*]Folder:{0}", folder); 1116 | Console.WriteLine("[*]Subject:{0}", item.Subject); 1117 | Console.WriteLine("[*]Size:{0}", item.Size); 1118 | Console.WriteLine("[*]ItemId:{0}", item.Id); 1119 | } 1120 | if (item.HasAttachments) 1121 | { 1122 | EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(ItemSchema.Attachments)); 1123 | foreach (Attachment attachment in message.Attachments) 1124 | { 1125 | FileAttachment fileAttachment = attachment as FileAttachment; 1126 | fileAttachment.Load(); 1127 | if (fileAttachment.Name.Contains(SearchString)) 1128 | { 1129 | Console.WriteLine("\r\n"); 1130 | Console.WriteLine("[+]Match mail found,Attachment match"); 1131 | Console.WriteLine("[*]Folder:{0}", folder); 1132 | Console.WriteLine("[*]Subject:{0}", item.Subject); 1133 | Console.WriteLine(" - Attachments:{0}", fileAttachment.Name); 1134 | Console.WriteLine("[*]Size:{0}", item.Size); 1135 | Console.WriteLine("[*]ItemId:{0}", item.Id); 1136 | } 1137 | } 1138 | } 1139 | item.Load(itempropertyset); 1140 | if (item.Body.Text != null) 1141 | { 1142 | if (item.Body.Text.Contains(SearchString)) 1143 | { 1144 | Console.WriteLine("\r\n"); 1145 | Console.WriteLine("[+]Match mail found,MessageBody match"); 1146 | Console.WriteLine("[*]Folder:{0}", folder); 1147 | Console.WriteLine("[*]Subject:{0}", item.Subject); 1148 | Console.WriteLine("[*]Size:{0}", item.Size); 1149 | Console.WriteLine("[*]ItemId:{0}", item.Id); 1150 | } 1151 | } 1152 | } 1153 | } 1154 | 1155 | private static bool RedirectionUrlValidationCallback(string redirectionUrl) 1156 | { 1157 | bool result = false; 1158 | Uri redirectionUri = new Uri(redirectionUrl); 1159 | if (redirectionUri.Scheme == "https") 1160 | { 1161 | result = true; 1162 | } 1163 | return result; 1164 | } 1165 | } 1166 | } 1167 | --------------------------------------------------------------------------------