[原创]Jmail收邮件类

5 views
Skip to first unread message

william

unread,
Apr 22, 2006, 10:13:31 AM4/22/06
to 13Com
提供了几个方法
1 把收到的邮件放进DataGrid输出,
2 .........................HashTable输出
可是自定附加存储的路径,并可以将邮件信息存入数据库中
希望对你有用` 使用jmail其实比用Socket简单的多`````

-----------------------收件箱.aspx
-----------------------------------------------------
<%@ Page language="c#" Codebehind="haventMails.aspx.cs"
AutoEventWireup="false" Inherits="CTI.webs.SLreg.eMail.haventMails" %>
<%@ Register TagPrefix="cr" Namespace="CrystalDecisions.Web"
Assembly="CrystalDecisions.Web, Version=9.1.5000.0, Culture=neutral,
PublicKeyToken=692fbea5521e1304" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>haventMails</title>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<script language="javascript" type="text/javascript">
function ShowMail(MailCode)
{

document.open("ShowMailInfo.aspx?mailCode="+MailCode,'','width=550,height=450,top=150,left=350,');
//<%# DataBinder.Eval(Container, "DataItem.mailCode") %>
}
function showDIV()
{
document.getElementById("div1").style.display='block';
}
function stachDIV()
{
document.getElementById("div1").style.display='none';
}
function selectPOP(text)
{
document.getElementById("popServer").innerText=text;
}
</script>
</HEAD>
<body leftMargin="3" topMargin="5" rightMargin="3"
MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table width="100%" border="0" bgcolor="#c0c0ff"
style="BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none;
BORDER-LEFT-STYLE: none; BORDER-BOTTOM-STYLE: none">
<tr id="lookOtherMail" style="DISPLAY:none">
<td align="center" colspan="4">
<table border="0" width="100%">
<tr>
<td>
<asp:textbox Width="95pt" id="popServer" runat="server"
BorderStyle="Groove"
ToolTip="选择或输入POP3服务器地址">pop.163.com</asp:textbox><SELECT
title="123" onchange="selectPOP(this[selectedIndex].value)">
<OPTION value="pop.163.com" selected>163</OPTION>
<OPTION value="pop.sina.com.cn">Sina</OPTION>
<OPTION value="pop.yahoo.com.cn">Yahoo</OPTION>
<OPTION value="pop.263.net">263</OPTION>
<OPTION value="pop.eyou.com">eYou</OPTION>
<OPTION value="pop.sohu.com">Sohu</OPTION>
<OPTION value="pop.21cn.com">21cn</OPTION>
<OPTION value="pop.mail.ynet.com">YNet</OPTION>
<OPTION value="">其他..</OPTION>
</SELECT></td>
<td>用户名:<asp:textbox Width="95pt" id="UserName"
runat="server" BorderStyle="Groove"
ToolTip="输入邮箱登陆名"></asp:textbox></td>
<td>密码:<asp:textbox id="Password" runat="server"
BorderStyle="Groove" TextMode="Password" Width="95pt"
ToolTip="输入邮箱登陆密码"></asp:textbox></td>
<td width="10%"><asp:button Width="100%" id="checkMail"
runat="server" Text="查看邮件" BackColor="#C0C0FF"
BorderColor="White"
BorderWidth="1px"></asp:button></td>
<td width="10%"><INPUT style="BORDER-RIGHT: white 1pt solid;
BORDER-TOP: white 1pt solid; BORDER-LEFT: white 1pt solid; WIDTH: 100%;
BORDER-BOTTOM: white 1pt solid; HEIGHT: 20px; BACKGROUND-COLOR:
#c0c0ff"
onclick="document.getElementById('lookOtherMail').style.display='none'"
type="button" value="取消"></td>
</tr>
</table>
</td>
</tr>
<tr style="BACKGROUND-COLOR: #c0c0ff">
<td align="center" width="25%"><asp:label id="mailBoxName"
runat="server"></asp:label></td>
<td align="center" width="25%"><asp:label id="mailCount"
runat="server"></asp:label></td>
<td align="center" width="25%"><asp:label id="attsCount"
runat="server"></asp:label></td>
<td align="center"><INPUT type="button" value="查看其他邮箱"
style="BORDER-RIGHT: white 1pt solid; BORDER-TOP: white 1pt solid;
BORDER-LEFT: white 1pt solid; WIDTH: 60%; BORDER-BOTTOM: white 1pt
solid; HEIGHT: 20px; BACKGROUND-COLOR: #c0c0ff"
onclick="document.getElementById('lookOtherMail').style.display='block'"></td>
</tr>
<tr bgcolor="#e7e7ff">
<td colSpan="4">
<asp:datagrid id="DataGrid1" runat="server" BorderStyle="None"
Width="100%" AutoGenerateColumns="False"
BorderColor="Gainsboro" BorderWidth="1px" BackColor="White"
CellPadding="3" GridLines="Horizontal"
CellSpacing="3" AllowPaging="True" PageSize="25">
<SelectedItemStyle Font-Bold="True" BorderWidth="0px"
ForeColor="White" BorderColor="#FF8080"
BackColor="#FF8080"></SelectedItemStyle>
<AlternatingItemStyle
BackColor="#F7F7F7"></AlternatingItemStyle>
<ItemStyle Font-Size="10pt" ForeColor="Black"
BackColor="WhiteSmoke"></ItemStyle>
<HeaderStyle ForeColor="#400000"
BackColor="LightSteelBlue"></HeaderStyle>
<FooterStyle ForeColor="#4A3C8C"
BackColor="#B5C7DE"></FooterStyle>
<Columns>
<asp:TemplateColumn HeaderText="级别">
<HeaderStyle Width="35px"></HeaderStyle>
<ItemTemplate>
<asp:Label Runat="server" ID="Label1">
<%#

