当我在C#中尝试追加命令时,请参阅代码:
private readonly string n = Environment.NewLine;
public string CreateDraft()
{
string output;
string command = "MIME-Version: 1.0" + n +
"From: username@gmail.com" + n +
"To: username@gmail.com" + n +
"Subject: " + machineId + n +
"Content-Type: text/plain; charset=utf-8" + n +
"TESTING" + n;
output = ReceiveResponse("$$ APPEND [Gmail]/Drafts (\\Draft) {" + Encoding.UTF8.GetByteCount(command) + "}" + n);
output += n + ReceiveResponse(command);
return output;
}
private string ReceiveResponse(string command)
{
try
{
if (InternetIsConnected() == true)
{
if (!(tcpc.Connected))
{
Connect();
}
if (tcpc.Connected)
{
dummy = Encoding.ASCII.GetBytes(command);
ssl.Write(dummy, 0, dummy.Length);
ssl.Flush();
response = "";
do
{
bytes = ssl.Read(buffer, 0, buffer.Length);
Decoder decoder = Encoding.UTF8.GetDecoder();
char[] chars = new char[decoder.GetCharCount(buffer, 0, bytes)];
decoder.GetChars(buffer, 0, bytes, chars, 0);
response += new string(chars);
} while (response.Contains("$$") == false && (response.Contains("+") == false));
return response;
}
return "Failure: Terminal Disconnected";
}
}
catch (Exception ex)
{
}
return "Failure";
}
它说,BAD无法解析命令。它在Encoding.UTF8.GetByteCount(command)
上崩溃,它的长度为162,但是当我尝试160时,它会起作用。 GMAIL只是不同意我的意思,然后停止工作。
答案 0 :(得分:1)
你的命令实际上是错的;你必须发送文字,然后发送CRLF,然后等待服务器告诉你发送更多,然后发送正文。
FooLib.LinkWith.cs
之类的内容有误,应为{3}foo
,等待服务器告诉您继续{3}\r\n
。