read outlook email in C# using pop3 (in korean)

1.3k views Asked by At

I want to read outlook email in C# using pop3. when i get message from my mailbox, there is encoding exception because i read korean email. how can i read korean email? And Can i set mailbox that i want to read? please help me

    class Program
    {
        static Pop3Client client = new Pop3Client();
        static StringBuilder builder = new StringBuilder(); 
        static void Main(string[] args)
        {

            client.Connect("outlook.office365.com", 995, true);
            client.Authenticate("blahblahblah", "blahblahblah");//로그인

            Console.WriteLine("Checking Inbox");
            DataTable table = new DataTable();
            var count = client.GetMessageCount();//몇개의 메세지가 있는지
            Console.WriteLine(count);

            for (int i=count;i>=1;i--)
            {
                var message = client.GetMessage(i);
                var fromAddress = message.Headers.From.Address;
                var subject = message.Headers.Subject;
                var messageBody = String.Empty;

                var plainText = message.FindFirstPlainTextVersion();

                if (plainText == null)
                {
                    var html = message.FindFirstHtmlVersion();
                    messageBody = html.GetBodyAsText();
                }
                else
                {
                    messageBody = plainText.GetBodyAsText();
                }
                table.Rows.Add(i,subject, fromAddress, messageBody);
            }

        }
    }
1

There are 1 answers

4
Claudio Valerio On BEST ANSWER

Hi and welcome to Stack Overflow. As per my understanding, you are using OpenPop.NET library.

OpenPop.NET uses EncodingFinder class to find correct encoding. By default it supports only utf8 and ascii (at least reading library code at github). According to this page: http://hpop.sourceforge.net/exampleChangeCharacterSetMapping.php you can add your encoding(s) to EncodingFinder. In your case, all you have to do is:

static void Main(string[] args)
{
   EncodingFinder.AddMapping("ks_c_5601-1987", Encoding.GetEncoding(949));
   // rest of the application

Please note this will work only on .NET Framework, not in .NET Core, since the latter supports a really limited number of encodings (https://learn.microsoft.com/en-us/dotnet/api/system.text.encodinginfo.getencoding?view=netcore-3.1).

I do not have a Korean pop3 on which to test this solution, but I hope it will work. Good luck!

Edit after some search It should be possible to work with Korean encoding in .NET Core also, it's just a little trickier:

static void Main(string[] args)
{
   Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
   EncodingFinder.AddMapping("ks_c_5601-1987", Encoding.GetEncoding(949));
   // rest of application

Give it a try, if you are working with .NET Core.