Question

Please have some patience with this question. As I understand if email client reads email from POP3 server, that email is erased from server and only exists in email client.

In attempt to develop very simple web-mail client, I have manage to put together script that will read email from given server. But I'm struggling to find a simplest way to archive that email on client.

Here is the snippet:

protected void Read_Emails()
{
    Pop3Client pop3Client;
    if (Session["Pop3Client"] == null)
    {
        pop3Client = new Pop3Client();
        pop3Client.Connect("mail.mail.com", 110, false);
        pop3Client.Authenticate("user@mail.com", "password");
        Session["Pop3Client"] = pop3Client;
    }
    else
    {
        pop3Client = (Pop3Client)Session["Pop3Client"];
    }
    int count = pop3Client.GetMessageCount();
    DataTable dtMessages = new DataTable();
    dtMessages.Columns.Add("Number");
    dtMessages.Columns.Add("MessageNumber");
    dtMessages.Columns.Add("From");
    dtMessages.Columns.Add("Subject");
    dtMessages.Columns.Add("DateSent");

    for (int i = 1; i <= count; i++)
    {
        try
        {
            Message message = pop3Client.GetMessage(i);
            dtMessages.Rows.Add();
            dtMessages.Rows[dtMessages.Rows.Count - 1]["Number"] = i;
            dtMessages.Rows[dtMessages.Rows.Count - 1]["From"] = message.Headers.From;
            dtMessages.Rows[dtMessages.Rows.Count - 1]["Subject"] = message.Headers.Subject;
            dtMessages.Rows[dtMessages.Rows.Count - 1]["DateSent"] = message.Headers.DateSent;
        }catch{}


    }
    gvEmails.DataSource = dtMessages;
    gvEmails.DataBind();


}

And ASP.NET part

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="MA2._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">


        <asp:GridView ID="gvEmails" runat="server" AutoGenerateColumns = "false">
            <Columns>
            <asp:BoundField HeaderText = "#" DataField = "Number" />
            <asp:BoundField HeaderText = "From" DataField = "From" />
            <asp:HyperLinkField HeaderText = "Subject" DataNavigateUrlFields = "MessageNumber" DataNavigateUrlFormatString = "~/ShowMessageCS.aspx?MessageNumber={0}" DataTextField = "Subject" />
            <asp:BoundField HeaderText = "Date" DataField = "DateSent" />
            </Columns>
        </asp:GridView>

</asp:Content>

My question is how to preserve received emails on client? Using database or store them as files on server?

I was looking everywhere and all examples that I could find, but nowhere I could find part about preserving emails on client.

Was it helpful?

Solution

Ok, so I'll more or less try to provide you a conceptual answer since you haven't tried anything yet... and it depends on your definition of archive. You can archive "data" to a database, or xml file typically. What you're doing is breaking the email up into objects, and all you have to do is store those objects somewhere you can retrieve them. If you don't have a database, use the xml file approach, consider making a series of xml files, so that one doesn't get too big and then slow. The other approach is to a traditional outlook archive and I can only provide you the path here:

The only way you'd be able to create / append to a .pst binary file is through interop services. Here's some links to get you started:

http://www.codeproject.com/Questions/441470/how-to-read-emails-from-outlook-using-csharp

http://www.codeproject.com/Questions/461801/Open-Outlook-asp-net

http://forums.asp.net/t/1041781.aspx/1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top