예제 #1
0
		/// <summary>
		/// Initializes a new instance of the <see cref="MailKit.MailService"/> class.
		/// </summary>
		/// <remarks>
		/// Initializes a new instance of the <see cref="MailKit.MailService"/> class.
		/// </remarks>
		/// <param name="protocolLogger">The protocol logger.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="protocolLogger"/> is <c>null</c>.
		/// </exception>
		protected MailService (IProtocolLogger protocolLogger)
		{
			if (protocolLogger == null)
				throw new ArgumentNullException ("protocolLogger");

			ProtocolLogger = protocolLogger;
		}
예제 #2
0
		/// <summary>
		/// Initializes a new instance of the <see cref="MailKit.Net.Smtp.SmtpStream"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new <see cref="SmtpStream"/>.
		/// </remarks>
		/// <param name="source">The underlying network stream.</param>
		/// <param name="socket">The underlying network socket.</param>
		/// <param name="protocolLogger">The protocol logger.</param>
		public SmtpStream (Stream source, Socket socket, IProtocolLogger protocolLogger)
		{
			logger = protocolLogger;
			IsConnected = true;
			Stream = source;
			Socket = socket;
		}
예제 #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MailKit.Net.Smtp.SmtpStream"/> class.
 /// </summary>
 /// <remarks>
 /// Creates a new <see cref="SmtpStream"/>.
 /// </remarks>
 /// <param name="source">The underlying network stream.</param>
 /// <param name="socket">The underlying network socket.</param>
 /// <param name="protocolLogger">The protocol logger.</param>
 public SmtpStream(Stream source, Socket socket, IProtocolLogger protocolLogger)
 {
     logger      = protocolLogger;
     IsConnected = true;
     Stream      = source;
     Socket      = socket;
 }
예제 #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MailKit.MailService"/> class.
        /// </summary>
        /// <remarks>
        /// Initializes a new instance of the <see cref="MailKit.MailService"/> class.
        /// </remarks>
        /// <param name="protocolLogger">The protocol logger.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="protocolLogger"/> is <c>null</c>.
        /// </exception>
        protected MailService(IProtocolLogger protocolLogger)
        {
            if (protocolLogger == null)
            {
                throw new ArgumentNullException("protocolLogger");
            }

            ProtocolLogger = protocolLogger;
        }
예제 #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MailKit.Net.Smtp.SmtpClient"/> class.
        /// </summary>
        /// <remarks>
        /// Before you can send messages with the <see cref="SmtpClient"/>, you
        /// must first call the <see cref="Connect"/> method. Depending on the
        /// server, you may also need to authenticate using the
        /// <see cref="Authenticate"/> method.
        /// </remarks>
        /// <param name="protocolLogger">The protocol logger.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="protocolLogger"/> is <c>null</c>.
        /// </exception>
        public SmtpClient(IProtocolLogger protocolLogger)
        {
            if (protocolLogger == null)
            {
                throw new ArgumentNullException("protocolLogger");
            }

            logger = protocolLogger;
        }
예제 #6
0
파일: MailService.cs 프로젝트: dcga/MailKit
		/// <summary>
		/// Initializes a new instance of the <see cref="MailKit.MailService"/> class.
		/// </summary>
		/// <remarks>
		/// Initializes a new instance of the <see cref="MailKit.MailService"/> class.
		/// </remarks>
		/// <param name="protocolLogger">The protocol logger.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="protocolLogger"/> is <c>null</c>.
		/// </exception>
		protected MailService (IProtocolLogger protocolLogger)
		{
			if (protocolLogger == null)
				throw new ArgumentNullException ("protocolLogger");

#if !NETFX_CORE
			SslProtocols = DefaultSslProtocols;
#endif
			ProtocolLogger = protocolLogger;
		}
예제 #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MailKit.MailService"/> class.
        /// </summary>
        /// <remarks>
        /// Initializes a new instance of the <see cref="MailKit.MailService"/> class.
        /// </remarks>
        /// <param name="protocolLogger">The protocol logger.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="protocolLogger"/> is <c>null</c>.
        /// </exception>
        protected MailService(IProtocolLogger protocolLogger)
        {
            if (protocolLogger == null)
            {
                throw new ArgumentNullException("protocolLogger");
            }

#if !NETFX_CORE
            SslProtocols = DefaultSslProtocols;
#endif
            ProtocolLogger = protocolLogger;
        }
예제 #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MailKit.Net.Imap.ImapClient"/> class.
        /// </summary>
        /// <remarks>
        /// Before you can retrieve messages with the <see cref="ImapClient"/>, you
        /// must first call the <see cref="Connect"/> method and authenticate with
        /// the <see cref="Authenticate"/> method.
        /// </remarks>
        /// <param name="protocolLogger">The protocol logger.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="protocolLogger"/> is <c>null</c>.
        /// </exception>
        public ImapClient(IProtocolLogger protocolLogger)
        {
            if (protocolLogger == null)
            {
                throw new ArgumentNullException("protocolLogger");
            }

            // FIXME: should this take a ParserOptions argument?
            engine        = new ImapEngine();
            engine.Alert += OnAlert;
            logger        = protocolLogger;
        }
 internal MailkitSmtpClientAdaptor(IProtocolLogger logger)
 {
     _smtpClient = new SmtpRelayClientImp(logger);
 }
예제 #10
0
		/// <summary>
		/// Initializes a new instance of the <see cref="MailKit.Net.Smtp.SmtpClient"/> class.
		/// </summary>
		/// <remarks>
		/// Before you can send messages with the <see cref="SmtpClient"/>, you must first call one of
		/// the <a href="Overload_MailKit_Net_Smtp_SmtpClient_Connect.htm">Connect</a> methods.
		/// Depending on whether the SMTP server requires authenticating or not, you may also need to
		/// authenticate using one of the
		/// <a href="Overload_MailKit_Net_Smtp_SmtpClient_Authenticate.htm">Authenticate</a> methods.
		/// </remarks>
		/// <param name="protocolLogger">The protocol logger.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="protocolLogger"/> is <c>null</c>.
		/// </exception>
		/// <example>
		/// <code language="c#" source="Examples\SmtpExamples.cs" region="ProtocolLogger"/>
		/// </example>
		public SmtpClient (IProtocolLogger protocolLogger) : base (protocolLogger)
		{
		}
예제 #11
0
파일: MailStore.cs 프로젝트: dcga/MailKit
		/// <summary>
		/// Initializes a new instance of the <see cref="MailKit.MailStore"/> class.
		/// </summary>
		/// <remarks>
		/// Initializes a new instance of the <see cref="MailKit.MailStore"/> class.
		/// </remarks>
		/// <param name="protocolLogger">The protocol logger.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="protocolLogger"/> is <c>null</c>.
		/// </exception>
		protected MailStore (IProtocolLogger protocolLogger) : base (protocolLogger)
		{
		}
예제 #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MailKit.Net.Imap.ImapStream"/> class.
 /// </summary>
 /// <param name="source">The underlying network stream.</param>
 /// <param name="protocolLogger">The protocol logger.</param>
 public ImapStream(Stream source, IProtocolLogger protocolLogger)
 {
     logger = protocolLogger;
     IsConnected = true;
     Stream = source;
 }
 public MailkitSmtpClientFactory()
 {
     _logger = new MailProtocolLogger();
 }
예제 #14
0
파일: MailStore.cs 프로젝트: ywscr/MailKit
 /// <summary>
 /// Initializes a new instance of the <see cref="MailKit.MailStore"/> class.
 /// </summary>
 /// <remarks>
 /// Initializes a new instance of the <see cref="MailKit.MailStore"/> class.
 /// </remarks>
 /// <param name="protocolLogger">The protocol logger.</param>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="protocolLogger"/> is <c>null</c>.
 /// </exception>
 protected MailStore(IProtocolLogger protocolLogger) : base(protocolLogger)
 {
 }
