Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to correctly download attachment #29

Open
GamerClassN7 opened this issue May 3, 2022 · 7 comments
Open

how to correctly download attachment #29

GamerClassN7 opened this issue May 3, 2022 · 7 comments
Labels
question Further information is requested

Comments

@GamerClassN7
Copy link

GamerClassN7 commented May 3, 2022

I am unable to download attachment, and i was not able to found any examples reguarding this topic can anybody help ?

@PrzemyslawKlys
Copy link
Member

Try to be more specific - what you have, what doesn't work, what you are doing, which protocol.

@GamerClassN7
Copy link
Author

Hello,
i manage toresolve it on my own but i struggle with eider mark emails as read or move them to folder
my code:

image

no error but folder is not moved, i checket taht i am setting ReadWrite

i am unable to do copy paste from isolated enviroment before you ask why screenshot :D

@PrzemyslawKlys
Copy link
Member

According to docs

The way to mark messages as read using the IMAP protocol is to set the \Seen flag on the message(s).

To do this using MailKit, you will first need to know either the index(es) or the UID(s) of the messages that you would like to set the \Seen flag on. Once you have that information, you will want to call one of the AddFlags() methods on the ImapFolder. For example:

folder.AddFlags (uids, MessageFlags.Seen, true);

To mark messages as unread, you would remove the \Seen flag, like so:

folder.RemoveFlags (uids, MessageFlags.Seen, true);

@PrzemyslawKlys
Copy link
Member

As for your move problem:

If you look at the source code for the MoveTo() method, what you'll notice is that there are several code paths depending on the features that the IMAP server supports.

If the IMAP server supports the MOVE extension, then MailKit's MoveTo() method will use the MOVE command. I suspect that your server does not support the MOVE command or you probably wouldn't be seeing what you are seeing.

When the IMAP server does not support the MOVE command, MailKit has to use the COPY command to copy the message(s) to the destination folder. Once the COPY command has completed, it will then mark the messages that you asked it to move for deletion by setting the \Deleted flag on those messages.

If the server supports the UIDPLUS extension, then MailKit will attempt to EXPUNGE the subset of messages that it just marked for deletion, however, if the UIDPLUS extension is not supported by the IMAP server, then it cannot safely expunge just that subset of messages and so it simply stops there.

My guess is that your server supports neither MOVE nor UIDPLUS and that is why Outlook continues to see the messages in your folder. I believe, however, that Outlook has a setting to show deleted messages with a strikeout (which you probably have disabled).

So to answer your question more succinctly: After calling folder.MoveTo (...);, if you are confident that the messages marked for deletion should be expunged, call folder.Expunge ();

FWIW, if you follow the directions in the FAQ for getting protocol logs, it should show you in more detail what I'm talking about and will confirm (or deny) my explanation.

@buckyj
Copy link

buckyj commented May 6, 2022

Hello, I have a problem with these piece of the code:

foreach ($email in $Client.data.Inbox){

$resolvedFolder = $Client.data.GetFolder("Junk")
$Client.Data.Inbox.MoveTo($email,$resolvedFolder)

}

I get an error: Cannot find an overload for "MoveTo" and the argument count: "2".
Is here supposed to be uid of the email? I don't know how to get this uid of this specific email..

I have seen an example like this with 0 instead of the $email and tried but I didn't move the correct email :(
$Client.Data.Inbox.MoveTo(0, $TestFolder)

How did you solve this issue?

@PrzemyslawKlys
Copy link
Member

Type $Client.Data.Inbox.MoveTo without any brackets - it will show you what options and what it expects. Then verify what $email variablee and $resolvedfolder variable are showing and what type those are?

From the docs:

uids
Type: [System.Collections.Generic.IList](https://docs.microsoft.com/dotnet/api/system.collections.generic.ilist-1)<UniqueId>
The UIDs of the messages to move.
destination
Type: [MailKit.IMailFolder](http://www.mimekit.net/docs/html/T_MailKit_IMailFolder.htm)
The destination folder.
cancellationToken (Optional)
Type: [System.Threading.CancellationToken](https://docs.microsoft.com/dotnet/api/system.threading.cancellationtoken)
The cancellation token.
C#
[Copy](http://www.mimekit.net/docs/html/M_MailKit_Net_Imap_ImapFolder_MoveTo_1.htm#)
public override void MoveTo(
	IList<int> indexes,
	IMailFolder destination,
	CancellationToken cancellationToken = default
)
Parameters
indexes
Type: [System.Collections.Generic.IList](https://docs.microsoft.com/dotnet/api/system.collections.generic.ilist-1)<Int32>
The indexes of the messages to move.
destination
Type: [MailKit.IMailFolder](http://www.mimekit.net/docs/html/T_MailKit_IMailFolder.htm)
The destination folder.
cancellationToken (Optional)
Type: [System.Threading.CancellationToken](https://docs.microsoft.com/dotnet/api/system.threading.cancellationtoken)
The cancellation token

You can see that the expectation is either List of INTs (indexes) or list of UIDs in specific format.

You probably just need to provide it in proper way

@buckyj
Copy link

buckyj commented May 9, 2022

Thank you for you answer. I have turned the code around like this:

#get all emails from inbox
$inbox = $Client.data.Inbox.Search("All")

#loop through email UID-s and retrieve messages
foreach ($email in $inbox.UniqueIds){
$message = $Client.data.Inbox.GetMessage($email.id)
$Client.Data.Inbox.MoveTo($email.Id,$resolvedFolder)
}

I do get an error where ONE unique email gets like 30 unique UID-s and when I try running GetMessage with these ID-s I get an error like this: Exception calling "GetMessage" with "1" argument(s): "Specified argument was out of the range of valid values. Parameter name: index at MailKit.Net.Imap.ImapFolder.d__226.MoveNext()

The email sender and content is the same for all 30 UID-s, which should only bet one UID.

Also I get an error but it still works when I run this piece of code:

$folderDestination = $Client.data.GetFolder('Junk')

An error occurred while enumerating through a collection: The folder is not currently open...

@PrzemyslawKlys PrzemyslawKlys added the question Further information is requested label Oct 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants