📄 default.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
public partial class Default2 : System.Web.UI.Page
{
public static jmail.POP3Class popMail;
public static jmail.Message mailMessage;
public static jmail.Attachments atts;
protected void Page_Load(object sender, EventArgs e)
{
}
public static string Base64Decode(string str)
{
return Encoding.UTF8.GetString(Convert.FromBase64String(str));
}
protected void Button1_Click(object sender, EventArgs e)
{
popMail = new jmail.POP3Class();
try
{
popMail.Connect(this.TxtPopUser.Text.Trim(), this.TxtPopPwd.Text.Trim(), this.TxtPopServer.Text.Trim(), Convert.ToInt32(this.TxtPopPort.Text.Trim()));
TxtPopPort.Enabled = false;
TxtPopPwd.Enabled = false;
TxtPopServer.Enabled = false;
TxtPopUser.Enabled = false;
Page.RegisterStartupScript("", "<script>alert('连接成功')</script>");
Button1.Enabled = false;
Button2.Enabled = true;
Button3.Enabled = true;
Label1.Text = "邮箱内共有邮件" + popMail.Count.ToString() + "封";
}
catch
{
Page.RegisterStartupScript("", "<script>alert('请检查邮件服务器的设置是否正确')</script>");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
if (popMail != null)
{
if (popMail.Count > 0)
{
if (Convert.ToInt32(txtBh.Text) <= popMail.Count && Convert.ToInt32(txtBh.Text) > 0)
{
mailMessage = popMail.Messages[Convert.ToInt32(txtBh.Text)];
mailMessage.Charset = "GB2312";
mailMessage.Encoding = "Base64";
mailMessage.ISOEncodeHeaders = false;
txtSendMail.Text = mailMessage.From;
txtSubject.Text =Base64Decode(mailMessage.Subject);
txtBody.Text =Base64Decode(mailMessage.Body);
txtSize.Text = mailMessage.Size.ToString();
ReceiveMails(Convert.ToInt32(txtBh.Text));
}
else
{
Page.RegisterStartupScript("", "<script>alert('超出邮件数范围')</script>");
}
}
else
{
Response.Write("没有新邮件");
}
}
else
{
Page.RegisterStartupScript("", "<script>alert('请连接服务器')</script>");
}
}
protected void Button3_Click(object sender, EventArgs e)
{
popMail.Disconnect();
popMail = null;
TxtPopPort.Enabled = true;
TxtPopPwd.Enabled = true;
TxtPopServer.Enabled = true;
TxtPopUser.Enabled = true;
Button1.Enabled = true;
Button2.Enabled = false;
Button3.Enabled = false;
}
private void ReceiveMails(int i)
{
jmail.Attachments atts;
jmail.Attachment att;
if (popMail.Count >= 1 && i <= popMail.Count)
{
LinkButton1.Enabled = true;
LinkButton1.Text = "附件下载";
mailMessage = popMail.Messages[i];
atts = mailMessage.Attachments;
if (atts.Count >= 1)
{
for (int k = 0; k < atts.Count; k++)
{
att = atts[k];
string attname = att.Name;
try
{
Random TempNameInt = new Random();
string NewMailDirName = TempNameInt.Next(100000000).ToString();
Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(".") + "\\AttachFiles\\" + TxtPopUser.Text + "\\" + NewMailDirName);
string mailPath = "\\AttachFiles\\" + TxtPopUser.Text + "\\" + NewMailDirName + "\\" + attname;
att.SaveToFile(System.Web.HttpContext.Current.Server.MapPath(".") + mailPath);
Session["path"] = Server.MapPath("AttachFiles") + "\\" + TxtPopUser.Text + "\\" + NewMailDirName + "\\" + attname;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
else
{
LinkButton1.Text = "暂时没有附件";
LinkButton1.Enabled = false;
}
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
System.IO.FileInfo fi = new FileInfo(Session["path"].ToString());
if (fi.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fi.Name));
Response.AddHeader("Content-Length", fi.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Filter.Close();
Response.WriteFile(fi.FullName);
Response.End();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -