//  
//   Rebex Sample Code License
// 
//   Copyright 2025, REBEX CR s.r.o.
//   All rights reserved.
//   https://www.rebex.net/
// 
//   Permission to use, copy, modify, and/or distribute this software for any
//   purpose with or without fee is hereby granted.
// 
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//   OTHER DEALINGS IN THE SOFTWARE.
// 

using System;
using System.IO;
using System.Text;
using Rebex.Mail;

namespace Rebex.Samples
{
    class Program
    {
        static int Main(string[] args)
        {
            // set the trial license key
            Rebex.Licensing.Key = Rebex.Samples.TrialKey.Key;


            // If no argument is specified, show syntax
            if (args.Length < 1)
            {
                ShowHelp();
                return 1;
            }

            // Load the mail message from disk
            var mail = new MsgMessage();
            mail.Load(args[0]);

            Console.WriteLine(
               "Message contains {0} attachments.",
               mail.Attachments.Count
           );

            // If message has no attachments, just exit
            if (mail.Attachments.Count == 0)
                return 0;

            foreach (MsgAttachment attachment in mail.Attachments)
            {
                // Print attachment's file name and Content-Type
                Console.WriteLine(
                    "Saving '{0}' ({1}).",
                    attachment.FileName,
                    attachment.Properties.GetValue(MsgPropertyTag.AttachMimeTag, MediaTypeNames.Application.Octet)
                );

                // Save the attachment to file 
                attachment.Save(attachment.FileName);
            }

            return 0;
        }

        private static void ShowHelp()
        {
            string applicationName = AppDomain.CurrentDomain.FriendlyName;
            Console.WriteLine("=====================================================================");
            Console.WriteLine(" {0} ", applicationName);
            Console.WriteLine("=====================================================================");
            Console.WriteLine("");
            Console.WriteLine("Extracts all attachments from .msg e-mail message.");
            Console.WriteLine("Supported e-mail formats: .msg (Microsoft Outlook).");
            Console.WriteLine("");
            Console.WriteLine("The program is a sample for Rebex MSG for .NET component.");
            Console.WriteLine("For more info, see https://www.rebex.net/msg/");
            Console.WriteLine("");
            Console.WriteLine("Syntax is: {0} <mailfile.msg>", applicationName);
        }
    }
}
