如何从asp.net Web表单中的特定电子邮件地址获取和读取电子邮件?

我想知道如何从特定的电子邮件地址获取和读取Webforms中的电子邮件,我已经搜索了互联网并找到了一些解决方案,但现在适合我想要的?我正在使用OPENPOP来获取电子邮件.我正在分享我的代码,请告诉我我该怎么做.

页面类别:

public partial class CS : System.Web.UI.Page
{
    protected List<Email> Emails
    {
        get { return (List<Email>)ViewState["Emails"]; }
        set { ViewState["Emails"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Read_Emails();
        }
    }

    private void Read_Emails()
    {
        Pop3Client pop3Client;
        if (Session["Pop3Client"] == null)
        {
            pop3Client = new Pop3Client();
            pop3Client.Connect("pop.gmail.com", 995, true);
            pop3Client.Authenticate("email@gmail.com", "123456", AuthenticationMethod.TryBoth);
            Session["Pop3Client"] = pop3Client;
        }
        else
        {
            pop3Client = (Pop3Client)Session["Pop3Client"];
        }
        int count = pop3Client.GetMessageCount();
        this.Emails = new List<Email>();
        int counter = 0;
        for (int i = count; i >= 1; i--)
        {
            Message message = pop3Client.GetMessage(i);
            Email email = new Email()
            {
                MessageNumber = i,
                Subject = message.Headers.Subject,
                DateSent = message.Headers.DateSent,
                From = string.Format("<a href = 'mailto:{1}'>{0}</a>", message.Headers.From.DisplayName, message.Headers.From.Address),
            };
            MessagePart body = message.FindFirstHtmlVersion();
            if (body != null)
            {
                email.Body = body.GetBodyAsText();
            }
            else
            {
                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);
            counter++;
            if (counter > 2)
            {
                break;
            }
        }
        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 Download(object sender, EventArgs e)
    {
        LinkButton lnkAttachment = (sender as LinkButton);
        GridViewRow row = (lnkAttachment.Parent.Parent.NamingContainer as GridViewRow);
        List<Attachment> attachments = this.Emails.Where(email => email.MessageNumber == Convert.ToInt32(gvEmails.DataKeys[row.RowIndex].Value)).FirstOrDefault().Attachments;
        Attachment attachment = attachments.Where(a => a.FileName == lnkAttachment.Text).FirstOrDefault();
        Response.AddHeader("content-disposition", "attachment;filename=" + attachment.FileName);
        Response.ContentType = attachment.ContentType;
        Response.BinaryWrite(attachment.Content);
        Response.End();
    }
}

电子邮件和附件类别:

[Serializable]
public class Email
{
    public Email()
    {
        this.Attachments = new List<Attachment>();
    }
    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; }
}

我如何从“ abc@def.com”之类的特定发件人处获取电子邮件.

解决方法:

您不能直接从特定用户那里获取消息.但是,如果您不想下载所有内容以查找是否需要任何内容​​,请使用GetMessageHeaders()获取消息标题,并查看.From属性以查看其是否与您想要的…相匹配,如果可以,然后下载电子邮件的内容.

上一篇:从特定日期下载POP3标头(Python)


下一篇:115云盘资源那里买