Shawn_Medinsky
Shawn_Medinsky

Reputation: 1

How to read emails dynamically using C# library

I'm trying to work with email data in my application. Everything works fine, I just need that when a new email arrives, it should also be read. This way only the emails are printed before the application starts. Is this possible in the EAGetEmails library using POP3 Server or do I have to program my own Sever or after some short time turn on and off the Application to update itself(This seems to me a rather clumsy solution)?

using EAGetMail;
using System;
using System.Globalization;
using System.IO;

namespace PopServer
{
    class Program
    {
        static string _generateFileName(int sequence)
        {
            DateTime currentDateTime = DateTime.Now;
            return string.Format("{0}-{1:000}-{2:000}.eml",
                currentDateTime.ToString("yyyyMMddHHmmss", new CultureInfo("en-US")),
                currentDateTime.Millisecond,
                sequence);
        }
        static void Main(string[] args)
        {
            try
            {
                
                string localInbox = string.Format("{0}\\inbox", Directory.GetCurrentDirectory());
                
                if (!Directory.Exists(localInbox))
                {
                    Directory.CreateDirectory(localInbox);
                }

                MailServer oServer = new MailServer("popserver.com",
                        "[email protected]",
                        "password",
                        ServerProtocol.Pop3);
                
                oServer.SSLConnection = true;
                oServer.Port = 995;

                MailClient oClient = new MailClient("TryIt");
                oClient.Connect(oServer);

                MailInfo[] infos = oClient.GetMailInfos();
                Console.WriteLine("Total {0} email(s)\r\n", infos.Length);
//Here I tried to take advantage of the fact that if the application is connected via SSLConnection, it could update, but it only prints the emails before starting the application
                while (oServer.SSLConnection)
                {
                    for (int i = 0; i < infos.Length; i++)
                    {
                        MailInfo info = infos[i];
                        Console.WriteLine("Index: {0}; Size: {1}; UIDL: {2}",
                            info.Index, info.Size, info.UIDL);


                        Mail oMail = oClient.GetMail(info);

                        Console.WriteLine("From: {0}", oMail.From.ToString());
                        Console.WriteLine("Subject: {0}\r\n", oMail.Subject);
                        Console.WriteLine("Body: {0}\r\n", oMail.TextBody);

                     

                        string fileName = _generateFileName(i + 1);
                        string fullPath = string.Format("{0}\\{1}", localInbox, fileName);


                        oMail.SaveAs(fullPath, true);

                        //Delete email-isRead
                        //oClient.Delete(info);
                    }
                }
               

                
                oClient.Quit();
                Console.WriteLine("Completed!");
            }
            catch (Exception ep)
            {
                Console.WriteLine(ep.Message);
            }
        
    }
    }
}

Upvotes: 0

Views: 5580

Answers (1)

Quentin Roger
Quentin Roger

Reputation: 6538

EAGetMail is not really popular,you can take a look on this library : https://www.nuget.org/packages/OpenPop.NET/

public static List<Message> FetchAllMessages(string hostname, int port, bool useSsl, string username, string password)
{
    // The client disconnects from the server when being disposed
    using(Pop3Client client = new Pop3Client())
    {
        // Connect to the server
        client.Connect(hostname, port, useSsl);

        // Authenticate ourselves towards the server
        client.Authenticate(username, password);

        // Get the number of messages in the inbox
        int messageCount = client.GetMessageCount();

        // We want to download all messages
        List<Message> allMessages = new List<Message>(messageCount);

        // Messages are numbered in the interval: [1, messageCount]
        // Ergo: message numbers are 1-based.
        // Most servers give the latest message the highest number
        for (int i = messageCount; i > 0; i--)
        {
            allMessages.Add(client.GetMessage(i));
        }

        // Now return the fetched messages
        return allMessages;
    }
}

Then you just have to code a little loop to check email every few secondes.

Upvotes: 2

Related Questions