在Windows窗体中阅读邮件使用C# [英] Reading Mail in Windows form Using C#
问题描述
这是我的代码:
using System;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Configuration;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Web.UI.HtmlControls;
使用System.IO;
使用System.Net.NetworkInformation;
使用System.Net.Security;
使用System.Net.Sockets;
命名空间emailcheck
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent() ;
}
private void button1_Click(object sender,EventArgs e)
{
try
{
//创建一个TcpClient的实例
TcpClient tcpclient = new TcpClient();
// HOST NAME POP SERVER和gmail使用端口号995作为POP
tcpclient.Connect(pop.mail.yahoo.com,995);
//这是Secure Stream //打开客户端和POP Server之间的连接
System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream()) ;
//验证客户端
sslstream.AuthenticateAsClient(pop.mail.yahoo.com);
// bool flag = sslstream.IsAuthenticated; // check flag
//分配作者流
System.IO.StreamWriter sw = new StreamWriter(sslstream);
//分配读者流
System.IO.StreamReader reader = new StreamReader(sslstream);
//参考POP rfc命令,很少有6-9个命令
sw.WriteLine(user@yahoo.com);
//发送到服务器
sw.Flush(); sw.WriteLine( 密码);
sw.Flush();
// RETR 1将会发回您的第一封电子邮件。它会读取您的第一封电子邮件的内容
sw.WriteLine(RETR 1);
sw.Flush();
//关闭连接
sw.WriteLine(Quit);
sw.Flush(); string str = string.Empty;
string strTemp = string.Empty;
while((strTemp = reader.ReadLine())!= null)
{
//找到。字符在行
if(strTemp ==。)
{
break;
}
if(strTemp.IndexOf( - ERR)!= -1)
{
break;
}
str + = strTemp;
}
richTextBox1.Text = str;
// textBox1.Text =祝贺.. .... !!!你读了你的第一个Gmail邮件;
}
catch(Exception ex)
{
richTextBox1.Text = ex.ToString();
}
}
}}
有谁能告诉我显示消息?还有如何检查其他域邮件,如gmail,hotmail等,
为什么在从流中获取电子邮件之前退出?
服务器在RETR命令之后返回电子邮件的内容。
我运行了你的代码,它寻找经典的登录方案:
sw.WriteLine(USER user@gmail.com); //参考RFC 1939
//发送到服务器
sw.Flush();
strTemp = reader.ReadLine();
sw.WriteLine(PASS password);
sw.Flush();
strTemp = reader.ReadLine();
您现在应该使用ie来确定。 + OK。欢迎。
现在你应该能够弹出消息。
要获取消息内容(从您的代码):
sw.WriteLine(RETR 1);
sw.Flush();
strTemp = reader.ReadLine();
while((strTemp = reader.ReadLine())!= null)
{
//找到。字符在行
if(strTemp ==。)
{
break;
}
if(strTemp.IndexOf( - ERR)!= -1)
{
break;
}
str + = strTemp;
}
完整代码
try
{
//创建一个TcpClient的实例
string str = string.Empty;
string strTemp = string.Empty;
TcpClient tcpclient = new TcpClient();
// HOST NAME POP SERVER和gmail使用端口号995作为POP
tcpclient.Connect(pop.gmail.com,995);
//这是Secure Stream //打开客户端和POP Server之间的连接
System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream()) ;
//验证客户端
sslstream.AuthenticateAsClient(pop.gmail.com);
// bool flag = sslstream.IsAuthenticated; // check flag
//分配作者流
System.IO.StreamWriter sw = new StreamWriter(sslstream);
//分配读者流
System.IO.StreamReader reader = new StreamReader(sslstream);
strTemp = reader.ReadLine();
//参考POP rfc命令,很少有6-9个命令
sw.WriteLine(USER user@gmail.com);
//发送到服务器
sw.Flush();
strTemp = reader.ReadLine();
sw.WriteLine(PASS password);
sw.Flush();
strTemp = reader.ReadLine();
// RETR 1将会发回您的第一封电子邮件。它会读取您的第一封电子邮件的内容
sw.WriteLine(RETR 1);
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();
while((strTemp = reader.ReadLine())!= null)
{
//找到。字符在行
if(strTemp ==。)
{
break;
}
if(strTemp.IndexOf( - ERR)!= -1)
{
break;
}
str + = strTemp;
}
// textBox1.Text =祝贺.. .... !!!你读过你的第一个Gmail邮件;
}
catch(Exception ex)
{
}
I have tried to read my email in windows form . That is it should show my first message in email inbox. First i tried using my yahoo account . Code works fine but its not showing my message . Just am getting this result : " +OK hello from popgate-0.8.0.450444 pop113.plus.mail.bf1.yahoo.com ".
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net.NetworkInformation;
using System.Net.Security;
using System.Net.Sockets;
namespace emailcheck
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
// create an instance of TcpClient
TcpClient tcpclient = new TcpClient();
// HOST NAME POP SERVER and gmail uses port number 995 for POP
tcpclient.Connect("pop.mail.yahoo.com", 995);
// This is Secure Stream // opened the connection between client and POP Server
System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());
// authenticate as client
sslstream.AuthenticateAsClient("pop.mail.yahoo.com");
//bool flag = sslstream.IsAuthenticated; // check flag
// Asssigned the writer to stream
System.IO.StreamWriter sw = new StreamWriter(sslstream);
// Assigned reader to stream
System.IO.StreamReader reader = new StreamReader(sslstream);
// refer POP rfc command, there very few around 6-9 command
sw.WriteLine("user@yahoo.com");
// sent to server
sw.Flush(); sw.WriteLine("password");
sw.Flush();
// RETR 1 will retrive your first email. it will read content of your first email
sw.WriteLine("RETR 1");
sw.Flush();
// close the connection
sw.WriteLine("Quit ");
sw.Flush(); string str = string.Empty;
string strTemp = string.Empty;
while ((strTemp = reader.ReadLine()) != null)
{
// find the . character in line
if (strTemp == ".")
{
break;
}
if (strTemp.IndexOf("-ERR") != -1)
{
break;
}
str += strTemp;
}
richTextBox1.Text = str;
// textBox1.Text ="Congratulation.. ....!!! You read your first gmail email ";
}
catch (Exception ex)
{
richTextBox1.Text = ex.ToString();
}
}
}}
Can anyone tell me how to show the message ? And also how to check other domain mails like gmail,hotmail etc .,
Why you Quit before getting your email from stream? Server returns content of email right after RETR command.
i ran your code, and it looks for classic scheme of logging in:
sw.WriteLine("USER user@gmail.com"); // Refer to RFC 1939
// sent to server
sw.Flush();
strTemp = reader.ReadLine();
sw.WriteLine("PASS password");
sw.Flush();
strTemp = reader.ReadLine();
You should get OK right now with ie. +OK. Welcome. Now you should be able to pop messages.
To get message content (from your code):
sw.WriteLine("RETR 1");
sw.Flush();
strTemp = reader.ReadLine();
while ((strTemp = reader.ReadLine()) != null)
{
// find the . character in line
if (strTemp == ".")
{
break;
}
if (strTemp.IndexOf("-ERR") != -1)
{
break;
}
str += strTemp;
}
complete code
try
{
// create an instance of TcpClient
string str = string.Empty;
string strTemp = string.Empty;
TcpClient tcpclient = new TcpClient();
// HOST NAME POP SERVER and gmail uses port number 995 for POP
tcpclient.Connect("pop.gmail.com", 995);
// This is Secure Stream // opened the connection between client and POP Server
System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());
// authenticate as client
sslstream.AuthenticateAsClient("pop.gmail.com");
//bool flag = sslstream.IsAuthenticated; // check flag
// Asssigned the writer to stream
System.IO.StreamWriter sw = new StreamWriter(sslstream);
// Assigned reader to stream
System.IO.StreamReader reader = new StreamReader(sslstream);
strTemp = reader.ReadLine();
// refer POP rfc command, there very few around 6-9 command
sw.WriteLine("USER user@gmail.com");
// sent to server
sw.Flush();
strTemp = reader.ReadLine();
sw.WriteLine("PASS password");
sw.Flush();
strTemp = reader.ReadLine();
// RETR 1 will retrive your first email. it will read content of your first email
sw.WriteLine("RETR 1");
sw.Flush();
strTemp = reader.ReadLine();
while ((strTemp = reader.ReadLine()) != null)
{
// find the . character in line
if (strTemp == ".")
{
break;
}
if (strTemp.IndexOf("-ERR") != -1)
{
break;
}
str += strTemp;
}
// close the connection
sw.WriteLine("Quit ");
sw.Flush();
while ((strTemp = reader.ReadLine()) != null)
{
// find the . character in line
if (strTemp == ".")
{
break;
}
if (strTemp.IndexOf("-ERR") != -1)
{
break;
}
str += strTemp;
}
// textBox1.Text ="Congratulation.. ....!!! You read your first gmail email ";
}
catch (Exception ex)
{
}
这篇关于在Windows窗体中阅读邮件使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!