如何使用openpop.net下载多个附件

时间:2013-08-06 05:13:30

标签: c# asp.net openpop

我正在尝试使用openpop.net创建一个pop3电子邮件客户端。 以下是我在下载功能中尝试过的内容(来自gridview的模板字段):

    [Serializable]
    public class Email
    {
        public Email()
        {
            this.Attachments = new List<Attachment>();
        }
        public string Uuid { get; set; }
        public int MessageNumber { get; set; }
        public string From { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public DateTime DateSent { get; set; }
        public List<Attachment> Attachments { get; set; }
    }

    [Serializable]
    public class Attachment
    {
        public string FileName { get; set; }
        public string ContentType { get; set; }
        public byte[] Content { get; set; }
    }

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

        }

        protected List<Email> Emails
        {
            get { return (List<Email>)ViewState["Emails"]; }
            set { ViewState["Emails"] = value; }
        }


        private void Read_Emails()
        {
            Pop3Client pop3Client = new Pop3Client();
            pop3Client.Connect(txtMailAccount.Text, 995, true);
            pop3Client.Authenticate(txtUsername.Text, txtPassword.Text, AuthenticationMethod.TryBoth);
            Session["Pop3Client"] = pop3Client;
            int count = pop3Client.GetMessageCount();
            this.Emails = new List<Email>();
            DataSet ds = new DataSet();
            ds = DB.ExecuteQuery_SP("getAllSeenUuids");
            List<string> uuids = pop3Client.GetMessageUids();
            List<string> listSeenUuids = new List<string>();
            List<string> newListSeenUuids = new List<string>();
            List<Message> newMessages = new List<Message>();
            List<string> listUnreadUuids = new List<string>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                listSeenUuids.Add(ds.Tables[0].Rows[i][0].ToString());
            }
            int uuidCounter = 0;
            for (int i = uuids.Count - 1; i >= 0; i--)
            {
                if (!listSeenUuids.Contains(uuids[i]))
                {
                    Message unseenMessage = pop3Client.GetMessage(i + 1);
                    newMessages.Add(unseenMessage);
                    object[,] parArray = new object[,] { { "@seenUuid", uuids[i] } };
                    DB.ExecuteNonQuery_SP("saveToSeenUuids", parArray);
                    uuidCounter++;
                }
                if (uuidCounter >= Convert.ToInt32(txtNoOfMails.Text))
                    break;
            }
            int j = count;
            foreach (Message message in newMessages)
            {
                Email email = new Email()
                {
                    MessageNumber = j,
                    Subject = message.Headers.Subject,
                    DateSent = message.Headers.DateSent,
                    From = message.Headers.From.MailAddress.ToString(),
                };
                MessagePart body = message.FindFirstPlainTextVersion();
                if (body != null)
                {
                    email.Body = body.GetBodyAsText();
                }
                List<MessagePart> attachments = message.FindAllAttachments();

                foreach (MessagePart attachment in attachments)
                {
                    email.Attachments.Add(new Attachment
                    {
                        FileName = attachment.FileName,
                        ContentType = attachment.ContentType.MediaType,
                        Content = attachment.Body
                    });
                }
                this.Emails.Add(email);
                j--;
            }
            gvEmails.DataSource = this.Emails;
            gvEmails.DataBind();
        }
        protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Repeater rptAttachments = (e.Row.FindControl("rptAttachments") as Repeater);
                List<Attachment> attachments = this.Emails.Where(email => email.MessageNumber == Convert.ToInt32(gvEmails.DataKeys[e.Row.RowIndex].Value)).FirstOrDefault().Attachments;
                rptAttachments.DataSource = attachments;
                rptAttachments.DataBind();
            }
        }
        protected void SaveAll(object sender, EventArgs e)
        {
            Button btnSaveAll = (sender as Button);
            GridViewRow row = (GridViewRow)btnSaveAll.NamingContainer;
            int id = (int)gvEmails.DataKeys[row.RowIndex].Value;
            List<Attachment> attachments = this.Emails.Where(email => email.MessageNumber == Convert.ToInt32(gvEmails.DataKeys[row.RowIndex].Value)).FirstOrDefault().Attachments;
            foreach (Attachment attachment1 in attachments)
            {
                Attachment attachment = attachments.Where(a => a.FileName == attachment1.FileName).FirstOrDefault();
                Response.AddHeader("content-disposition", "attachment;filename=" + attachment.FileName);
                Response.ContentType = attachment.ContentType;
                Response.BinaryWrite(attachment.Content);
                Response.End();
            }
        }
    }

问题是它只下载第一个附件。 当我再次尝试将Response.End()函数保留在foreach循环之外时,它会说:“检测到重复的标题”。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我找到了this site,其中包含对您的问题的完整代码答案