예제 #15
0
 public InboxService(IProtocolLogger protoLogger, IOptions <MailOptions> mailSettings)
 {
     _protoLog     = protoLogger;
     _mailSettings = mailSettings.Value;
 }
예제 #16
0
파일: ImapStream.cs 프로젝트: zamis/MailKit
 /// <summary>
 /// Initializes a new instance of the <see cref="MailKit.Net.Imap.ImapStream"/> class.
 /// </summary>
 /// <remarks>
 /// Creates a new <see cref="ImapStream"/>.
 /// </remarks>
 /// <param name="source">The underlying network stream.</param>
 /// <param name="protocolLogger">The protocol logger.</param>
 public ImapStream(Stream source, IProtocolLogger protocolLogger)
 {
     logger      = protocolLogger;
     IsConnected = true;
     Stream      = source;
 }
예제 #17
0
		/// <summary>
		/// Initializes a new instance of the <see cref="MailKit.Net.Smtp.SmtpClient"/> class.
		/// </summary>
		/// <remarks>
		/// Before you can send messages with the <see cref="SmtpClient"/>, you must first call one of
		/// the <a href="Overload_MailKit_Net_Smtp_SmtpClient_Connect.htm">Connect</a> methods.
		/// Depending on whether the SMTP server requires authenticating or not, you may also need to
		/// authenticate using one of the
		/// <a href="Overload_MailKit_Net_Smtp_SmtpClient_Authenticate.htm">Authenticate</a> methods.
		/// </remarks>
		/// <param name="protocolLogger">The protocol logger.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="protocolLogger"/> is <c>null</c>.
		/// </exception>
		/// <example>
		/// <code language="c#" source="Examples\SmtpExamples.cs" region="ProtocolLogger"/>
		/// </example>
		public SmtpClient (IProtocolLogger protocolLogger) : base (protocolLogger)
		{
			QueryCapabilitiesAfterAuthenticating = true;
		}
예제 #18
0
 public SmtpRelayClientImp(IProtocolLogger protocolLogger) : base(protocolLogger)
 {
 }
예제 #19
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MailKit.Net.Imap.ImapClient"/> class.
        /// </summary>
        /// <remarks>
        /// Before you can retrieve messages with the <see cref="ImapClient"/>, you must first
        /// call the <see cref="Connect(Uri,CancellationToken)"/> method and authenticate with
        /// the <see cref="Authenticate(ICredentials,CancellationToken)"/> method.
        /// </remarks>
        /// <param name="protocolLogger">The protocol logger.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="protocolLogger"/> is <c>null</c>.
        /// </exception>
        public ImapClient(IProtocolLogger protocolLogger)
        {
            if (protocolLogger == null)
                throw new ArgumentNullException ("protocolLogger");

            // FIXME: should this take a ParserOptions argument?
            engine = new ImapEngine ();
            engine.Alert += OnAlert;
            logger = protocolLogger;
        }
예제 #20
0
		/// <summary>
		/// Initializes a new instance of the <see cref="MailKit.MailTransport"/> class.
		/// </summary>
		/// <remarks>
		/// Initializes a new instance of the <see cref="MailKit.MailTransport"/> class.
		/// </remarks>
		/// <param name="protocolLogger">The protocol logger.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="protocolLogger"/> is <c>null</c>.
		/// </exception>
		protected MailTransport (IProtocolLogger protocolLogger) : base (protocolLogger)
		{
		}
