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 = "[email protected]";
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");
}
}
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.