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

时间:2014-05-02 17:22:49

标签: c# asp.net pop3

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

Page Class:

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”。

1 个答案:

答案 0 :(得分:3)

您无法直接从特定用户获取消息。但是如果您不想下载所有内容以确定是否需要任何内容​​,请使用GetMessageHeaders()来获取消息头并查看.From属性以查看它是否与您想要的内容相匹配...如果匹配,然后下载电子邮件的内容。