예제 #21
0
파일: Pop3Client.cs 프로젝트: EGrun/MailKit
        /// <summary>
        /// Initializes a new instance of the <see cref="MailKit.Net.Pop3.Pop3Client"/> class.
        /// </summary>
        /// <remarks>
        /// Before you can retrieve messages with the <see cref="Pop3Client"/>, you
        /// must first call the <see cref="Connect"/> method and authenticate with
        /// the <see cref="Authenticate"/> method.
        /// </remarks>
        /// <param name="protocolLogger">The protocol logger.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="protocolLogger"/> is <c>null</c>.
        /// </exception>
        public Pop3Client(IProtocolLogger protocolLogger)
        {
            if (protocolLogger == null)
                throw new ArgumentNullException ("protocolLogger");

            // FIXME: should this take a ParserOptions argument?
            engine = new Pop3Engine ();
            logger = protocolLogger;
        }
예제 #22
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MailKit.MailTransport"/> class.
 /// </summary>
 /// <remarks>
 /// Initializes a new instance of the <see cref="MailKit.MailTransport"/> class.
 /// </remarks>
 /// <param name="protocolLogger">The protocol logger.</param>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="protocolLogger"/> is <c>null</c>.
 /// </exception>
 protected MailTransport(IProtocolLogger protocolLogger) : base(protocolLogger)
 {
 }
예제 #23
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MailKit.Net.Smtp.SmtpClient"/> class.
        /// </summary>
        /// <remarks>
        /// Before you can send messages with the <see cref="SmtpClient"/>, you must first call
        /// the <see cref="Connect(Uri,CancellationToken)"/> method. Depending on the server,
        /// you may also need to authenticate using the
        /// <see cref="Authenticate(ICredentials,CancellationToken)"/> method.
        /// </remarks>
        /// <param name="protocolLogger">The protocol logger.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="protocolLogger"/> is <c>null</c>.
        /// </exception>
        public SmtpClient(IProtocolLogger protocolLogger)
        {
            if (protocolLogger == null)
                throw new ArgumentNullException ("protocolLogger");

            logger = protocolLogger;
        }
예제 #24
0
		/// <summary>
		/// Initializes a new instance of the <see cref="MailKit.Net.Imap.ImapClient"/> class.
		/// </summary>
		/// <remarks>
		/// Before you can retrieve messages with the <see cref="ImapClient"/>, you must first
		/// call one of the <a href="Overload_MailKit_Net_Imap_ImapClient_Connect.htm">Connect</a>
		/// methods and then authenticate with the one of the
		/// <a href="Overload_MailKit_Net_Imap_ImapClient_Authenticate.htm">Authenticate</a>
		/// methods.
		/// </remarks>
		/// <example>
		/// <code language="c#" source="Examples\ImapExamples.cs" region="ProtocolLogger"/>
		/// </example>
		/// <param name="protocolLogger">The protocol logger.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="protocolLogger"/> is <c>null</c>.
		/// </exception>
		public ImapClient (IProtocolLogger protocolLogger) : base (protocolLogger)
		{
			// FIXME: should this take a ParserOptions argument?
			engine = new ImapEngine (CreateImapFolder);
			engine.Alert += OnEngineAlert;
		}
예제 #25
0
		/// <summary>
		/// Initializes a new instance of the <see cref="MailKit.MailSpool"/> class.
		/// </summary>
		/// <remarks>
		/// Initializes a new instance of the <see cref="MailKit.MailSpool"/> class.
		/// </remarks>
		/// <param name="protocolLogger">The protocol logger.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="protocolLogger"/> is <c>null</c>.
		/// </exception>
		protected MailSpool (IProtocolLogger protocolLogger) : base (protocolLogger)
		{
		}
예제 #26
0
 public FakeClient(IProtocolLogger logger) : base(logger)
 {
 }
예제 #27
0
 public DsnSmtpClient(IProtocolLogger protocolLogPath, DeliveryStatusNotification?dsn = null)
     : base(protocolLogPath)
 {
     Dsn = dsn;
 }
