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 jmail;
namespace CommonFunction
{
	/// <summary>
	/// JmailConfig 的摘要说明。
	/// </summary>
	public class JmailConfig : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox TxtPopServer;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator5;
		protected System.Web.UI.WebControls.TextBox TxtPopUser;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator6;
		protected System.Web.UI.WebControls.TextBox TxtPopPwd;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator7;
		protected System.Web.UI.WebControls.TextBox TxtPopPort;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator8;
		protected System.Web.UI.WebControls.Button BtnTake;
		protected System.Web.UI.WebControls.TextBox txtSender;
		protected System.Web.UI.WebControls.TextBox txtSubject;
		protected System.Web.UI.WebControls.Panel panMailInfo;
		protected System.Web.UI.WebControls.TextBox txtSendMail;
		protected System.Web.UI.WebControls.TextBox txtpriority;
		protected System.Web.UI.WebControls.TextBox txtSize;
		protected System.Web.UI.WebControls.TextBox txtBody;
		protected System.Web.UI.WebControls.TextBox txtAttPath;
		protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.BtnTake.Click += new System.EventHandler(this.BtnTake_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void BtnTake_Click(object sender, System.EventArgs e)
		{
			jmail.POP3Class popMail = new POP3Class();//建立收邮件对象
			jmail.Message mailMessage;  //建立邮件信息接口
			jmail.Attachments atts;//建立附件集接口
			jmail.Attachment att;//建立附件接口

			try
			{
				popMail.Connect(TxtPopUser.Text.Trim(),TxtPopPwd.Text.Trim(),TxtPopServer.Text.Trim(),Convert.ToInt32(TxtPopPort.Text.Trim()));

				if(0 < popMail.Count)                                                                          //如果收到邮件
				{
					for(int i=1;i <= popMail.Count;i++)                                                         //根据取到的邮件数量依次取得每封邮件
					{
						mailMessage = popMail.Messages[i];                                                        //取得一条邮件信息
						atts = mailMessage.Attachments;                                                           //取得该邮件的附件集合
						mailMessage.Charset = "GB2312";                                                           //设置邮件的编码方式
						mailMessage.Encoding = "Base64";                                                          //设置邮件的附件编码方式
						mailMessage.ISOEncodeHeaders = false;                                                     //是否将信头编码成iso-8859-1字符集
						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);                                              //上传到服务器
							
						}
						
					}
					panMailInfo.Visible = true;
					att = null;
					atts = null;
				}
				else
				{
					Response.Write("没有新邮件!");
				}

				popMail.DeleteMessages();
				popMail.Disconnect();
				popMail = null;
			}
			catch
			{
				Response.Write("Warning!请检查邮件服务器的设置是否正确！");
			}
		}
	}
}
