3

I am trying to create a pop3 email client using openpop.net. Here is what I have tried in the download function (from a template field of a 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();
            }
        }
    }

The problem is that it downloads only the first attachment. When I tried again by keeping the Response.End() function outside of the foreach loop, it says: "duplicate headers detected". Any help would be appreciated.

laylarenee
  • 3,276
  • 7
  • 32
  • 40
Raging Bull
  • 18,593
  • 13
  • 50
  • 55
  • I can't see much OpenPop.NET in that code. There is no `Attachment` class. Are you using a very old version? Have you looked at the [examples for OpenPop](http://hpop.sourceforge.net/examples.php)? Also, could you give a minimal example instead of having your code tangled that much into other classes, it might help yourself discover the problem and will make it easier for stackoverflow users to disassemble your code. – foens Aug 06 '13 at 05:41
  • did you try http://stackoverflow.com/questions/10317411/how-to-save-email-attachment-useing-openpop ? – Isaiyavan Babu Karan Nov 08 '13 at 09:23

1 Answers1

1

I found this site with a complete code answer to your poblem

laylarenee
  • 3,276
  • 7
  • 32
  • 40
Ching Chang Chung
  • 215
  • 1
  • 5
  • 14