rss· 投稿· 设为首页· 加入收藏· 繁體版
当前位置: 火魔网 » 程序开发 » C#

c# 接收邮件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading; namespace mail1 class ReceiveMail1 private TcpClient receiveClient = null;
private StreamReader strRe = null;
private StreamWriter strWr = null;
private NetworkStream netStream = null; private string POP3_Server = "pop.sina.com";
private string username = "herolilonghero@sina.com";
private string password = "lilong"; private string response;
private int totalmessage; /// <summary> /// </summary>
/// <returns></returns>
public bool con() receiveClient = new TcpClient(POP3_Server,110);
try netStream = receiveClient.GetStream();
return true; catch (Exception e) Console.WriteLine("连接服务器失败!");
return false; }
/// <summary> /// </summary>
/// <returns></returns>
public bool user_pass() strRe = new StreamReader(netStream,Encoding.Default);
strWr = new StreamWriter(netStream,Encoding.Default);
try response = strRe.ReadLine(); strWr.WriteLine("user "+username);
strWr.Flush();
response = strRe.ReadLine(); strWr.WriteLine("pass "+password);
strWr.Flush();
response = strRe.ReadLine();
if ('-' == response[0]) Console.WriteLine("服务器认证失败!");
return false;
} return true; catch(Exception e) Console.WriteLine("服务器认证失败!");
return false; }
/// <summary> /// </summary>
/// <returns></returns>
public int getMailCount() strWr.WriteLine("stat");
strWr.Flush();
response = strRe.ReadLine();
string[] count = response.Split(' ');
totalmessage = Convert.ToInt32(count[1]);
Console.WriteLine("total = "+totalmessage);
if (totalmessage > 0) return totalmessage;
else return 0;
} public void getMailTop(int i) string str = null; strWr.WriteLine("top "+i+" 0");
strWr.Flush();
str = strRe.ReadLine();
while ("." != str) Console.WriteLine(str);
str = null;
str = strRe.ReadLine(); //读取邮件内容
strWr.WriteLine("retr " + i);
strWr.Flush();
str = strRe.ReadLine();
while ("." != str) Console.WriteLine(str);
str = null;
str = strRe.ReadLine(); } static void Main(string[] args) ReceiveMail1 rm = new ReceiveMail1();
rm.con();
rm.user_pass();
int t = rm.getMailCount();
for (int i = 1; i <= t; i++) Console.WriteLine("\n\n########################\n\n");
rm.getMailTop(i); } }
顶一下
(0)
踩一下
(0)