日期:2014-05-18  浏览次数:20753 次

C#邮件接收问题!
C#邮件接收问题,要能接收附件,不用jmail一类的外组件,还有就是 解码问题。谁有这方面的源码、资料麻烦发邮件到shen521941@163.com
我都头痛两天了,在线等!

------解决方案--------------------
UP!!
------解决方案--------------------
http://industry.ccidnet.com/art/1155/20040520/797955_1.html
------解决方案--------------------

发送
System.Web.Mail.SmtpMail.SmtpServer="smtp.sohu.com";
System.Web.Mail.MailMessage msg = new MailMessage();
msg.Subject = "主题";
msg.From = "发送方";
msg.To = "接收方";
msg.Body = "内容";
MailAttachment att = new MailAttachment(@"附件路径);
msg.Attachments.Add(att);
System.Web.Mail.SmtpMail.Send(msg);

接收
//先连接到服务器
public void Connect()
{
TcpClient sender = new TcpClient("pop3.sohu.com", 110);
sender.ReceiveBufferSize = 4090*10;
sender.ReceiveTimeout = 60000*10;
sender.SendBufferSize = 4090;
sender.SendTimeout = 60000;

Byte[] outbytes;
string input;

try
{

ns = sender.GetStream();
sr = new StreamReader(ns);
Console.WriteLine(sr.ReadLine() );

input = "user " + 邮箱的用户名+ "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);

Console.WriteLine(sr.ReadLine() );

input = "pass " + 密码+ "\r\n";
outbytes = System.Text.Encoding.GetEncoding(936).GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);

Console.WriteLine(sr.ReadLine() );
Console.WriteLine("连接成功");
}
catch (InvalidOperationException ioe)
{
Console.WriteLine("Could not connect to mail server");
}
}
//有多少封邮件
public int GetNumberOfNewMessages()
{
Byte[] outbytes;
string input;

try
{
Connect();

input = "stat" + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
string resp = sr.ReadLine();

string[] tokens = resp.Split(new Char[] { " " });
Disconnect();

return Convert.ToInt32(tokens[1]);
}
catch (InvalidOperationException ioe)
{
Console.WriteLine("Could not connect to mail server");
return 0;
}
}

//获取邮件 参数代表第几封邮件
public ArrayList GetRawMessage(int messagenumber)
{
Byte[] outbytes;
string input;
string line = "";
input = "retr " + messagenumber.ToString() + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
ArrayList msglines = new ArrayList();
do
{
line = sr.ReadLine();
msglines.Add(line);

} while (line != ".");
msglines.RemoveAt(msglines.Count - 1);
return msglines;
}

//取主题 其他的都同理
private string GetMessageSubject(ArrayList msglines)
{
string[] tokens;
IEnumerator msgenum = msglines.GetEnumerator();
while (msgenum.MoveNext())
{
string line = (string)msgenum.Current;
if (line.StartsWith("Subject:")) //如果取发件人就From:
{
tokens = line.Split(new Char[] { " " });

Console.WriteLine(line); //subject:主题
Console.WriteLine(tokens[1].Trim());//subject

return tokens[1].Trim();
}
}
return "None";