3

我正在尝试使用 MailSystem.Net 连接到 IMAP 服务器。一切似乎都很好,直到我尝试使用包含多个单词的密码(使用此代码访问我将使用它的任何实际非测试电子邮件的要求)。那时我收到登录命令的“参数太多”错误。

这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using CommandLine.Utility;
using ActiveUp.Net.Mail;
using PT.MailIntegration.IMAP; // not using this, could be removed



namespace MailP
{
    class Program
    {
        static void Main(string[] args)
        {
            string user = null;
            string password = null;
            string server = null;
            string path = null;

            // this sends the args string into the CommdandLine class and dumps any values into a hashtable
            Arguments CommandLine = new Arguments(args);

            // sets the user
            if (CommandLine["u"] != null)
            {
                user = CommandLine["u"];
            }

            // sets the password
            if (CommandLine["p"] != null)
            {
                password = CommandLine["p"];
            }

            // sets the server
            if (CommandLine["s"] != null)
            {
                server = CommandLine["s"];
            }

            // sets the path
            if (CommandLine["d"] != null)
            {
                @path = CommandLine["d"];
            }

            // program only moves forward as long as it has these 4 arguments
            if ((user != null) &
                (password != null) &
                (server != null) &
                (path != null))
            {
                Imap4Client client = new Imap4Client();
                Console.WriteLine(" " + user + " " + password + " " + server + " " + path);
                client.ConnectSsl(server, 993);
                client.Login(user, password);  //this is where the error pops up
                // this print out means we got in!
                Console.WriteLine("Connection Successful!");

                Mailbox box = client.Mailboxes["inbox"];
                Fetch fetch = box.Fetch;
                int messagesLeft = box.MessageCount;
                int msgIndex = 0;

                while (messagesLeft > 0)
                {
                    msgIndex++;
                    messagesLeft--;
                    Message email = fetch.MessageObject(msgIndex);

                    // if the email has attachments
                    if (email.Attachments.Count > 0)
                    {
                        foreach (MimePart attachment in email.Attachments)
                        {
                            email.Attachments.StoreToFolder(@path);
                            Console.WriteLine("Downloaded!");
                        }
                    }

                    // delete the message using it's UID and position in the mailbox
                    box.UidDeleteMessage(fetch.Uid(msgIndex), true);
                    msgIndex--;
                }

            }
            // this means the user did not enter the expected arguments
            else
            {
                Console.WriteLine("You did not enter the expected data!\n");
                Console.WriteLine("Format should be: ");
                Console.WriteLine("MailP.exe -u <user> -p <password> -s <IMAP SERVER> -d <path>");

                Console.WriteLine("\r\n" + "Press any key to continue...");
                Console.ReadKey(true);

            }
        }
    }
}

对于输入:

mailP.exe -u "用户名" -password "这是我的密码" -s "imap.server.com" -d "c:\downloads"

我得到错误:

{"Command \"login username this is my password \" failed : 130312093815354 BAD Unexpected extra arguments to LOGIN\r\n"}

我尝试在登录调用中将密码字符串转换为字符串。我还尝试让登录成为另一个函数的一部分,该函数接受两个字符串作为参数。我猜在登录功能中可能没有正确解析引号?如果是因为我无法在网上找到另一个有类似问题的人。即使对密码进行硬编码也会产生同样的问题。我在这里完全不知所措。我希望将整个字符串作为密码的参数传入。我知道这一定是我错过了一些愚蠢的事情,但我已经倒过这段代码并且错过了它。

4

1 回答 1

3

由于密码包含空格,我打赌您只需用引号将其括起来。

client.Login(user, "\"" + password + "\"");
于 2013-03-12T14:18:44.620 回答