DataBinder.Eval(Container, "DataItem.jibie")%>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="FromName" HeaderText="发件人">
<HeaderStyle Width="100pt"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="From"
HeaderText="发件人地址"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="邮件主题">
<ItemTemplate>
<a href="javascript:ShowMail(<%# DataBinder.Eval(Container,
"DataItem.mailCode") %>)">
<%#DataBinder.Eval(Container, "DataItem.Subject") %>
</a>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="data"
HeaderText="时间"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="查看附件">
<ItemTemplate>
<asp:Label Runat="server" ID="attCounts">
<%#

DataBinder.Eval(Container, "DataItem.atts")%>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle Height="1px" BorderWidth="3px"
HorizontalAlign="Right" ForeColor="#4A3C8C" Position="TopAndBottom"
BackColor="#E7E7FF"></PagerStyle>
</asp:datagrid></td>
</tr>
<tr>
<td colSpan="4"><asp:validationsummary id="ValidationSummary1"
runat="server" ShowMessageBox="True"
ShowSummary="False"></asp:validationsummary><asp:requiredfieldvalidator
id="pwd" runat="server" ControlToValidate="Password"
ErrorMessage="请输&#13;&#13;&#10;入邮箱密码!"
Display="None"></asp:requiredfieldvalidator>
<asp:requiredfieldvalidator id="name" runat="server"
ControlToValidate="UserName" ErrorMessage="请输入邮箱用户名!"
Display="None"></asp:requiredfieldvalidator></td>
</tr>
</table>
</form>
</body>
</HTML>
----------------------收件箱.aspx .cs--------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;

