发送邮件的功能我已经搞定了。现在我要做“收信”的功能,google了一圈,也没搞定。有高人做过吗?下面是发信的代码,已经测试成功了。 public void SendEmail(string to, string subject, string body)
{
MailMessage mail = new MailMessage(); string from = "[email protected]";
string fromName = "Jhtest";
string smtp = "mail.sce.pccu.edu.tw";
string password = "****";
mail.To = to;
mail.From = from;
mail.Subject = subject;
mail.Body = body;
mail.BodyFormat = MailFormat.Html;//设置为HTML格式
mail.Priority = MailPriority.Normal;//一般优先级 SmtpMail.SmtpServer = smtp;
//设置为需要用户验证
mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = "1";
//帐号
mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = from;
//密码
mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = password;
SmtpMail.Send(mail);
}
{
MailMessage mail = new MailMessage(); string from = "[email protected]";
string fromName = "Jhtest";
string smtp = "mail.sce.pccu.edu.tw";
string password = "****";
mail.To = to;
mail.From = from;
mail.Subject = subject;
mail.Body = body;
mail.BodyFormat = MailFormat.Html;//设置为HTML格式
mail.Priority = MailPriority.Normal;//一般优先级 SmtpMail.SmtpServer = smtp;
//设置为需要用户验证
mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = "1";
//帐号
mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = from;
//密码
mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = password;
SmtpMail.Send(mail);
}
using jmail; private void ReciveByJmail()
{
/**/
///建立收邮件对象
jmail.POP3Class popMail = new POP3Class(); /**/
///建立邮件信息接口
jmail.Message mailMessage; /**/
///建立附件集接口
jmail.Attachments atts; /**/
///建立附件接口
jmail.Attachment att; //try
//{
popMail.Connect("[email protected]", "你的密码", "pop.qq.com", 110); /**/
///如果收到邮件
if (0 < popMail.Count)
{
/**/
///根据取到的邮件数量依次取得每封邮件
Response.Write("<table width=900>");
for (int i = 1; i <= popMail.Count; i++)
{
/**/
///取得一条邮件信息
mailMessage = popMail.Messages[i]; /**/
///取得该邮件的附件集合
atts = mailMessage.Attachments; /**/
///设置邮件的编码方式
mailMessage.Charset = "gb2312";
// mailMessage.Charset = "utf-8"; /**/
///设置邮件的附件编码方式
mailMessage.Encoding = "base64"; ///是否将信头编码成iso-8859-1字符集
mailMessage.ISOEncodeHeaders = false; Response.Write("<tr><td>邮件的优先集:" + mailMessage.Priority.ToString() + "</td><td>信箱地址:" + mailMessage.From + "</td><td>发送人:" + mailMessage.FromName + "</td><td>主题:" + mailMessage.Subject + "</td><td>邮件内容:" + mailMessage.BodyText + "</td><td>邮件大小:" + mailMessage.Size.ToString() + "</td><tr>"); ///邮件的优先级
//txtpriority.Text = mailMessage.Priority.ToString(); ///邮件的发送人的信箱地址
// txtSendMail.Text = mailMessage.From; ///邮件的发送人
//txtSender.Text = mailMessage.FromName; ///邮件主题
//txtSubject.Text = mailMessage.Subject; ///邮件内容
// txtBody.Text = mailMessage.Body; ///邮件大小
//txtSize.Text = mailMessage.Size.ToString(); //for (int j = 0; j < atts.Count; j++)
//{
// /**/
// ///取得附件
// att = atts[j]; // /**/
// ///附件名称
// string attname = att.Name; // /**/
// ///上传到服务器
// att.SaveToFile("e:\\attFile\\" + attname); //} }
Response.Write("</table>");
// panMailInfo.Visible = true;
att = null;
atts = null;
}
else
{
Response.Write("没有新邮件!");
} popMail.DeleteMessages();
popMail.Disconnect();
popMail = null;
//}
//catch
//{
// //Response.Write("Warning!请检查邮件服务器的设置是否正确!");
//}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.Net.Sockets;
using System.Collections;
using System.Net.Configuration;
using System.Configuration;
namespace Creator.Mail
{
#region NET Mial 邮件接收类 /// <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
{
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
}