LumiSoft.Net.POP3.Client接收邮件例子(包括附件)

官方网站:http://www.lumisoft.ee/

LumiSoftReceive.aspx.cs

using System;using System.Collections.Generic;


using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LumiSoft.Net.POP3.Client;
using LumiSoft.Net.Mime;

public partial class LumiSoftReceive : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        receivemail();
    }
    private void receivemail()
    {
        string strHost;
        int intPort;
        string strlogin;
        string strPassword;
        strHost = "pop.sina.com.cn";
        intPort = 110;
        strlogin = "账户";
        strPassword = "密码";
        bool pop3UseSsl = false;
        List<string> gotEmailIds = new List<string>();

        List<Mime> result = new List<Mime>();

        //Response.Write(Count.ToString());
        DataTable mailTable = new DataTable("mail");
        mailTable.Columns.Add(new DataColumn("MessageID", typeof(string)));
        mailTable.Columns.Add(new DataColumn("Keywords", typeof(string)));
        mailTable.Columns.Add(new DataColumn("Title", typeof(string)));
        mailTable.Columns.Add(new DataColumn("frommail", typeof(string)));
        mailTable.Columns.Add(new DataColumn("tomail", typeof(string)));
        mailTable.Columns.Add(new DataColumn("senddate", typeof(string)));
        mailTable.Columns.Add(new DataColumn("numbers", typeof(string)));
        mailTable.Columns.Add(new DataColumn("content", typeof(string)));
        mailTable.Columns.Add(new DataColumn("Attacht", typeof(string)));

        using (LumiSoft.Net.POP3.Client.POP3_Client pop3 = new POP3_Client())
        {
            try
            {
                //与Pop3服务器建立连接
                pop3.Connect(strHost, intPort, pop3UseSsl);
                //验证身份
                pop3.Authenticate(strlogin, strPassword, false);
                //获取邮件信息列表
                POP3_ClientMessageCollection infos = pop3.Messages;
                foreach (POP3_ClientMessage info in infos)
                {
                    //每封Email会有一个在Pop3服务器范围内唯一的Id,检查这个Id是否存在就可以知道以前有没有接收过这封邮件
                    if (gotEmailIds.Contains(info.UID))
                        continue;
                    //获取这封邮件的内容
                    byte[] bytes = info.MessageToByte();
                    //记录这封邮件的Id
                    gotEmailIds.Add(info.UID);

                    //解析从Pop3服务器发送过来的邮件信息
                    Mime m = Mime.Parse(bytes);
                    
                    string Attacht = "";
                    string content = "";
                    if (m != null)
                    {
                        string mailfrom = "";
                        string mailfromname = "";
                        if (m.MainEntity.From != null)
                        {
                            for (int i = 0; i < m.MainEntity.From.Mailboxes.Length; i++)
                            {
                                if (i == 0)
                                {
                                    mailfrom = (m.MainEntity.From).Mailboxes[i].EmailAddress;
                                }
                                else
                                {
                                    mailfrom += string.Format(",{0}", (m.MainEntity.From).Mailboxes[i].EmailAddress);
                                }
                                mailfromname = (m.MainEntity.From).Mailboxes[0].DisplayName != ""
                                                   ? (m.MainEntity.From).Mailboxes[0].DisplayName
                                                   : (m.MainEntity.From).Mailboxes[0].LocalPart;
                            }
                        }
                        string mailto = "";
                        string mailtotocollection = "";
                        if (m.MainEntity.To != null)
                        {
                            mailtotocollection = m.MainEntity.To.ToAddressListString();

                            for (int i = 0; i < m.MainEntity.To.Mailboxes.Length; i++)
                            {
                                if (i == 0)
                                {
                                    mailto = (m.MainEntity.To).Mailboxes[i].EmailAddress;
                                }
                                else
                                {
                                    mailto += string.Format(",{0}", (m.MainEntity.To).Mailboxes[i].EmailAddress);
                                }

                            }
                        }
                       

                        mailTable.Rows.Add(new object[]
                                           {
                                               
                                               //(m.MainEntity.From).Mailboxes[0].DisplayName
                                               m.MainEntity.MessageID,info.UID, m.MainEntity.Subject, mailfrom, m.MainEntity.To, m.MainEntity.Date, m.MimeEntities.Length,m.BodyText ,
                                               m.BodyHtml
                                           });
                        
                    }
                    //获取附件
                    foreach (MimeEntity entry in m.Attachments)
                    {
                        string fileName = entry.ContentDisposition_FileName; //获取文件名称
                        string path = Server.MapPath("~\\Attch\\"+fileName);
                        if(File.Exists(path))
                        {
                            Random random=new Random();
                            int newfile = random.Next(1, 100000);
                            path = Server.MapPath("~\\Attch\\" + newfile.ToString());
                            Directory.CreateDirectory(path);
                            path +="\\"+ fileName;
                        }
                        byte[] data = entry.Data;
                        FileStream pFileStream = null;
                        pFileStream = new FileStream(path, FileMode.Create);
                        pFileStream.Write(data, 0, data.Length);
                        pFileStream.Close();
                    }
                }
                GridView1.DataSource = mailTable;
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
                
                Response.Write(ex.Message);
            }
            
        }

    }

}

LumiSoftReceive.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LumiSoftReceive.aspx.cs" Inherits="LumiSoftReceive" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
     <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            CellPadding="4" ForeColor="#333333" GridLines="None" Width="1003px">
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
            <asp:BoundField DataField="MessageID" HeaderText="MessageID" SortExpression="MessageID" ReadOnly="True" />
            <asp:BoundField DataField="Keywords" HeaderText="Keywords" SortExpression="Keywords" ReadOnly="True" />
            <asp:BoundField DataField="Title" HeaderText="主题" SortExpression="Title" ReadOnly="True" />
            <asp:BoundField DataField="frommail" HeaderText="发信人" SortExpression="frommail" ReadOnly="True" />
            <asp:BoundField DataField="tomail" HeaderText="收信人" SortExpression="tomail" ReadOnly="True" />
            <asp:BoundField DataField="senddate" HeaderText="时间" SortExpression="senddate" ReadOnly="True" />
            <asp:BoundField DataField="numbers" HeaderText="大小" SortExpression="numbers" ReadOnly="True" />
            <asp:BoundField DataField="content" HeaderText="内容" SortExpression="numbers" ReadOnly="True" />
             <asp:BoundField DataField="Attacht" HeaderText="附件" SortExpression="Attacht" ReadOnly="True" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>
晚上更新例子上来

你可能感兴趣的:(client)