Asked by:
Exchange server - send email,move email to folder,delete email ,Read email

Question
-
User-148788041 posted
Hi,
I have 4 filters likes
- From Address
- Date of email
- Subject
- Operator = less than,equal to ,less than equal to
I do not how to use filter and read email based on date or from address.
Also i need to move specific email to a folder in exchange.
Based on filter i need to delete email
Based on filter i need to move attachment to folder.
Thursday, December 27, 2018 5:41 PM
All replies
-
User-943250815 posted
Try Mailkit https://github.com/jstedfast/MailKit
You can install it through NugetThursday, December 27, 2018 9:45 PM -
User-893317190 posted
Hi Guhananth,
As jzero has said, you could use MailKit,
Below is a simple sample of MailKit, I use gmail and imap as a test.
using (var client = new ImapClient()) { // For demo-purposes, accept all SSL certificates client.ServerCertificateValidationCallback = (s, c, h, en) => true; client.Connect("imap.gmail.com", 993, true); client.Authenticate("ackerlyx@gmail.com", "yourmailpassword"); // The Inbox folder is always available on all IMAP servers... var inbox = client.Inbox; inbox.Open(FolderAccess.ReadWrite); Response.Write("Total messages: "+ inbox.Count+"<br/>"); Response.Write("Recent messages:"+ inbox.Recent+"<br/>");
// get a folder and append the message into the folder var newFolder= client.GetFolder("Social"); newFolder.Append(inbox.GetMessage(1));
// delete the first message client.Inbox.AddFlags(new int[] { 0}, MessageFlags.Deleted,true); for (int i = 0; i < inbox.Count; i++) { var message = inbox.GetMessage(i); //loop through the attachment of the message foreach (var attachment in message.Attachments) {
// save the attachment var fileName = attachment.ContentDisposition?.FileName ?? attachment.ContentType.Name; using (var stream = File.Create(fileName)) { if (attachment is MessagePart) { var rfc822 = (MessagePart)attachment; rfc822.Message.WriteTo(stream); } else { var part = (MimePart)attachment; part.Content.DecodeTo(stream); } } } // get the subject of the message Response.Write( "subject:"+message.Subject+"<br/>");
// get from of the message Response.Write("address:" + message.From + "<br/>"); Response.Write("<br/><br/>"); } client.Disconnect(true); }For move message , you could refer to the link below.
https://github.com/jstedfast/MailKit/issues/338
Best regards,
Ackerly Xu
Friday, December 28, 2018 4:41 AM -
User-148788041 posted
That is for imap4.
I am using Microsoft.exchange.webservices dll.Friday, December 28, 2018 5:07 AM -
User-893317190 posted
Hi Guhananth,
You could refer to the link below to read mail using your dll.
https://blog.kloud.com.au/2016/04/19/programmatically-read-email-from-an-exchange-sever-mailbox/
For more discussion, you could refer to
https://stackoverflow.com/questions/652549/read-ms-exchange-email-in-c-sharp
Best regards,
Ackerly Xu
Friday, December 28, 2018 5:26 AM -
User-943250815 posted
For EWS you can get more info here https://docs.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/dd877037%28v%3dexchg.140%29
I already worked with EWS, all I can tell, there is not so much samples available, it uses a proper dialect, the way to work is not always clear and you need to have in mind it is a WebService.
Friday, December 28, 2018 3:10 PM