【问题标题】:Cannot delete an email message using POP3无法使用 POP3 删除电子邮件
【发布时间】:2016-10-11 22:01:34
【问题描述】:

我的代码使用 POP3 访问电子邮件帐户并搜索已发送但地址不存在的邮件。处理它们后,我删除了失败消息。我有一个客户可以获取和处理消息,但无法删除它们。他们不断收到消息Error deleting message 1: -ERR The specified message is out of range.

我的删除方法的代码如下。这适用于大多数客户,而且非常简单,所以我不确定为什么它不起作用。

    public bool Delete(int index)
    {
        bool result = false;
        String response = SendReceive("DELE ", index.ToString());
        if (response.StartsWith("+OK"))
            result = true;
        else
            logger.Log("Error deleting message " + index + ": " + response, Verbose.LogImportant);

        return result;
    }

对于 SendReceive 方法:

    private String SendReceive(String command, String parameter)
    {
        String result = null;
        try
        {
            String myCommand = command.ToUpper().Trim() + " " + parameter.Trim() + Environment.NewLine;
            byte[] data = System.Text.Encoding.ASCII.GetBytes(myCommand.ToCharArray());
            tcpClient.GetStream().Write(data, 0, data.Length);
            result = streamReader.ReadLine();
        }
        catch { }   //  Not logged in...
        return result;
    }

索引是直接从收到的电子邮件中获取的,直到删除方法处理完毕后才会关闭连接。由于必须返回一封电子邮件才能运行此方法,并且由于索引从 1 运行到 n,并且发送了 1,所以我看不出是什么导致了此方法失败。

【问题讨论】:

    标签: c# pop3


    【解决方案1】:

    SendReceive() 调用看起来不对。我的猜测是它可能应该在格式字符串中有一个{0}。换句话说,您的代码可能发送的是DELE 而不是DELE 1

    【讨论】:

    • 感谢@jstedfast 的回复。不包括我的 SendReceive 方法是我的疏忽。
    • 不幸的是,这并没有解决问题。我已经发送了正确的信息。我只是没有包含代码来显示这一点,所以我更新了我的问题。
    • 您确定在此之前您还没有发送过 DELE 1 命令吗?您不能多次删除同一条消息。
    • 如果您使用像MailKit 这样的库,它是否有效?如果是这样,我建议花时间将日志记录添加到您的代码中。从我这里拿走吧,这将是您宝贵的时间投资。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2022-09-23
    • 2013-04-26
    • 1970-01-01
    • 2017-05-20
    • 2019-12-06
    相关资源
    最近更新 更多