C# MailSystem.Net中使用字符串的参数太多
我正在尝试使用MailSystem.Net连接到IMAP服务器。在我尝试使用包含不止一个单词的密码(这是使用此代码访问我将要使用它的任何实际非测试电子邮件的要求)之前,一切似乎都正常。此时,我收到登录命令的“参数太多”错误 代码如下:C# MailSystem.Net中使用字符串的参数太多,c#,imap,C#,Imap,我正在尝试使用MailSystem.Net连接到IMAP服务器。在我尝试使用包含不止一个单词的密码(这是使用此代码访问我将要使用它的任何实际非测试电子邮件的要求)之前,一切似乎都正常。此时,我收到登录命令的“参数太多”错误 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using CommandLi
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);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用CommandLine.Utility;
使用ActiveUp.Net.Mail;
使用PT.maileintegration.IMAP;//如果不使用此选项,则可能会被删除
名称空间邮件
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串user=null;
字符串密码=null;
字符串服务器=null;
字符串路径=null;
//这会将args字符串发送到CommdandLine类中,并将任何值转储到哈希表中
Arguments命令行=新参数(args);
//设置用户
if(命令行[“u”]!=null)
{
用户=命令行[“u”];
}
//设置密码
if(命令行[“p”]!=null)
{
密码=命令行[“p”];
}
//设置服务器
if(命令行[“s”]!=null)
{
服务器=命令行[“s”];
}
//设置路径
if(命令行[“d”]!=null)
{
@路径=命令行[“d”];
}
//只要程序有这4个参数,它就只向前移动
if((用户!=null)&
(密码!=null)&
(服务器!=null)&
(路径!=null))
{
Imap4Client=新Imap4Client();
Console.WriteLine(“+user+”“+password+”“+server+”“+path”);
ConnectSsl(服务器,993);
client.Login(user,password);//这是弹出错误的地方
//这张打印出来的照片意味着我们进去了!
Console.WriteLine(“连接成功!”);
邮箱盒=客户端。邮箱[“收件箱”];
Fetch=box.Fetch;
int messagesLeft=box.MessageCount;
int-msgIndex=0;
while(messagesLeft>0)
{
msgIndex++;
messagesLeft--;
Message email=fetch.MessageObject(msgIndex);
//如果电子邮件有附件
如果(email.Attachments.Count>0)
{
foreach(电子邮件中的MimePart附件。附件)
{
email.Attachments.StoreToFolder(@path);
Console.WriteLine(“已下载!”);
}
}
//使用邮件的UID和邮箱中的位置删除邮件
UidDeleteMessage(fetch.Uid(msgIndex),true);
msgIndex--;
}
}
//这意味着用户没有输入预期的参数
其他的
{
Console.WriteLine(“您没有输入预期的数据!\n”);
WriteLine(“格式应为:”);
WriteLine(“MailP.exe-u-p-s-d”);
Console.WriteLine(“\r\n”+”按任意键继续…”);
Console.ReadKey(true);
}
}
}
}
对于输入:
mailP.exe-u“用户名”-密码“这是我的密码”-s
“imap.server.com”-d“c:\downloads”
我得到一个错误:
{“命令\”登录用户名“这是我的密码\”失败:
130312093815354登录时出现错误的意外额外参数\r\n“}
我已尝试在登录调用中将密码字符串强制转换为字符串。我还尝试让登录名成为另一个函数的一部分,该函数接受两个字符串作为参数。我猜可能是登录函数中没有正确解析引号吧?如果是因为我在网上找不到其他有类似问题的人。即使是硬编码密码也会出现同样的问题。我在这里完全不知所措。我希望将整个字符串作为password的参数传入。我知道我错过了一些愚蠢的东西,但我已经把这段代码向后看了一遍,我错过了它。因为密码包含空格,我打赌你只需要用引号括起来就可以了
client.Login(user, "\"" + password + "\"");
先生,你是我翅膀下的风。这绝对解决了问题。非常感谢。