예제 #28
0
        public static void Main(string[] args)
        {
            AppDomain currentDomain = AppDomain.CurrentDomain;

            currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

            string dataPath        = GetLocalAppDataPath();
            string MBoxViewerPath  = Path.Combine(dataPath, "MBoxViewer");
            string MailServicePath = Path.Combine(MBoxViewerPath, "MailService");
            string TempPath        = Path.Combine(MailServicePath, "Temp");

            string okFilePath               = MailServicePath + "\\ForwardMailSuccess.txt";
            string errorFilePath            = MailServicePath + "\\ForwardMailError.txt";
            string errorFilePathOldInstance = MailServicePath + "\\ForwardMailError2.txt";

            System.IO.DirectoryInfo info = Directory.CreateDirectory(TempPath);

            string loggerFilePath = FindKeyinArgs(args, "--logger-file");
            var    logger         = new FileLogger();

            logger.Open(loggerFilePath);
            logger.Log("Logger Open");

            try
            {
                // File.Delete doesn't seem to generate exceptions if file doesn't exist
                //if (File.Exists(okFilePath)
                File.Delete(okFilePath);
                //if (File.Exists(errorFilePath)
                File.Delete(errorFilePath);
                File.Delete(errorFilePathOldInstance);
            }
            catch (Exception ex)
            {
                string txt = String.Format("Delete Critical Files Failed\n{0}\n{1}\n{2}\n\n{3}",
                                           okFilePath, errorFilePath, errorFilePathOldInstance, ex.Message);
                bool rval = FileUtils.CreateWriteCloseFile(errorFilePath, txt);
                logger.Log("Exception in Delete Critical Files: ", ex.ToString());
                System.Environment.Exit(ExitCodes.ExitMailAddress);
            }

            int numArgs = args.GetLength(0);

            if ((numArgs <= 0) || ((numArgs % 2) != 0))
            {
                string errorText = String.Format("Invalid command argument list: {0} .", String.Join(" ", args));
                bool   rval      = FileUtils.CreateWriteCloseFile(errorFilePath, errorText + "\n");
                logger.Log(errorText);
                System.Environment.Exit(ExitCodes.ExitCmdArguments);
            }

            /*
             * if (numArgs <= 0)
             * {
             *  logger.Log(@"Usage: --from addr --to addr1,addr2,.. --cc addr1,addr2,.. -bcc addr1,addr2,..
             *      --user login-user-name --passwd --login-user-password --smtp smtp-server-name", "");
             *  Debug.Assert(true == false);
             *  System.Environment.Exit(1);
             * }
             */
            string     instance               = "";
            IniFile    smtpIni                = null;
            EMailInfo  mailInfo               = new EMailInfo();
            SMTPConfig smtpConfig             = new SMTPConfig();
            string     smtpConfigFilePath     = "";
            string     UserPassword           = "";
            string     protocolLoggerFilePath = "";

            int tcpListenPort = 0;

            logger.Log("Command line argument list:");
            for (int j = 0, i = 0; j < numArgs; j = j + 2, i++)
            {
                string key = args[j];
                string val = args[j + 1];

                if (!key.StartsWith("--"))
                {
                    string errorText = String.Format("Invalid key: {0} ", key);
                    bool   rval      = FileUtils.CreateWriteCloseFile(errorFilePath, errorText + "\n");
                    logger.Log(errorText);
                    System.Environment.Exit(ExitCodes.ExitCmdArguments);
                }
                if ((j + 1) >= numArgs)
                {
                    string errorText = String.Format("Found key: {0} without value.", key);
                    bool   rval      = FileUtils.CreateWriteCloseFile(errorFilePath, errorText + "\n");
                    logger.Log(errorText);
                    System.Environment.Exit(ExitCodes.ExitCmdArguments);
                }
                if (key.CompareTo("--instance-id") == 0)
                {
                    instance = val;
                }
                else if (key.CompareTo("--smtp-protocol-logger") == 0)
                {
                    protocolLoggerFilePath = val;
                }
                else if (key.CompareTo("--from") == 0)
                {
                    mailInfo.From = val;
                }
                else if (key.CompareTo("--to") == 0)
                {
                    mailInfo.To = val;
                }
                else if (key.CompareTo("--cc") == 0)
                {
                    mailInfo.CC = val;
                }
                else if (key.CompareTo("--bcc") == 0)
                {
                    mailInfo.BCC = val;
                }
                else if (key.CompareTo("--user") == 0)
                {
                    mailInfo.To = val;
                }
                else if (key.CompareTo("--passwd") == 0)
                {
                    UserPassword = val;
                }
                else if (key.CompareTo("--smtp-cnf") == 0)
                {
                    smtpConfigFilePath = val;
                }
                else if (key.CompareTo("--tcp-port") == 0)
                {
                    tcpListenPort = int.Parse(val);
                }
                else if (key.CompareTo("--eml-file") == 0)
                {
                    mailInfo.EmlFilePath = val;
                }
                else if (key.CompareTo("--mail-text-file") == 0)
                {
                    mailInfo.TextFilePath = val;
                }
                else if (key.CompareTo("--logger-file") == 0)
                {
                    ; // see FindKeyinArgs(args, "--logger-file");
                }
                else
                {
                    logger.Log(String.Format("    Unknown Key: {0} {1}", args[j], args[j + 1]));
                }
                logger.Log(String.Format("    {0} {1}", args[j], args[j + 1]));
            }

            if (smtpConfigFilePath.Length == 0)
            {
                string errorText = String.Format("required --smtp-cnf command line argument missing.");
                bool   rval      = FileUtils.CreateWriteCloseFile(errorFilePath, errorText + "\n");
                logger.Log(errorText);
                System.Environment.Exit(ExitCodes.ExitCmdArguments);
            }

            if (!File.Exists(smtpConfigFilePath))
            {
                string errorText = String.Format("SMTP configuration file {0} doesn't exist.", smtpConfigFilePath);
                bool   rval      = FileUtils.CreateWriteCloseFile(errorFilePath, errorText + "\n");
                logger.Log(errorText);
                System.Environment.Exit(ExitCodes.ExitCmdArguments);
            }
            try
            {
                if (protocolLoggerFilePath.Length > 0)
                {
                    File.Delete(protocolLoggerFilePath);
                }
            }
            catch (Exception /*ex*/) {; } // ignore

            smtpIni = new IniFile(smtpConfigFilePath);

            string ActiveMailService = smtpIni.IniReadValue("MailService", "ActiveMailService");

            smtpConfig.MailServiceName   = smtpIni.IniReadValue(ActiveMailService, "MailServiceName");
            smtpConfig.SmtpServerAddress = smtpIni.IniReadValue(ActiveMailService, "SmtpServerAddress");
            smtpConfig.SmtpServerPort    = int.Parse(smtpIni.IniReadValue(ActiveMailService, "SmtpServerPort"));
            smtpConfig.UserAccount       = smtpIni.IniReadValue(ActiveMailService, "UserAccount");
            if (UserPassword.Length > 0)
            {
                smtpConfig.UserPassword = UserPassword;
            }
            else
            {
                smtpConfig.UserPassword = smtpIni.IniReadValue(ActiveMailService, "UserPassword");
            }
            smtpConfig.EncryptionType = int.Parse(smtpIni.IniReadValue(ActiveMailService, "EncryptionType"));

            logger.Log(smtpConfig.ToString());

            // Uncomment when you exec this application from MBoxViewer
            //smtpConfig.UserPassword = "";
            if (smtpConfig.UserPassword.Length == 0)
            {
                logger.Log("Waiting to receive password");
                smtpConfig.UserPassword = WaitForPassword(tcpListenPort, logger, errorFilePath);

                if (smtpConfig.UserPassword.Length > 0)
                {
                    logger.Log("Received non empty password");
                    //logger.Log("Received non empty password: "******"Received empty password");
                }

                int found = smtpConfig.UserPassword.IndexOf(":");
                if (found <= 0)
                {
                    // Old instance , log to differnt file
                    bool rval = FileUtils.CreateWriteCloseFile(errorFilePathOldInstance, "Received invalid id:password. Exitting\n");
                    System.Environment.Exit(ExitCodes.ExitCmdArguments);
                }
                string id     = smtpConfig.UserPassword.Substring(0, found);
                string passwd = smtpConfig.UserPassword.Substring(found + 1);
                smtpConfig.UserPassword = passwd;

                logger.Log("Command line instance id: ", instance);
                logger.Log("Received instance id: ", id);
                //logger.Log("Received password: "******"Received password: "******"xxxxxxxxxxxx");

                if (id.CompareTo(instance) != 0)
                {
                    // Old instance , log to different file
                    bool rval = FileUtils.CreateWriteCloseFile(errorFilePathOldInstance, "This is old instance. Exitting\n");
                    System.Environment.Exit(ExitCodes.ExitCmdArguments);
                }
            }

            MimeKit.ParserOptions opt = new MimeKit.ParserOptions();

            var From = new MailboxAddress("", smtpConfig.UserAccount);
            //
            InternetAddressList CCList  = new InternetAddressList();
            InternetAddressList BCCList = new InternetAddressList();
            InternetAddressList ToList  = new InternetAddressList();

            try
            {
                if (mailInfo.To.Length > 0)
                {
                    ToList = MimeKit.InternetAddressList.Parse(opt, mailInfo.To);
                }
                if (mailInfo.CC.Length > 0)
                {
                    CCList = MimeKit.InternetAddressList.Parse(opt, mailInfo.CC);
                }
                if (mailInfo.BCC.Length > 0)
                {
                    BCCList = MimeKit.InternetAddressList.Parse(opt, mailInfo.BCC);
                }
            }
            catch (Exception ex)
            {
                bool rval = FileUtils.CreateWriteCloseFile(errorFilePath, "Parsing Internet Address list Failed\n", ex.Message);
                logger.Log("Exception in InternetAddressList.Parse: ", ex.ToString());
                System.Environment.Exit(ExitCodes.ExitMailAddress);
            }

            //
            string emlFilePath = mailInfo.EmlFilePath;

            // create the main textual body of the message
            var text = new TextPart("plain");

            try
            {
                text.Text = File.ReadAllText(mailInfo.TextFilePath, Encoding.UTF8);
            }
            catch (Exception ex)
            {
                bool rval = FileUtils.CreateWriteCloseFile(errorFilePath, "Building Mime Mesage Failed\n", ex.Message);
                logger.Log("Exception in Building Mime Message: ", ex.ToString());
                System.Environment.Exit(ExitCodes.ExitMimeMessage);
            }

            logger.Log("Forwarding Eml file:", emlFilePath);
            MimeMessage msg = null;

            try
            {
                var message = new MimeMessage();
                message = MimeKit.MimeMessage.Load(emlFilePath);

                List <MimeMessage> mimeMessages = new List <MimeMessage>();
                mimeMessages.Add(message);

                msg = BuildMimeMessageWithEmlAsRFC822Attachment(text, mimeMessages, From, ToList, CCList, BCCList);
            }
            catch (Exception ex)
            {
                bool rval = FileUtils.CreateWriteCloseFile(errorFilePath, "Building Mime Mesage Failed\n", ex.Message);
                logger.Log("Exception in Building Mime Message: ", ex.ToString());
                System.Environment.Exit(ExitCodes.ExitMimeMessage);
            }

            logger.Log("BuildMimeMessageWithEmlAsRFC822Attachment Done");

            //string msgAsString = MailkitConvert.ToString(msg);
            //string msgAsString = msg.ToString();
            //logger.Log("\n", msgAsString);

            // OAUTH2 works on Google but requires verification by Google and it seems to be chargable option if number of users > 100
            // Another problem is that ClientSecret can't be hardcoded in the application
            // For now we will just rely on User Account and User Password for authentication
            SaslMechanism oauth2    = null;;
            bool          useOAUTH2 = false;

            if (useOAUTH2)
            {
                string appClientId     = "xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com";
                string appClientSecret = "yyyyyyyyyyyyyyyyyyyyyyyyyyy";

                var accessScopes = new[]
                {
                    // that is the only scope that works per info from jstedfast
                    "https://mail.google.com/",
                };

                var clientSecrets = new ClientSecrets
                {
                    ClientId     = appClientId,
                    ClientSecret = appClientSecret
                };
                oauth2 = GetAuth2Token(smtpConfig.UserAccount, clientSecrets, accessScopes);
            }

            IProtocolLogger smtpProtocolLogger = null;

            if (protocolLoggerFilePath.Length > 0)
            {
                smtpProtocolLogger = new ProtocolLogger(protocolLoggerFilePath);
            }
            else
            {
                smtpProtocolLogger = new NullProtocolLogger();
            }

            using (var client = new SmtpClient(smtpProtocolLogger))
            {
                try
                {
                    client.Connect(smtpConfig.SmtpServerAddress, smtpConfig.SmtpServerPort, (SecureSocketOptions)smtpConfig.EncryptionType);
                }
                catch (Exception ex)
                {
                    bool rval = FileUtils.CreateWriteCloseFile(errorFilePath, "Connect to SMTP Server Failed\n", ex.Message);
                    logger.Log("Exception in Connect: ", ex.ToString());
                    System.Environment.Exit(ExitCodes.ExitConnect);
                }

                logger.Log(String.Format("Connected to {0} mail service", smtpConfig.MailServiceName));

                try
                {
                    if (useOAUTH2)
                    {
                        client.Authenticate(oauth2);
                    }
                    else
                    {
                        client.Authenticate(smtpConfig.UserAccount, smtpConfig.UserPassword);
                    }
                }
                catch (Exception ex)
                {
                    bool rval = FileUtils.CreateWriteCloseFile(errorFilePath, "SMTP Authentication Failed\n", ex.Message);
                    logger.Log("Exception in Authenticate: ", ex.ToString());
                    System.Environment.Exit(ExitCodes.ExitAuthenticate);
                }
                logger.Log("SMTP Authentication Succeeded");

                // Clear smtpConfig.UserPassword in case it cores
                smtpConfig.UserPassword = "";

                try
                {
                    client.Send(msg);
                }
                catch (Exception ex)
                {
                    bool rval = FileUtils.CreateWriteCloseFile(errorFilePath, "Send Failed\n", ex.Message);
                    logger.Log("Exception in Send to SMTP Server: ", ex.ToString());

                    //string msgString = MailkitConvert.ToString(msg);
                    //string msgAsString = msg.ToString();

                    // To help to investigate Warning place at the begining of the serialized MimeMessage
                    // X - MimeKit - Warning: Do NOT use ToString() to serialize messages! Use one of the WriteTo() methods instead!
                    //logger.Log("\n", msgString);

                    System.Environment.Exit(ExitCodes.ExitSend);
                }

                string txt = "Mail Sending Succeeded";
                logger.Log(txt);
                bool retval = FileUtils.CreateWriteCloseFile(okFilePath, txt);

                try
                {
                    client.Disconnect(true);
                }
                catch (Exception ex)
                {
                    // Ignore, not a fatal error
                    //bool rval = FileUtils.CreateWriteCloseFile(errorFilePath, "Send Failed\n", ex.Message);
                    logger.Log("Exception in Disconnect to SMTP Server: ", ex.ToString());
                }
                logger.Log("SMTP Client Disconnected. All done.");
            }
            System.Environment.Exit(ExitCodes.ExitOk);
        }