namespace CTI.webs.SLreg.eMail
{
/// <summary>
/// haventMails 的摘要说明。
/// </summary>
public class haventMails : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label mailBoxName;
protected System.Web.UI.WebControls.Label mailCount;
protected System.Web.UI.WebControls.Label attsCount;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.ValidationSummary
ValidationSummary1;
protected System.Web.UI.WebControls.RequiredFieldValidator pwd;
protected System.Web.UI.WebControls.RequiredFieldValidator name;
static DataTable dt=new DataTable();
protected System.Web.UI.WebControls.TextBox popServer;
protected System.Web.UI.WebControls.TextBox UserName;
protected System.Web.UI.WebControls.TextBox Password;
protected System.Web.UI.WebControls.Button checkMail;

/// <summary>
/// 收邮件
/// </summary>
static CTI.Class.eMailRegulate.POPClass pop3=new
CTI.Class.eMailRegulate.POPClass();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
{

}
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 -
不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.checkMail.Click += new
System.EventHandler(this.checkMail_Click);
this.DataGrid1.PageIndexChanged += new
System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

/// <summary>
/// 显示邮件列表
/// </summary>
private void mailList(string name,string pwd,string popserver,int
port)
{
if(pop3.TestConnect(name,pwd,popserver,port)=="0")
{
if(pop3.MailCount>0)
{
this.mailCount.Text="新邮件 "+pop3.MailCount.ToString()+" 封";
dt=pop3.GetMailMsgDataSet();
this.DataBinder();
}
else
{
this.DataGrid1.Visible=false;
Response.Write("<script
language='javascript'>alert('您当前没有新邮件')</script>");
}
}
else
{
this.DataGrid1.Visible=false;
Response.Write("<script
language='javascript'>alert('邮件下载失败!')</script>");
}

}
/// <summary>
/// 数据绑定
/// </summary>
private void DataBinder()
{
this.DataGrid1.DataSource=dt;
this.DataGrid1.DataBind();
}

private void checkMail_Click(object sender, System.EventArgs e)
{
this.mailList(this.UserName.Text.Trim(),this.Password.Text.Trim(),this.popServer.Text.Trim(),110);
}

private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
this.DataBinder();
}
}
}

--------------------------------------------------------------------------------
/*
* 接收邮件类 POPClass 放弃Socket 改用免费jmail组件
* z x j
*/

