在MailSystem.Net中使用String的参数太多

时间:2013-03-12 14:12:14

标签: c# imap

我正在尝试使用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“username”-password“这是我的密码”-s   “imap.server.com”-d“c:\ downloads”

我收到错误:

  

{“Command \”登录用户名这是我的密码\“失败:   130312093815354 BAD LOGIN \ r \ n“}

的意外额外参数

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

1 个答案:

答案 0 :(得分:3)

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

client.Login(user, "\"" + password + "\"");