背景 我们有一个用asp-classic和asp.net编写的内部Intranet系统 - iis服务器没有安装php(在php中看到一些漂亮的解决方案,例如this但是我们真的不能使用这些。) - 我们需要向用户显示他们在imap邮箱中有多少未读电子邮件。他们的imap邮箱内部托管在一个运行postfix和dovecot imap的linux盒子上。
我的问题: 如何通过.net(vb或c#)中的Dovecot Imap连接到Postfix邮件系统并确定未读项目的数量?
我已尝试过的内容: 我们使用了Limilabs mail dll和以下内容:
Imports System.Net.Security
Imports System
Imports Limilabs.Mail
Imports Limilabs.Client.IMAP
Imports Limilabs.Client
Function RetrieveUnread(_server, _user, _password)
Using imap As New Limilabs.Client.IMAP.Imap
'#### Handler needed because of self signed certificates
RetrieveUnread = vbNull
AddHandler imap.ServerCertificateValidate, AddressOf ValidateCertificate
imap.ConnectSSL(_server)
Try
imap.Login(_user, _password)
Catch ex As Exception
RetrieveUnread = "Failed to login"
End Try
If CStr(RetrieveUnread) <> "Failed to login" Then
imap.SelectInbox()
Dim uids As List(Of Long) = imap.Search(Flag.Unseen) 'Find all unseen messages.
'Console.WriteLine("Number of unseen messages is: " & CStr(uids.Count))
RetrieveUnread = CStr(uids.Count)
End If
imap.Close()
End Using
End Function
Private Sub ValidateCertificate(ByVal sender As Object, ByVal e As ServerCertificateValidateEventArgs)
Const ignoredErrors As SslPolicyErrors = SslPolicyErrors.RemoteCertificateChainErrors Or SslPolicyErrors.RemoteCertificateNameMismatch ' name mismatch
Dim nameOnCertificate As String = e.Certificate.Subject
If (e.SslPolicyErrors And Not ignoredErrors) = SslPolicyErrors.None Then
e.IsValid = True
Return
End If
e.IsValid = False
End Sub
然而,虽然这有效,但这种方法依赖于第三方,如果许可证不到位/最新,则DLL将失败。对于我们需要做的事情而言,我们非常臃肿。我们只是好奇,如果这可以在本机.net代码中完成。
答案 0 :(得分:2)
你不需要第三方dll;一个简单的TcpClient
会做。
您可以将正确的IMAP命令发送到服务器,并使用正则表达式解析结果。
LINQPad就绪示例:
Sub Main()
Dim client = New System.Net.Sockets.TcpClient("hostname", 143)
Using s = client.GetStream(),
reader = New StreamReader(s),
writer = New StreamWriter(s)
Dim send As Action(Of String) = Sub(command)
writer.WriteLine(command)
writer.Flush()
End Sub
Dim recieve As Func(Of String, String) = Function(tag)
Dim response As String
Dim str As String = ""
response = reader.ReadLine()
While response IsNot Nothing
str += response
If response.StartsWith(tag, StringComparison.Ordinal) Then
Exit While
End If
response = reader.ReadLine()
End While
Return str
End Function
send("a login username password"): recieve("a")
send("b select inbox"): recieve("b")
send("b2 SEARCH UNSEEN")
Dim b2response = recieve("b2")
Dim items = Regex.Match(b2response, "SEARCH (.*) OK").Groups(1).Value.Split(" "c)
Console.WriteLine("Unread items: " + items.Count().ToString())
send("c logout"): recieve("c")
End Using
End Sub
void Main()
{
var client = new System.Net.Sockets.TcpClient("hostname", 143);
using (var s = client.GetStream())
using (var reader = new StreamReader(s))
using (var writer = new StreamWriter(s))
{
Action<string> send = (command) =>
{
writer.WriteLine(command);
writer.Flush();
};
Func<String, String> recieve = (tag) =>
{
string response;
string str="";
while ((response = reader.ReadLine()) != null )
{
str+=response;
if (response.StartsWith(tag, StringComparison.Ordinal))
break;
}
return str;
};
send("a login username password"); recieve("a");
send("b select inbox"); recieve("b");
send("b2 SEARCH UNSEEN");
var b2response = recieve("b2");
var items = Regex.Match(b2response, @"SEARCH (.*) OK").Groups[1].Value.Split(' ');
Console.WriteLine("Unread items: " + items.Count().ToString());
send("c logout"); recieve("c");
}
}
如果要使用SSL,则必须将流s
包装在SslStream
中并首先调用AuthenticateAsClient
(并且可能使用端口993)。