求接收邮件的代码,用.NET的自带类库 求接收邮件的代码,用.NET的自带类库 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 System.Net.Mail 命名空间包含用于将电子邮件发送到简单邮件传输协议 (SMTP) 服务器进行传送的类。 MailMessage 类表示邮件的内容。SmtpClient 类将电子邮件传输到您指定用于邮件传送的 SMTP 主机。可以使用 Attachment 类创建邮件附件。 using System;using System.Net;using System.Net.Mail;using System.Net.Mime;using System.Threading;using System.Net.Sockets;using System.IO;using System.Collections;using System.Collections.Generic;using System.Net.Configuration;using System.Configuration; #region 邮件接收类 /// <summary> /// 邮件接收类 /// </summary> public class POP3 { #region Fields string POPServer; string mPOPUserName; string mPOPPass; int mPOPPort; NetworkStream ns; StreamReader sr; #endregion #region Constructors /// <summary> /// POP3 /// </summary> /// <param name="server">POP3服务器名称</param> /// <param name="userName">用户名</param> /// <param name="password">用户密码</param> public POP3(string server, string userName, string password) : this(server, 110, userName, password) { } /// <summary> /// POP3 /// </summary> /// <param name="server">POP3服务器名称</param> /// <param name="port">端口号</param> /// <param name="userName">用户名</param> /// <param name="password">用户密码</param> public POP3(string server, int port, string userName, string password) { POPServer = server; mPOPUserName = userName; mPOPPass = password; mPOPPort = port; } #endregion #region Methods #region Public /// <summary> /// 获得新邮件数量 /// </summary> /// <returns>新邮件数量</returns> 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 { return -1; } } /// <summary> /// 获取新邮件内容 /// </summary> /// <param name="subj">邮件主题</param> /// <returns>新邮件内容</returns> public List<MailMessage> GetNewMessages(string subj) { int newcount; List<MailMessage> newmsgs = new List<MailMessage>(); try { newcount = GetNumberOfNewMessages(); Connect(); for (int n = 1; n < newcount + 1; n++) { List<string> msglines = GetRawMessage(n); string msgsubj = GetMessageSubject(msglines); if (msgsubj.CompareTo(subj) == 0) { MailMessage msg = new MailMessage(); msg.Subject = msgsubj; msg.From = new MailAddress(GetMessageFrom(msglines)); msg.Body = GetMessageBody(msglines); newmsgs.Add(msg); DeleteMessage(n); } } Disconnect(); return newmsgs; } catch (Exception e) { return newmsgs; } } ///接上面 /// <summary> /// 获取新邮件内容 /// </summary> /// <param name="nIndex">新邮件索引</param> /// <returns>新邮件内容</returns> public MailMessage GetNewMessages(int nIndex) { int newcount; MailMessage msg = new MailMessage(); try { newcount = GetNumberOfNewMessages(); Connect(); int n = nIndex + 1; if (n < newcount + 1) { List<string> msglines = GetRawMessage(n); string msgsubj = GetMessageSubject(msglines); msg.Subject = msgsubj; msg.From = new MailAddress(GetMessageFrom(msglines)); msg.Body = GetMessageBody(msglines); } Disconnect(); return msg; } catch { return null; } } #endregion #region Private private bool Connect() { TcpClient sender = new TcpClient(POPServer, mPOPPort); byte[] outbytes; string input; try { ns = sender.GetStream(); sr = new StreamReader(ns); sr.ReadLine(); input = "user " + mPOPUserName + "\r\n"; outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes, 0, outbytes.Length); sr.ReadLine(); input = "pass " + mPOPPass + "\r\n"; outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes, 0, outbytes.Length); sr.ReadLine(); return true; } catch { return false; } } private void Disconnect() { string input = "quit" + "\r\n"; Byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes, 0, outbytes.Length); ns.Close(); } private List<string> 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); List<string> msglines = new List<string>(); do { line = sr.ReadLine(); msglines.Add(line); } while (line != "."); msglines.RemoveAt(msglines.Count - 1); return msglines; } private string GetMessageSubject(List<string> msglines) { string[] tokens; IEnumerator msgenum = msglines.GetEnumerator(); while (msgenum.MoveNext()) { string line = (string)msgenum.Current; if (line.StartsWith("Subject:")) { tokens = line.Split(new Char[] { ' ' }); return tokens[1].Trim(); } } return "None"; } private string GetMessageFrom(List<string> msglines) { string[] tokens; IEnumerator msgenum = msglines.GetEnumerator(); while (msgenum.MoveNext()) { string line = (string)msgenum.Current; if (line.StartsWith("From:")) { tokens = line.Split(new Char[] { '<' }); return tokens[1].Trim(new Char[] { '<', '>' }); } } return "None"; } private string GetMessageBody(List<string> msglines) { string body = ""; string line = " "; IEnumerator msgenum = msglines.GetEnumerator(); while (line.CompareTo("") != 0) { msgenum.MoveNext(); line = (string)msgenum.Current; } while (msgenum.MoveNext()) { body = body + (string)msgenum.Current + "\r\n"; } return body; } private void DeleteMessage(int messagenumber) { Byte[] outbytes; string input; try { input = "dele " + messagenumber.ToString() + "\r\n"; outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes, 0, outbytes.Length); } catch (Exception e) { return; } } #endregion #endregion } #endregion 可以参考一下OpenPOP,感觉做得不错,开源的 字符串转换为超链接? 圣诞快乐!散分! C#怎么写示例代码,简单问题 installshield如何自动打包制作安装程序 同过.NET的前台界面来管理MS SQL的数据库(数据库备份,恢复)可以调用SQL 里的存储过程吗? 在C#中调用dataset中的table[0]时老是提示找不到table[0]怎么办? ?? Window.Forms在ComboBox中手动加入数据行 我刚学C#,第一个程序Welcome就不能成功的显示,出现了下面的错误,希望各位大侠进来帮忙谢谢! 菜鸟求问~~ 〖编程擂台〗用C#去掉SQL语句中的注释 datatable.select的问题
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.Net.Sockets;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Net.Configuration;
using System.Configuration;
#region 邮件接收类 /// <summary>
/// 邮件接收类
/// </summary>
public class POP3
{
#region Fields string POPServer;
string mPOPUserName;
string mPOPPass;
int mPOPPort;
NetworkStream ns;
StreamReader sr; #endregion #region Constructors /// <summary>
/// POP3
/// </summary>
/// <param name="server">POP3服务器名称</param>
/// <param name="userName">用户名</param>
/// <param name="password">用户密码</param>
public POP3(string server, string userName, string password)
: this(server, 110, userName, password)
{
} /// <summary>
/// POP3
/// </summary>
/// <param name="server">POP3服务器名称</param>
/// <param name="port">端口号</param>
/// <param name="userName">用户名</param>
/// <param name="password">用户密码</param>
public POP3(string server, int port, string userName, string password)
{
POPServer = server;
mPOPUserName = userName;
mPOPPass = password;
mPOPPort = port;
} #endregion #region Methods #region Public /// <summary>
/// 获得新邮件数量
/// </summary>
/// <returns>新邮件数量</returns>
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
{
return -1;
}
} /// <summary>
/// 获取新邮件内容
/// </summary>
/// <param name="subj">邮件主题</param>
/// <returns>新邮件内容</returns>
public List<MailMessage> GetNewMessages(string subj)
{ int newcount;
List<MailMessage> newmsgs = new List<MailMessage>(); try
{
newcount = GetNumberOfNewMessages();
Connect(); for (int n = 1; n < newcount + 1; n++)
{
List<string> msglines = GetRawMessage(n);
string msgsubj = GetMessageSubject(msglines);
if (msgsubj.CompareTo(subj) == 0)
{
MailMessage msg = new MailMessage();
msg.Subject = msgsubj;
msg.From = new MailAddress(GetMessageFrom(msglines));
msg.Body = GetMessageBody(msglines);
newmsgs.Add(msg);
DeleteMessage(n);
}
} Disconnect();
return newmsgs;
}
catch (Exception e)
{
return newmsgs;
}
}
/// <summary>
/// 获取新邮件内容
/// </summary>
/// <param name="nIndex">新邮件索引</param>
/// <returns>新邮件内容</returns>
public MailMessage GetNewMessages(int nIndex)
{
int newcount;
MailMessage msg = new MailMessage(); try
{
newcount = GetNumberOfNewMessages();
Connect();
int n = nIndex + 1; if (n < newcount + 1)
{
List<string> msglines = GetRawMessage(n);
string msgsubj = GetMessageSubject(msglines);
msg.Subject = msgsubj;
msg.From = new MailAddress(GetMessageFrom(msglines));
msg.Body = GetMessageBody(msglines);
} Disconnect();
return msg;
}
catch
{
return null;
}
} #endregion #region Private private bool Connect()
{
TcpClient sender = new TcpClient(POPServer, mPOPPort);
byte[] outbytes;
string input; try
{
ns = sender.GetStream();
sr = new StreamReader(ns); sr.ReadLine();
input = "user " + mPOPUserName + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
sr.ReadLine(); input = "pass " + mPOPPass + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
sr.ReadLine();
return true; }
catch
{
return false;
}
} private void Disconnect()
{
string input = "quit" + "\r\n";
Byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
ns.Close();
} private List<string> 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); List<string> msglines = new List<string>();
do
{
line = sr.ReadLine();
msglines.Add(line);
} while (line != ".");
msglines.RemoveAt(msglines.Count - 1); return msglines;
} private string GetMessageSubject(List<string> msglines)
{
string[] tokens;
IEnumerator msgenum = msglines.GetEnumerator();
while (msgenum.MoveNext())
{
string line = (string)msgenum.Current;
if (line.StartsWith("Subject:"))
{
tokens = line.Split(new Char[] { ' ' });
return tokens[1].Trim();
}
}
return "None";
} private string GetMessageFrom(List<string> msglines)
{
string[] tokens;
IEnumerator msgenum = msglines.GetEnumerator();
while (msgenum.MoveNext())
{
string line = (string)msgenum.Current;
if (line.StartsWith("From:"))
{
tokens = line.Split(new Char[] { '<' });
return tokens[1].Trim(new Char[] { '<', '>' });
}
}
return "None";
} private string GetMessageBody(List<string> msglines)
{
string body = "";
string line = " ";
IEnumerator msgenum = msglines.GetEnumerator(); while (line.CompareTo("") != 0)
{
msgenum.MoveNext();
line = (string)msgenum.Current;
} while (msgenum.MoveNext())
{
body = body + (string)msgenum.Current + "\r\n";
}
return body;
} private void DeleteMessage(int messagenumber)
{
Byte[] outbytes;
string input; try
{
input = "dele " + messagenumber.ToString() + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
}
catch (Exception e)
{
return;
} } #endregion #endregion
} #endregion