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.
I found this site with a complete code answer to your poblem