我可以使用TcpClient
通过以下代码在winform中阅读来自gmail,yahoo和hotmail的新邮件。但我想获取由特定人发送或包含特定主题行的邮件。我搜索了更多文章,但我发现任何工作正常。谁能告诉我怎么做?
public string hotmail(string username, string password)
{
string result = "";
string str = string.Empty;
string strTemp = string.Empty;
try
{
TcpClient tcpclient = new TcpClient();
tcpclient.Connect("pop3.live.com", 995);
System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());
sslstream.AuthenticateAsClient("pop3.live.com");
System.IO.StreamWriter sw = new StreamWriter(sslstream);
System.IO.StreamReader reader = new StreamReader(sslstream);
strTemp = reader.ReadLine();
sw.WriteLine("USER" + " " + username);
sw.Flush();
strTemp = reader.ReadLine();
sw.WriteLine("PASS" + " " + password);
sw.Flush();
strTemp = reader.ReadLine();
string[] numbers = Regex.Split(strTemp, @"\D+");
int a = 0;
foreach (string value in numbers)
{
if (!string.IsNullOrEmpty(value))
{
int i = int.Parse(value);
numbers[a] = i.ToString();
a++;
}
}
sw.WriteLine("RETR" + " " + numbers[0]);
sw.Flush();
strTemp = reader.ReadLine();
while ((strTemp = reader.ReadLine()) != null)
{
if (strTemp == ".")
{
break;
}
if (strTemp.IndexOf("-ERR") != -1)
{
break;
}
str += strTemp;
}
sw.WriteLine("Quit ");
sw.Flush();
result = str;
return result;
}
Catch ( Exception ex)
{}
return result;
}
谢谢你提前..
答案 0 :(得分:1)
POP3不允许过滤要回复的邮件。如果要在下载前过滤消息,则应使用IMAP4协议。
请参阅针对pop3的RFC:http://www.ietf.org/rfc/rfc1939.txt和IMAP4:http://tools.ietf.org/html/rfc3501
答案 1 :(得分:1)
根据@tray建议,您必须检索邮件并根据地址或主题行对其进行过滤,如下所示:
public void check()
{
string sub;
string result,from;
int i = 1;
do
{
ImapClient ic = new ImapClient("imap.mail.yahoo.com", "user@yahoo.com", "password", ImapClient.AuthMethods.Login, 993, true);
ic.SelectMailbox("INBOX");
int n = ic.GetMessageCount();
MailMessage mail = ic.GetMessage(n - i);
ic.Dispose();
sub = mail.Subject;
from = mail.From.ToString();
result = mail.Raw;
i++;
} while (sub != "subject" || from == "person@example.com");
string mailmsg = result;
}
根据您的消息,这需要时间。 欢呼......