2

im trying to make a sort of email receiver and so far i have got a button, which when pressed will show me the body of the newest email which is perfectly what i want except i also wanted the subject, sender and most importantly the time. Code is below

public static List<OpenPop.Mime.Message> FetchAllMessages(string hostname, int port, bool useSsl, string username, string password)
{
    using (Pop3Client client = new Pop3Client())
    {
        client.Connect(hostname, port, useSsl);
        client.Authenticate(username, password);
        int messageCount = client.GetMessageCount();
        List<OpenPop.Mime.Message> allMessages = new List<OpenPop.Mime.Message>(messageCount);

        for (int i = messageCount; i > 0; i--)
        {
            allMessages.Add(client.GetMessage(i));
        }
        return allMessages;
    }
}

private void button1_Click(object sender, EventArgs e)
{
    List<OpenPop.Mime.Message> allaEmail = FetchAllMessages("pop.gmail.com", 995, true, "xx", "xx");

    StringBuilder builder = new StringBuilder();
    OpenPop.Mime.Message message = allaEmail[0];
    OpenPop.Mime.MessagePart plainText = message.FindFirstPlainTextVersion();
    if (plainText != null)
    {
        builder.Append(plainText.GetBodyAsText());
        MessageBox.Show(builder.ToString());
    }

    OpenPop.Mime.MessagePart html = message.FindFirstHtmlVersion();
    if (html != null)
    {
        builder.Append(html.GetBodyAsText());
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Jason
  • 233
  • 4
  • 17

1 Answers1

0

You can use message.Headers.Subject and message.Headers.Date properties to get what you want.

jstedfast
  • 35,744
  • 5
  • 97
  • 110