this is what I have so far, the thing is with this code i can download all of the attachments found in the message, but what i really need is to download two specific file extension attachment (.PDF and .XML, IE), any tip will be very grateful:
Ews Managed Api Download Pdf
Written in 100% managed code, MailBee.NET EWS only requires the .NET framework be installed on the computer. It has dependency on EWS Managed API library from Microsoft, this .DLL is installed with the product or can be obtained via NuGet.
If you have challenges to write new code, SQLTreeo can help you writing code for you!\r\n\r\n\r\n\r\nC# Source Code including code to save email attachments\r\nC# Source Code including code to save email attachments including (1 year Support + minor + major updates)\r\n\r\n\r\n$49 one time purchase\r\n$98 one time purchase\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\nSituation\r\nYou have an Office 365 mailbox. You want to use C# to read those mails and save them to SQLServer.\r\nRequirements\r\n\r\nOffice365 mailbox\r\nSQLServer\r\nExchange WebServices\r\nC# (visual studio)\r\n\r\nExchange WebServices\r\nApplies to: EWS Managed API Exchange Online Exchange Server 2013 Office 365 There is 1 step you need to take and that is to install Exchange Webservices (MIT License). Download EWS Managed API via nuget. This step is very easy and does not require more then download\/install via a nuget package. This only has to be done on your Development Machine. Once you want to release your product for production release all you need to include is the Microsoft.Exchange.WebServices.dll and you're all set. \r\nImplemented Solution\r\nusing System;\r\nusing Microsoft.Exchange.WebServices.Data;\r\nusing System.Data.SqlClient;\r\n\r\nnamespace ReadOffice365Mailbox\r\n\r\n class Program\r\n \r\n static void Main(string[] args)\r\n \r\n ExchangeService _service;\r\n\r\n try\r\n \r\n Console.WriteLine(\"Registering Exchange connection\");\r\n\r\n _service = new ExchangeService\r\n \r\n Credentials = new WebCredentials(\"yourmailbox@email.com\", \"*******\")\r\n ;\r\n \r\n catch\r\n \r\n Console.WriteLine(\"new ExchangeService failed. Press enter to exit:\");\r\n return;\r\n \r\n\r\n \/\/ This is the office365 webservice URL\r\n _service.Url = new Uri(\"https:\/\/outlook.office365.com\/EWS\/Exchange.asmx\");\r\n\r\n \/\/ Prepare seperate class for writing email to the database\r\n try\r\n \r\n Write2DB db = new Write2DB();\r\n\r\n Console.WriteLine(\"Reading mail\");\r\n\r\n \/\/ Read 100 mails\r\n foreach (EmailMessage email in _service.FindItems(WellKnownFolderName.Inbox, new ItemView(100)))\r\n \r\n db.Save(email);\r\n\r\n \r\n\r\n Console.WriteLine(\"Exiting\");\r\n \r\n catch (Exception e)\r\n \r\n Console.WriteLine(\"An error has occured. \\n:\" + e.Message);\r\n \r\n \r\n \r\n\r\n\r\n \r\n...\r\nusing Microsoft.Exchange.WebServices.Data;\r\n...\r\n\r\nnamespace ReadOffice365Mailbox\r\n{\r\n class Program\r\n {\r\n static void Main(string[] args)\r\n \r\n ExchangeService _service;\r\n _service = new ExchangeService\r\n \r\n Credentials = new WebCredentials(\"yourmailbox@email.com\", \"*******\")\r\n ;\r\n...\r\n\r\nInstantiate a new ExchangeService and provide your mailbox credentials.\r\n \/\/ This is the official office365 webservice URL\r\n _service.Url = new Uri(\"https:\/\/outlook.office365.com\/EWS\/Exchange.asmx\");\r\n\r\n \/\/ Read 100 mails\r\n foreach (EmailMessage email in _service.FindItems(WellKnownFolderName.Inbox, new ItemView(100)))\r\n\r\n...\r\nThen, because you want to call an Webservice (ExchangeWebService EWS) you provide the webservice URL and you call the function FindItems( , ). This call will connect to your mailbox (if you've provided the correct credentials) and start reading (in this case 100 mail headers) After that you can read all the basic email properties like subject, from and to. Microsoft has implemented a lightweight EWS so that if you want to read more properties like Htmlbody or TextBody, you have to send an instruction to load that extra information with the following command:\r\n \r\n \/\/ Read 100 mails\r\n foreach (EmailMessage email in _service.FindItems(WellKnownFolderName.Inbox, new ItemView(100)))\r\n \r\n email.Load(new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.TextBody));\r\n\r\n \/\/ Then you can retrieve extra information like this:\r\n string recipients = \"\";\r\n\r\n foreach (EmailAddress emailAddress in email.CcRecipients)\r\n \r\n recipients += \";\" + emailAddress.Address.ToString();\r\n \r\n cmd.Parameters.AddWithValue(\"@message_id\", email.InternetMessageId);\r\n cmd.Parameters.AddWithValue(\"@from\", email.From.Address);\r\n cmd.Parameters.AddWithValue(\"@body\", email.TextBody.ToString());\r\n cmd.Parameters.AddWithValue(\"@cc\", recipients);\r\n cmd.Parameters.AddWithValue(\"@subject\", email.Subject);\r\n cmd.Parameters.AddWithValue(\"@received_time\", email.DateTimeReceived.ToUniversalTime().ToString());\r\n\r\n recipients = \"\";\r\n foreach (EmailAddress emailAddress in email.ToRecipients)\r\n \r\n recipients += \";\" + emailAddress.Address.ToString();\r\n \r\n cmd.Parameters.AddWithValue(\"@to\", recipients);\r\n \r\n...\r\nSave your emails to the database\r\n \r\nclass Write2DB\r\n \r\n SqlConnection conn = null;\r\n\r\n public Write2DB()\r\n \r\n Console.WriteLine(\"Connecting to SQL Server\");\r\n try\r\n \r\n conn = new SqlConnection(\"Server=*******;DataBase=***********;uid=******;pwd=**********\");\r\n conn.Open();\r\n Console.WriteLine(\"Connected\");\r\n \r\n catch (System.Data.SqlClient.SqlException e)\r\n \r\n throw (e);\r\n \r\n \r\n\r\n public void Save(EmailMessage email)\r\n \r\n email.Load(new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.TextBody));\r\n\r\n SqlCommand cmd = new SqlCommand(\"dbo.usp_servicedesk_savemail\", conn)\r\n \r\n CommandType = System.Data.CommandType.StoredProcedure,\r\n CommandTimeout = 1500\r\n\r\n ;\r\n\r\n string recipients = \"\";\r\n\r\n foreach (EmailAddress emailAddress in email.CcRecipients)\r\n \r\n recipients += \";\" + emailAddress.Address.ToString();\r\n \r\n cmd.Parameters.AddWithValue(\"@message_id\", email.InternetMessageId);\r\n cmd.Parameters.AddWithValue(\"@from\", email.From.Address);\r\n cmd.Parameters.AddWithValue(\"@body\", email.TextBody.ToString());\r\n cmd.Parameters.AddWithValue(\"@cc\", recipients);\r\n cmd.Parameters.AddWithValue(\"@subject\", email.Subject);\r\n cmd.Parameters.AddWithValue(\"@received_time\", email.DateTimeReceived.ToUniversalTime().ToString());\r\n\r\n recipients = \"\";\r\n foreach (EmailAddress emailAddress in email.ToRecipients)\r\n \r\n recipients += \";\" + emailAddress.Address.ToString();\r\n \r\n cmd.Parameters.AddWithValue(\"@to\", recipients);\r\n\r\n \/\/ Execute the procedure\r\n cmd.ExecuteNonQuery();\r\n \r\n Write2DB()\r\n \r\n Console.WriteLine(\"Disconnecting from SQLServer\");\r\n \r\n \r\n\r\nSET ANSI_NULLS ON\r\nGO\r\nSET QUOTED_IDENTIFIER ON\r\nGO\r\nCREATE procedure [dbo].[usp_servicedesk_savemail] @message_id varchar(50),@to varchar(512), @from varchar(256), @cc varchar(256)=null,@subject varchar(256), @body varchar(max), @received_time datetime\r\nas\r\nbegin\r\n\tIF NOT EXISTS( SELECT 1 from mnit_email where param1 = @message_id )\r\n\t\tinsert into mnit_email( created_datetime, type, [from], [to], cc, [subject], body, param1 )\r\n\t\tvalues( @received_time, 'receive_supportmail', @from, @to, @cc, @subject, @body, @message_id)\r\nend\r\nGO\r\n\r\nThe complete visual studio solution & database table and procedure code can be downloaded after purchase.\r\n\r\nAbout us\r\nOver the years, SQLTreeo gained precious know-how which we transformed into customer support, managed database services and database solutions. In order to express our knowledge and experiences, we tend to work with anything less than the best-trained staff and that is the key to success and the biggest asset of our company. Thus, all our database management services are controlled by high level skilled and Senior database administrators. We can help you in every aspect of SQL Server.\r\n ","link":"C# Scripts\/Read emails from exchange","title":"C# - Read emails from exchange online mailbox (Office 365) into SQL Server","created":"2020-04-14T14:45:34+0300","added_by":0,"updated_by":182,"viewed":124660,"docs_operations":["id":980,"page_id":9403,"user_id":87,"action":"edited","created":"2020-04-15T16:21:08+0300","id":1031,"page_id":9403,"user_id":1650,"action":"edited","created":"2020-05-22T11:00:01+0300","id":1032,"page_id":9403,"user_id":1650,"action":"edited","created":"2020-05-22T11:00:28+0300","id":1033,"page_id":9403,"user_id":1650,"action":"edited","created":"2020-05-22T11:01:26+0300","id":1047,"page_id":9403,"user_id":1650,"action":"edited","created":"2020-05-22T11:39:07+0300","id":1104,"page_id":9403,"user_id":182,"action":"edited","created":"2020-08-20T21:30:24+0300","id":1481,"page_id":9403,"user_id":182,"action":"edited","created":"2022-02-05T10:48:13+0200","id":1482,"page_id":9403,"user_id":182,"action":"edited","created":"2022-02-05T10:54:06+0200","id":1485,"page_id":9403,"user_id":182,"action":"edited","created":"2022-02-21T21:51:23+0200","id":1486,"page_id":9403,"user_id":182,"action":"edited","created":"2022-02-21T21:51:49+0200","id":1514,"page_id":9403,"user_id":182,"action":"edited","created":"2022-07-12T07:23:45+0300"],"docs_comments":[]]; var barray = "link":"C# Scripts\/Read emails from exchange"; var pa_id = "Pid":9403; Link Title Content : close Report a Bug In this article
An open source project named EwsEditor [9] (Figure 2) shows how extensive the possibilities are. The project is public, and everyone can download and view the code. The program lets you access the root structure of a mailbox; it not only lets you view elements but also edit them directly. This makes it a very powerful tool and similar to the Microsoft Exchange MAPI Editor (known as MFCMAPI); it is a useful helper for Exchange administrators and developers.
2ff7e9595c
Comments