c# - 如何使用 C# 清空 Gmail 垃圾箱

标签 c# .net imap gmail-imap interimap

我尝试使用 InterImap 访问我的 Gmail 邮箱图书馆。它在那里显示我的消息,但无法删除消息。我还找到了一种方法“EmptyFolder()”,但它不起作用。

所有对我有用的是 MoveMessageToFolder() 但这不是我需要的。

请帮助我使用相同或任何其他库使用 C# 清空我的回收站。我需要执行此操作的代码示例。

这是我设法编写的代码。

var config = new InterIMAP.IMAPConfig("imap.gmail.com", "<my gmail username>", "<my gmail password", true, true, "");
var client = new InterIMAP.Synchronous.IMAPClient(config, new InterIMAP.IMAPLogger(config, new object[] { }), 1);
var trash = client.Folders["[Gmail]"].SubFolders["Trash"];
trash.EmptyFolder();
client.Logoff();

提前致谢。

最佳答案

我不喜欢手动删除 Gmail 中的垃圾文件夹。每天都充斥着垃圾邮件。 所以我复制了 c# 代码来为我完成这项工作。我从评估版 Limilabs 下载并使用了 mail.dll。为应用创建 Gmail 密码。 这是代码:

using System;
using Limilabs.Client.IMAP;
using System.Collections.Generic;

namespace delete_gmail_trash
{
    class Program
    {
        static void Main(string[] args)
        {
            using (Imap imap = new Imap())
            {
                imap.ConnectSSL("imap.gmail.com");
                imap.UseBestLogin("username@gmail.com", "password for Gmail apps");
                // Recognize Trash folder
                List<FolderInfo> folders = imap.GetFolders();

                CommonFolders common = new CommonFolders(folders);

                FolderInfo trash = common.Trash;
                // Find all emails we want to delete
                imap.Select(trash);
                List<long> uidList = imap.Search(Flag.All);
                foreach (long uid in uidList)
                {
                    imap.DeleteMessageByUID(uid);
                    Console.WriteLine("{0} deleted", uid);
                }
                Console.WriteLine("Press any key to exit.");
                Console.ReadKey();
                imap.Close();
            }    
        }
    }
}

关于c# - 如何使用 C# 清空 Gmail 垃圾箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9853838/

相关文章:

c - IMAP协议(protocol)的解释?

php - 如何使用 IMAP 和 php 将邮件附件下载到特定文件夹

c# - 为什么 VB 允许声明静态类型的对象?

c# - 选择 Table.Value == 最大值

c# - 为什么 "Covariance"和 "Contravariance"的概念在实现接口(interface)的方法时适用?

c# - Linq - 获取数组最后一个非零数的索引

c# - 如何以WAL方式打开SQLite连接

c# - Prism Xamarin Forms 选项卡式页面导航

c# - TryExecuteTask(task) 总是阻塞吗?

从 IMAP 请求电子邮件的 PHP 库