public class POPClass
{
#region 属性&变量&对象
/// <summary>
/// 存储邮件信息Hashtable
/// </summary>
Hashtable mailHash=new Hashtable();
/// <summary>
/// 存储邮件信息DataSet
/// </summary>
static DataSet mailDS=new DataSet();
/// <summary>
/// 保存附件地址
/// </summary>
static string savePath="c:\\";
/// <summary>
/// jmail pop3对象
/// </summary>
static jmail.POP3Class pop3;
/// <summary>
/// 邮件附件
/// </summary>
jmail.Attachment att;
/// <summary>
/// 邮件附件集合
/// </summary>
jmail.Attachments atts;
/// <summary>
/// 邮件信息
/// </summary>
jmail.Message msg;
/// <summary>
/// 存储邮件信息DataTable
/// </summary>
DataTable dt;

private string username;
/// <summary>
/// 登陆名
/// </summary>
public string UserName
{
get{return this.username;}
set{this.username=value;}
}
private string password;
public string Password
{
get{return this.password;}
set{this.password=value;}
}
private string popHostServer;
/// <summary>
/// 设置或返回POP3服务器地址
/// </summary>
public string Pop3HostServer
{
get{return this.popHostServer;}
set{this.popHostServer=value;}
}
private int popHostPort;
/// <summary>
/// 设置或返回POP3服务器端口号[default:110]
/// </summary>
public int Pop3HostPort
{
get{return this.popHostPort;}
set{this.popHostPort=value;}
}
private string mailBody;
/// <summary>
/// 邮件正文
/// </summary>
public string MailBody
{
get{return this.mailBody;}
set{this.mailBody=value;}
}
private string from;
/// <summary>
/// 邮件发送人地址
/// </summary>
public string MailFrom
{
get{return this.from;}
set{this.from=value;}
}
private string fromname;
/// <summary>
/// 邮件发送人姓名
/// </summary>
public string MailFromName
{
get{return this.fromname;}
set{this.fromname=value;}
}
private string receiver;
/// <summary>
/// 收件人地址
/// </summary>
public string MailReceiver
{
get{return this.receiver;}
set{this.receiver=value;}
}
private int mailCount;
/// <summary>
/// 邮件数量
/// </summary>
public int MailCount
{
get{return this.mailCount;}
set{this.mailCount=value;}
}
private int attCount;
/// <summary>
/// 附件数量
/// </summary>
public int MailAttCount
{
get{return this.attCount;}
set{this.attCount=value;}
}
private string mailPriority;
public string MailPriority
{
get{return this.mailPriority;}
set{this.mailPriority=value;}
}
#endregion

/// <summary>
/// 创建POP类对象
/// </summary>
public POPClass()
{}

/// <summary>
/// 尝试连接POP3邮件服务器
/// </summary>
/// <param name="UserName">用户名</param>
/// <param name="Password">密码</param>
/// <param name="popServer">服务器地址</param>
/// <param name="port">端口号</param>
/// <returns>string 0:成功 1:失败</returns>
public string TestConnect(string UserName,string Password,string
popServer,int port)
{
try
{
pop3=new POP3Class();
pop3.Connect(UserName,Password,popServer,port);
this.Pop3HostServer=popServer;
this.Password=Password;
this.UserName=UserName;
this.Pop3HostPort=port;
this.MailCount=pop3.Count;
return "0";
}
catch(System.Exception ee)
{
return ee.Message;
//将错误写入日志
}
finally
{
pop3.Disconnect();
pop3=null;
}
}

#region 读取eMail信息[GetMailMsgString reteurn:Hashtable]
/// <summary>
/// 读取eMail信息
/// </summary>
/// <returns>Hashtable</returns>
public Hashtable GetMailMsgHashTable()
{
try
{
this.myConnect();
mailHash.Add("MailCount",pop3.Count.ToString());
//获取邮件数量
if(pop3.Count>0)
{
for(int i=1;i<=pop3.Count;i++)
{
string mailNum=i.ToString(); //邮件记数
this.msg=pop3.Messages[i];
mailHash.Add("body"+mailNum,msg.Body); //正文
mailHash.Add("from"+mailNum,msg.From); //发送人地址
mailHash.Add("fromName"+mailNum,msg.FromName);//发送人姓名
mailHash.Add("subject"+mailNum,msg.Subject); //主题

//处理附件
atts=msg.Attachments;
mailHash.Add("attCount"+mailNum,atts.Count.ToString());
for(int j=0;j<atts.Count;j++)
{
string attNum=j.ToString();//附件记数

att=atts[j];
mailHash.Add("attName"+mailNum+attNum,att.Name);
mailHash.Add("attSize"+mailNum+attNum,att.Size);
mailHash.Add("attContent"+mailNum+attNum,att.ContentType);
mailHash.Add("attPath"+mailNum+attNum,savePath+att.Name);
att.SaveToFile(savePath+att.Name);
}
}
}
mailHash.Add("ISerror","false");
return mailHash;
}
catch(System.Exception ee)
{
mailHash.Add("ISerror","true");
mailHash.Add("errMsg",ee.Message);
return mailHash;
}
finally
{
pop3.Disconnect();
pop3=null;
}
}
#endregion


#region 读取eMail信息[GetMailMsgDataSet reteurn:DataSet]
/// <summary>
/// 读取eMail信息
/// </summary>
/// <returns>DataSet</returns>
public DataTable GetMailMsgDataSet()
{
this.CreateTable();
try
{
this.myConnect();
if(pop3.Count>0)
{
for(int i=1;i<=pop3.Count;i++)
{
//string mailNum=i.ToString(); //邮件记数
this.msg=pop3.Messages[i];
//处理附件
atts=msg.Attachments;
//string haveAtt;
//if(atts.Count>0){haveAtt=;}
//0-不重要,1-一般,2-重要
if(msg.Priority.ToString()=="0"){this.MailPriority="低";}
else
if(msg.Priority.ToString()=="1"){this.MailPriority="一般";}
else if(msg.Priority.ToString()=="2"){this.MailPriority="高";}

this.addRowToTable(i.ToString(),msg.From,msg.FromName,msg.Subject,msg.Priority.ToString(),atts.Count.ToString(),msg.Date.ToShortDateString());
}
}
return dt;
}
catch(System.Exception ee)
{
return dt;
}
finally
{
//msg.Close();
pop3.Disconnect();
pop3=null;
}
}
#endregion


#region 读取eMail信息[GetMailMsgString
reteurn:string--页面调用类属性]
/// <summary>
/// 读取eMail信息 未完成
/// </summary>
/// <returns>string</returns>
public string GetMailMsgString()
{
try
{
//this.pop3.Connect(UserName,Password,popServer,port);
mailHash.Add("MailCount",pop3.Count.ToString());
//获取邮件数量
if(pop3.Count>0)
{
for(int i=1;i<=pop3.Count;i++)
{
string mailNum=i.ToString(); //邮件记数
this.msg=pop3.Messages[i];
mailHash.Add("body"+mailNum,msg.Body); //正文
mailHash.Add("from"+mailNum,msg.From); //发送人地址
mailHash.Add("fromName"+mailNum,msg.FromName);//发送人姓名
mailHash.Add("subject"+mailNum,msg.Subject); //主题

//处理附件
atts=msg.Attachments;
mailHash.Add("attCount"+mailNum,atts.Count.ToString());
for(int j=0;j<atts.Count;j++)
{
string attNum=j.ToString();//附件记数

att=atts[j];
mailHash.Add("attName"+mailNum+attNum,att.Name);
mailHash.Add("attSize"+mailNum+attNum,att.Size);
mailHash.Add("attContent"+mailNum+attNum,att.ContentType);
mailHash.Add("attPath",savePath+att.Name);
att.SaveToFile(savePath+att.Name);
}
}
}
mailHash.Add("ISerror","false");
return "1";
}
catch(System.Exception ee)
{
mailHash.Add("ISerror","true");
mailHash.Add("errMeg",ee.Message);
return "1";
}
finally
{
pop3.Disconnect();
pop3=null;
}
}

