Unhandled Exception: System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list

huangapple go评论65阅读模式
英文:

Unhandled Exception: System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list

问题

以下是您要翻译的代码部分:

Hey I have this error when I try to send an email but only when I publish the app:

> Unhandled Exception: System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
> at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)                                                                                                                                                                    at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
> at System.IO.TextWriter.WriteLine(String format, Object arg0)
> at System.IO.TextWriter.SyncTextWriter.WriteLine(String format, Object arg0)
> at System.Console.WriteLine(String format, Object arg0)
> at Impersonation.test.EnviarMail()
> at ImpersonationTest.test.Main(String\[\] args)

My code:

                using (StreamReader reader = new StreamReader(FilePath))
                {
                    string MailText = reader.ReadToEnd();
                    reader.Close();
                    try
                    {
                        //Using COM to send email
                        Outlook._Application _app = new Outlook.Application();
                        Outlook.MailItem mail =  (Outlook.MailItem)_app.CreateItem(Outlook.OlItemType.olMailItem);
    
                        //Remplaza el texto por otro que se define manualmente
                        MailText = MailText.Replace("[Fecha]", Convert.ToString(dateTime));
                        MailText = MailText.Replace("[Cantidad]", Convert.ToString(VariablesGlobals.OpMgr)+" de "+Convert.ToString(VariablesGlobals.Dir));
                        //FALTA Agregar los correos correspondientes To & CC
                        mail.To = "correo.prueba@outlook.com"
    
                        mail.Subject = "Actualización Programa [DataInformer] " + mes + ", " + Convert.ToString(dia);
                        mail.HTMLBody = MailText;
                        mail.Importance = Outlook.OlImportance.olImportanceHigh;//Cambiar la importancia a alta                                                                           
    
                        ((Outlook._MailItem)mail).Send();
                        Console.WriteLine("Your message has been successfully sent.", "Message");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message, "Message");
                    }
    
                }
英文:

Hey I have this error when I try to send an email but only when I publish the app:

> Unhandled Exception: System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
> at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args) at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
> at System.IO.TextWriter.WriteLine(String format, Object arg0)
> at System.IO.TextWriter.SyncTextWriter.WriteLine(String format, Object arg0)
> at System.Console.WriteLine(String format, Object arg0)
> at Impersonation.test.EnviarMail()
> at ImpersonationTest.test.Main(String[] args)

My code:

            using (StreamReader reader = new StreamReader(FilePath))
            {
                string MailText = reader.ReadToEnd();
                reader.Close();
                try
                {
                    //Using COM to send email
                    Outlook._Application _app = new Outlook.Application();
                    Outlook.MailItem mail =  (Outlook.MailItem)_app.CreateItem(Outlook.OlItemType.olMailItem);

                    //Remplaza el texto por otro que se define manualmente
                    MailText = MailText.Replace("[Fecha]", Convert.ToString(dateTime));
                    MailText = MailText.Replace("[Cantidad]", Convert.ToString(VariablesGlobals.OpMgr)+" de "+Convert.ToString(VariablesGlobals.Dir));
                    //FALTA Agregar los correos correspondientes To & CC
                    mail.To = "correo.prueba@outlook.com";

                    mail.Subject = "Actualización Programa [DataInformer] " + mes + ", " + Convert.ToString(dia);
                    mail.HTMLBody = MailText;
                    mail.Importance = Outlook.OlImportance.olImportanceHigh;//Cambiar la importancia a alta                                                                           

                    ((Outlook._MailItem)mail).Send();
                    Console.WriteLine("Your message has been successfully sent.", "Message");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message, "Message");
                }

            }

答案1

得分: 1

检索具有CLSID [CLSID] 的组件的COM类工厂失败,原因是以下错误:80040154

主机应用程序必须安装在尝试自动化它的计算机上。

请注意,微软目前不建议,也不支持从任何非交互式客户端应用程序或组件(包括ASP、ASP.NET、DCOM和NT服务)自动化微软Office应用程序,因为在这种环境中运行Office可能会表现出不稳定的行为和/或死锁。

如果您正在构建在服务器端上下文中运行的解决方案,您应该尝试使用已经为非交互式执行而设计的组件。或者,您应该尝试寻找允许至少部分代码在客户端运行的替代方法。如果您从服务器端解决方案中使用Office应用程序,该应用程序将缺少成功运行所需的许多功能。此外,您将冒整体解决方案稳定性的风险。有关详细信息,请参阅Office服务器端自动化的注意事项文章。

英文:

> Retrieving the COM class factory for component with CLSID [CLSID] failed due to the following error: 80040154

The host application must be installed on the machine where you try to automate it.

Be aware, Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

huangapple
  • 本文由 发表于 2023年3月31日 04:52:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定