小二哥's blog

----zhangzs8896(小二)

导航

jmail接收电子邮件

Posted on 2004-12-08 20:52  小二哥  阅读(552)  评论(1编辑  收藏  举报

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 pu1_erp;

namespace Pu1_Erp.Email
{
 /// <summary>
 /// Jmail_Receive 的摘要说明。
 /// </summary>
 public class Jmail_Receive : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Button Receive_btn;
  protected System.Web.UI.WebControls.TextBox username;
  protected System.Web.UI.WebControls.TextBox userpwd;
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.Label Label2;
  protected System.Web.UI.WebControls.DropDownList pop3_ddl;
  protected System.Web.UI.WebControls.Label Label3;
  protected System.Web.UI.WebControls.TextBox lblMessage;
  protected System.Web.UI.WebControls.Label subject_lab;
  protected System.Web.UI.WebControls.Label Email_Count;
  protected System.Web.UI.WebControls.Label prec_lab;
  protected System.Web.UI.WebControls.Label prec_subject;
  protected System.Web.UI.WebControls.Button login_btn;
  protected System.Web.UI.WebControls.Label us_info;
  protected System.Web.UI.WebControls.Label Label4;
  protected System.Web.UI.WebControls.Label Label5;
  protected System.Web.UI.WebControls.Label SendUs_lab;
  protected System.Web.UI.WebControls.Label SendTm_txt;
  protected System.Web.UI.WebControls.Label Label6;
  protected System.Web.UI.WebControls.TextBox Row_txt;
    // public int count; //公共变量,邮件号
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!IsPostBack)
   {
    Row_txt.Text=Convert.ToString(2);
   }
  }
  //获取邮件
  public void GetMail(int j)
  {
    POP3 myPop3=new pu1_erp.POP3Class();
       string uname,upwd,pop3;
    uname=Session["user"].ToString();
    upwd=Session["pwd"].ToString();
    pop3=Session["pop3"].ToString();

    //确定邮件总数:
  //============================================================
   try{
    myPop3.Connect(uname,upwd,pop3,110);
    Email_Count.Text="邮件总数:"+myPop3.Count.ToString();
   }
   catch
   {
    Email_Count.Text="未连接上邮件服务器!";
   }
        //==============================================================
   try
   {
    Message  msg=new pu1_erp.MessageClass();
                Message  msg_p=new pu1_erp.MessageClass();
    //count=myPop3.Count;
    if(j<myPop3.Count)
    {
     msg=myPop3.Messages[j];
     msg_p=myPop3.Messages[j+1];
    }
                SendUs_lab.Text="发件人:"+msg.FromName.ToString()+"["+msg.From.ToString()+"]";
    subject_lab.Text="邮件主题:<"+msg.Subject.ToString()+">";//邮件标题
                SendTm_txt.Text="发送时间: <"+msg.Date.ToString()+">"; //
    prec_subject.Text=msg_p.Subject.ToString();
             
    msg.ContentType="text/html";
    //msg.ContentTransferEncoding="Quoted-Printable";
    msg.Encoding="uuencode";
    msg.Encoding="quoted-printable";
    msg.Charset="gb2312";

    //lblMessage.Text =msg.BodyText.ToString();//.Body.ToString();  //这里有错误?
                object obj=msg.Body;
                lblMessage.Text=obj.ToString();
    msg.Close();
   }
   catch(Exception e)
   {
     lblMessage.Text=e.ToString();
   }
  
  }

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

  }
  #endregion
 
  //获取邮件总数量
  public bool MailConnect()
  {
   try
   {
    POP3 myPop3=new pu1_erp.POP3Class();
    string uname,upwd,pop3;
    uname=username.Text.Trim();
    upwd=userpwd.Text.Trim();
    pop3=pop3_ddl.SelectedItem.Value.ToString().Trim();
    myPop3.Connect(uname,upwd,pop3,110);
    return true;
   }
   catch{
     return false ;
   }
  }

  //显示邮件信息Click事件:
  private void Receive_btn_Click(object sender, System.EventArgs e)
  {
    //int i;
   try
   {
    //count=MailCount();
    GetMail(Convert.ToInt32(Row_txt.Text));
    Email_Count.Visible=true;
    prec_lab.Visible=true;
    subject_lab.Visible=true;
    prec_subject.Visible=true;
    lblMessage.Visible=true;
    SendUs_lab.Visible=true;
    SendTm_txt.Visible=true;
   }
   catch{
     Response.Write("Error");
   }
    //Response.Write(count.ToString());
  }
        //将用户名与密码存到Session
  private void login_btn_Click(object sender, System.EventArgs e)
  {
   if(MailConnect())
   {
    Session["user"]=username.Text.Trim();
    Session["pwd"]=userpwd.Text.Trim();
    Session["pop3"]=pop3_ddl.SelectedItem.Value.ToString().Trim();
    us_info.Visible=true;
    us_info.Text=Session["user"].ToString()+" 已登陆!";
    Row_txt.Visible=true;
    Receive_btn.Visible=true;
    Label4.Visible=true;
    Label5.Visible=true;
   }
   else{
      us_info.Visible=true;
      us_info.Text="未连接上邮件服务器!";
   }
  }

 

 }
}
图片:http://www.cnblogs.com/zhangzs8896/gallery/image/7867.html