3
我正在嘗試使用openpop.net創建pop3電子郵件客戶端。 以下是我在下載功能已經嘗試(從GridView控件的模板字段):如何使用openpop.net下載多個附件
[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循環之外時,它說:「檢測到重複的頭文件」。 任何幫助,將不勝感激。
我在該代碼中看不到太多的OpenPop.NET。沒有'Attachment'類。你使用的是舊版本嗎?你看過[OpenPop的例子](http://hpop.sourceforge.net/examples.php)嗎?另外,你可以舉一個最簡單的例子,而不是讓你的代碼與其他類糾纏得太多,它可能會幫助你發現問題,並且會使得stackoverflow用戶更容易反彙編你的代碼。 – foens
你有沒有得到任何解決方案? –
你試過http://stackoverflow.com/questions/10317411/how-to-save-email-attachment-useing-openpop? –