An abstract mail service implementation.
An abstract mail service implementation.
Inheritance: IMailService
 private void Connect(MailService client) {
     client.Connect(_mailBox.Host, _mailBox.Port, _mailBox.UseSsl);
     if (client.AuthenticationMechanisms.Contains("XOAUTH2")) {
         client.AuthenticationMechanisms.Remove("XOAUTH2");
     }
     client.Authenticate(_mailBox.Username, Cryptography.Decrypt(_mailBox.Password));
 }
Exemple #2
0
    public bool Start(Topshelf.HostControl hc)
    {
        // ログリスナーの設定 (コンソールアプリケーションとして起動した場合はコンソール、サービスとして起動した場合はイベントログ)
        if (hc is Topshelf.Hosts.ConsoleRunHost)
        {
            Trace.Listeners.Add(new ConsoleTraceListener());
        }
        else
        {
            Trace.Listeners.Add(new EventLogTraceListener(MailToStarPRNT.AppName));
        }

        Trace.TraceInformation("アプリケーションを開始します。\nStarPRNT SDK version " + StarMicronics.StarIO.Factory.I.GetStarIOVersion());

        // 設定ファイル読み込み
        var iniprop = LoadIniFile("MailToStarPRNT.ini");

        // メール受信インスタンス初期化
        string mail_protocol;

        if (iniprop.TryGetValue("MailProtocol", out mail_protocol))
        {
            if (Regex.IsMatch(mail_protocol, "IMAP4?", RegexOptions.IgnoreCase))
            {
                this.mailclient = new MailKit.Net.Imap.ImapClient();
            }
            else if (Regex.IsMatch(mail_protocol, "POP3?", RegexOptions.IgnoreCase))
            {
                this.mailclient = new MailKit.Net.Pop3.Pop3Client();
            }
            else
            {
                Trace.TraceError("MailProtocolの設定値が正しくありません。IMAPかPOPを指定してください。アプリケーションをエラー終了します。");
                hc.Stop();
                return(false);
            }
        }
        else
        {
            this.mailclient = new MailKit.Net.Pop3.Pop3Client();
        }

        if (!iniprop.TryGetValue("MailServerName", out this.mail_server_name))
        {
            Trace.TraceError("必須設定であるMailServerNameが設定されていません。アプリケーションをエラー終了します。");
            hc.Stop();
            return(false);
        }
        if (!iniprop.TryGetValue("MailServerUserName", out this.mail_server_username))
        {
            Trace.TraceError("必須設定であるMailServerUserNameが設定されていません。アプリケーションをエラー終了します。");
            hc.Stop();
            return(false);
        }
        if (!iniprop.TryGetValue("MailServerPassword", out this.mail_server_password))
        {
            Trace.TraceError("必須設定であるMailServerPasswordが設定されていません。アプリケーションをエラー終了します。");
            hc.Stop();
            return(false);
        }
        if (iniprop.TryGetValue("MailServerSSL", out string mail_server_ssl_str))
        {
            if (Regex.IsMatch(mail_server_ssl_str, @"^Yes$", RegexOptions.IgnoreCase))
            {
                this.mail_server_ssl = true;
            }
            else if (Regex.IsMatch(mail_server_ssl_str, @"^No$", RegexOptions.IgnoreCase))
            {
                this.mail_server_ssl = false;
            }
            else
            {
                Trace.TraceError("MailServerSSLの設定値はYesかNoである必要があります。アプリケーションをエラー終了します。");
                hc.Stop();
                return(false);
            }
        }
        if (iniprop.TryGetValue("MailServerPort", out string mail_server_port_number_str))
        {
            if (!UInt16.TryParse(mail_server_port_number_str, out this.mail_server_port_number))
            {
                Trace.TraceError("MailServerPortNumberの設定値は0以上65535以下の整数である必要があります。アプリケーションをエラー終了します。");
                hc.Stop();
                return(false);
            }
        }
        else
        {
            // 設定ファイルにポート番号が設定されていない場合は、デフォルトポート番号を使用する。
            if (this.mailclient is MailKit.Net.Imap.ImapClient && this.mail_server_ssl)
            {
                this.mail_server_port_number = 993;
            }
            else if (this.mailclient is MailKit.Net.Imap.ImapClient && !this.mail_server_ssl)
            {
                this.mail_server_port_number = 143;
            }
            else if (this.mailclient is MailKit.Net.Pop3.Pop3Client && this.mail_server_ssl)
            {
                this.mail_server_port_number = 995;
            }
            else if (this.mailclient is MailKit.Net.Pop3.Pop3Client && !this.mail_server_ssl)
            {
                this.mail_server_port_number = 110;
            }
        }
        iniprop.TryGetValue("MailPrintFromCondition", out this.mail_from_condition);
        iniprop.TryGetValue("MailPrintSubjectCondition", out this.mail_subject_condition);
        iniprop.TryGetValue("MailPrintBodyCondition", out this.mail_body_condition);

        if (iniprop.TryGetValue("PrintStartStopMessage", out string print_on_start_stop_str))
        {
            if (Regex.IsMatch(print_on_start_stop_str, @"^Yes$", RegexOptions.IgnoreCase))
            {
                this.print_on_start_stop = true;
            }
            else if (Regex.IsMatch(print_on_start_stop_str, @"^No$", RegexOptions.IgnoreCase))
            {
                this.print_on_start_stop = false;
            }
            else
            {
                Trace.TraceError("PrintStartStopMessageの設定値はYesかNoである必要があります。アプリケーションをエラー終了します。");
                hc.Stop();
                return(false);
            }
        }
        if (iniprop.TryGetValue("PrintErrors", out string print_on_error_str))
        {
            if (Regex.IsMatch(print_on_error_str, @"^Yes$", RegexOptions.IgnoreCase))
            {
                this.print_on_error = true;
            }
            else if (Regex.IsMatch(print_on_error_str, @"^No$", RegexOptions.IgnoreCase))
            {
                this.print_on_error = false;
            }
            else
            {
                Trace.TraceError("PrintErrorsの設定値はYesかNoである必要があります。アプリケーションをエラー終了します。");
                hc.Stop();
                return(false);
            }
        }

        // StarPRNTプリンターの初期化
        string port_name        = "";
        string model_name       = "";
        string firmware_version = "";

        if (!iniprop.TryGetValue("StarPRNTPortName", out port_name))
        {
            List <StarMicronics.StarIO.PortInfo> printers = StarMicronics.StarIO.Factory.I.SearchPrinter();

            printers.ForEach(printer =>
            {
#if DEBUG
                Console.WriteLine("================================");
                Console.WriteLine("PortName: " + printer.PortName);
                Console.WriteLine("ModelName: " + printer.ModelName);
                Console.WriteLine("MacAddress: " + printer.MacAddress);
                Console.WriteLine("USBSerialNumber: " + printer.USBSerialNumber);
#endif
                if (printer.ModelName != "")
                {
                    port_name = printer.PortName;
                    return;
                }
            });
            if (port_name == "")
            {
                Trace.TraceError("StarPRNTプリンターが見つかりませんでした。アプリケーションをエラー終了します。");
                hc.Stop();
                return(false);
            }
        }

        // StarPRNTプリンターへの接続開始
        try
        {
            int print_timeout = 10000;
            if (iniprop.TryGetValue("StarPRNTTimeOutSeconds", out string print_timeout_str))
            {
                if (int.TryParse(print_timeout_str, out print_timeout))
                {
                    print_timeout *= 1000; // ミリ秒単位に修正
                }
                else
                {
                    print_timeout = 10000;
                }
            }

            this.starprnt_port = StarMicronics.StarIO.Factory.I.GetPort(port_name, "", print_timeout);
            var devinfo = this.starprnt_port.GetFirmwareInformation();
            model_name       = devinfo["ModelName"];
            firmware_version = devinfo["FirmwareVersion"];
            Trace.TraceInformation("次のStarPRNTプリンターを使用します。\nモデル名: " + model_name + ", ファームウェア: " + firmware_version + ", 通信ポート: " + port_name);
        }
        catch (StarMicronics.StarIO.PortException e)
        {
            Trace.TraceError("StarPRNTプリンターに接続できませんでした。アプリケーションをエラー終了します。\n" + e.Message);
            hc.Stop();
            return(false);
        }

        // StarPRNTエミュレーションモードの設定
        if (iniprop.TryGetValue("StarPRNTEmulation", out string emulation_str))
        {
            switch (emulation_str)
            {
            case "StarGraphic":
                this.emulation = StarMicronics.StarIOExtension.Emulation.StarGraphic;
                break;

            case "StarLine":
                this.emulation = StarMicronics.StarIOExtension.Emulation.StarLine;
                break;

            case "StarPRNT":
                this.emulation = StarMicronics.StarIOExtension.Emulation.StarPRNT;
                break;

            case "StarPRNTL":
                this.emulation = StarMicronics.StarIOExtension.Emulation.StarPRNTL;
                break;

            case "StarDotImpact":
                this.emulation = StarMicronics.StarIOExtension.Emulation.StarDotImpact;
                break;

            case "EscPos":
                this.emulation = StarMicronics.StarIOExtension.Emulation.EscPos;
                break;

            case "EscPosMobile":
                this.emulation = StarMicronics.StarIOExtension.Emulation.EscPosMobile;
                break;

            default:
                Trace.TraceError("StarPRNTEmulationの設定値が正しくありません。アプリケーションをエラー終了します。");
                hc.Stop();
                return(false);
            }
        }
        else
        {
            this.emulation = GuessStarPRNTEmulation(model_name);
        }
#if DEBUG
        Trace.WriteLine("StarPRNT Emulation Mode: " + this.emulation.ToString());
#endif

        // 起動時メッセージを印刷
        if (this.print_on_start_stop)
        {
            if (!SendStringToStarPRNT("★" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " 自動印刷を開始します★\n\n\n\n\n\n\n\n\n\n"))
            {
                Trace.TraceError("StarPRNTプリンターへの初期化メッセージの送信時にエラーが発生しました。アプリケーションをエラー終了します。");
                hc.Stop();
                return(false);
            }
        }

        // メールログイン試行
        PrintMails(true);

        // メールチェックタイマー設定・開始
        int mail_check_interval = 30000;
        if (iniprop.TryGetValue("MailCheckIntervalSeconds", out string mail_check_interval_str))
        {
            if (int.TryParse(mail_check_interval_str, out mail_check_interval))
            {
                mail_check_interval *= 1000; // ミリ秒単位に修正
            }
            else
            {
                mail_check_interval = 30000;
            }
        }
        timer.Interval = mail_check_interval;
        timer.Start();

        return(true);
    }