#endregion

#region 获取邮件详细信息
public jmail.Message getMailInfo(int mailCode)
{
this.myConnect();
jmail.Message mailInfo;
mailInfo=pop3.Messages[mailCode];
return mailInfo;
}
#endregion

#region 下载附件
/// <summary>
/// 下载附件
/// </summary>
/// <param name="mailCode">邮件编号</param>
/// <param name="attCode">要下载的附件编号</param>
/// <returns></returns>
public bool downloadAtts(int mailCode,int attCode)
{
this.myConnect();
jmail.Message mailInfo=pop3.Messages[mailCode];
jmail.Attachment att=mailInfo.Attachments[attCode];
att.SaveToFile(@"c:\atts\"+DateTime.Now.ToShortDateString()+att.Name);
return false;
}
#endregion

/// <summary>
/// 添加行
/// </summary>
/// <param name="from">发件人</param>
/// <param name="fromname">发件人地址</param>
/// <param name="subject">主题</param>
/// <param name="jibie">级别</param>
/// <param name="atts">是否有附件</param>
/// <param name="data">发件时间</param>
private void addRowToTable(string mailCode,string from,string
fromname,string subject,string jibie,string atts,string data)
{
//须按表中列的顺序添加
string[] newRow=new
string[]{mailCode,from,fromname,subject,jibie,atts,data};
dt.Rows.Add(newRow);
}

private void CreateTable()
{
dt=new DataTable("mailInfo");
dt.Columns.Add("mailCode",System.Type.GetType("System.String"));
//编号,用于查看邮件详细信息
dt.Columns.Add("FromName",System.Type.GetType("System.String"));

dt.Columns.Add("From",System.Type.GetType("System.String"));

dt.Columns.Add("Subject",System.Type.GetType("System.String"));

//dt.Columns.Add("Body",System.Type.GetType("System.String"));

dt.Columns.Add("jibie",System.Type.GetType("System.String"));
dt.Columns.Add("atts",System.Type.GetType("System.String"));

dt.Columns.Add("data",System.Type.GetType("System.String"));
}

/// <summary>
/// 连接popServer
/// </summary>
private void myConnect()
{
pop3=new POP3Class();
pop3.Connect(this.UserName,this.Password,this.Pop3HostServer,this.popHostPort);
}
/// <summary>
/// 连接popServer
/// </summary>
/// <param name="UserLoginName"></param>
/// <param name="Password"></param>
/// <param name="Pop3Server"></param>
/// <param name="Pop3Port"></param>
public string myCounect(string UserLoginName,string Password,string
Pop3Server,int Pop3Port)
{
pop3=new POP3Class();
try
{
pop3.Connect(UserLoginName,Password,Pop3Server,Pop3Port);
this.MailCount=pop3.Count;
return "0";
}
catch
{
return "1";
}
}
}

william

unread,
Apr 22, 2006, 10:14:05 AM4/22/06
to 13Com
期待指教 ^_^
Reply all
Reply to author
Forward
0 new messages