Archive email in Gmail

First remember to enable IMAP in Gmail.

To archive message in Gmail you need to use DeleteMessageByUID method. Using INBOX folder is crucial for Archiving to work. If you use DeleteMessageByUID when other folder/label is selected, Gmail will not archive the message, but rather remove currently selected label.

After this operation messages will be still available in “All Mail” IMAP folder.

// C#

using(Imap imap = new Imap())
{
	imap.ConnectSSL("imap.gmail.com");
	imap.Login("user", "password");

	// Find all emails we want to delete
	imap.SelectInbox();
	List<long> uids = imap.Search(
		Expression.Subject("email to archive"));

	imap.DeleteMessageByUID(uids);

	imap.Close();
}
' VB.NET

Using imap As New Imap()
	imap.ConnectSSL("imap.gmail.com")
	imap.Login("user@gmail.com", "password")

	' Find all emails we want to delete
	imap.SelectInbox()
	Dim uids As List(Of Long) = imap.Search(_
		Expression.Subject("email to archive"))

	imap.DeleteMessageByUID(uids)

	imap.Close()
End Using

Tags:     

Questions?

Consider using our Q&A forum for asking questions.