gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:52:50 27 4
gpt4 key购买 nike

我正在尝试使用 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 循环之外再次尝试时,它说:“检测到重复的 header ”。任何帮助将不胜感激。

最佳答案

我找到了 this site为您的问题提供完整的代码答案

关于c# - 如何使用 openpop.net 下载多个附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18072227/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com