我正在使用this project中的代码编写自己的SMTP服务器。
到目前为止,我有这段代码(只是我上面链接的项目的片段):
TcpListener listener = new TcpListener(IPAddress.Any, nListeningPort);
listener.Start();
TcpClient client = listener.AcceptTcpClient();
client.ReceiveTimeout = 5000;
NetworkStream stream = client.GetStream();
//How do I authenticate incoming SMTP connection?
//In other words, how do I check user name and password?
//Begin reading stream
System.IO.StreamReader reader;
System.IO.StreamWriter writer;
reader = new System.IO.StreamReader(stream);
writer = new System.IO.StreamWriter(stream);
writer.NewLine = "\r\n";
writer.AutoFlush = true;
for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
{
Console.WriteLine("Read line {0}", line);
switch (line)
{
case "DATA":
writer.WriteLine("354 Start input, end data with <CRLF>.<CRLF>");
StringBuilder data = new StringBuilder();
//And so on...
writer.WriteLine("250 OK");
client.Close();
break;
//Other cases...
}
}
它工作正常,但它接受任何SMTP连接。
我的问题是如何对其进行身份验证,或检查正确的使用名称和密码?
编辑:我刚刚意识到添加以下内容非常重要。我需要为我们的Intranet编写此SMTP服务器代码,以便为来自某个特定应用程序的出站电子邮件提供服务(我无法控制源代码。)
该应用程序显然使用Redemption.SafeMailItem
对象作为SMTP客户端发送电子邮件。这是我从拆解它得到的伪代码:
//Code below was extracted using dotPeek
Microsoft.Office.Interop.Outlook.Application application = Interaction.CreateObject("Outlook.Application", "");
SafeMailItem safeMailItem = Interaction.CreateObject("Redemption.SafeMailItem", "");
MailItem mailItem = application.CreateItem(OlItemType.olMailItem);
safeMailItem.Item = mailItem;
safeMailItem.Recipients.Add(strRecipient);
safeMailItem.Body = sBody;
safeMailItem.Attachments.Add((object) strPath, (object) Missing.Value, (object) Missing.Value, (object) Missing.Value);
NewLateBinding.LateSet((object) safeMailItem, (Type) null, "Subject", new object[1]
{
(object) sSubject
}, (string[]) null, (Type[]) null);
safeMailItem.Send();
application.GetNamespace("MAPI").SyncObjects[(object) 1].Start();