smiley22 / S22.Imap

A free, easy-to-use and well-documented .NET library component for communicating with IMAP servers
http://smiley22.github.com/S22.Imap/Documentation/
MIT License
211 stars 115 forks source link

message DATE problem #146

Open haider001 opened 6 years ago

haider001 commented 6 years ago

I am using s22.imap library from NuGet to move messages from source IMAP server to target IMAP server. Everything is working fine except the library sets new/current DATETIME on the destination server instead of the date at the source IMAP.

e.g. message date is 18-Jun-2018 and when I move message on 20th of June then it will set the date as 20-Jun-2018 instead of 18-Jun-2018.

Here is the code.

MailMessage message = sourceIMAP.GetMessage(uid, FetchOptions.Normal, false, folderName); var isSeen = seenUids.Contains(uid); targetIMAP.StoreMessage(message, isSeen, folderName);

mmoo9154 commented 5 years ago

@haider001, the GetMessage() method does not support setting the date/time of the message. This would probably be a great feature request. Even better, you might code up the change and issue a pull request, especially after you see the solution. ;-)

Until someone implements the feature you need, you will need to use the existing client methods to generate your own IMAP APPEND command. Luckiliy, it's pretty simple.

The format of the command is defined in RFC 3501. Here's a link to the page: https://tools.ietf.org/html/rfc3501#page-46

You can look at the implementation of ImapClient.StoreMessage() as a template for your new and improved StoreMessage(). Here is a link to the existing implementation: https://github.com/smiley22/S22.Imap/blob/master/ImapClient.cs#L1270

Notice that smiley22 calls client.SendCommandMessage() at line 1303 with the following string:

tag + "APPEND " + Util.UTF7Encode(mailbox).QuoteString() + (seen ? @" (\Seen)" : "") + " {" + mime822.Length + "}"

It may be a little hard to see, but this string maps to the parts of the APPEND command documented in RFC 3501. All you need to do is insert a valid date/time string in between the "flag parenthesized list" and the curly bracketed MIME length:

string response = targetIMAP.SendCommandGetResponse(tag + "APPEND " +
        Util.UTF7Encode(foldername).QuoteString() +
        (isSeen ? @" (\Seen)" : "") +
        timestamp +
        " {" + mime822.Length + "}");

Then, all you need to do is generate a valid timestamp from your message.Date() and you are done. (Of course, you'll need the rest of the code from the implementation from StoreMessage(), but I'm assuming you can do that.) If it's not clear what you need to do, post a comment.

Now that you've got your new fangled, custom StoreMessage() working, you should see how you can update the existing implementation to add an optional DateTime? timestamp = null parameter to the method so anyone can just call it with message.Date() if they want to preserve the message's timestamp.