Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “何时检测”;Office.Interop.Outlook“;don';不要发邮件_C#_Outlook_Interop_Office Interop_Com Interop - Fatal编程技术网

C# “何时检测”;Office.Interop.Outlook“;don';不要发邮件

C# “何时检测”;Office.Interop.Outlook“;don';不要发邮件,c#,outlook,interop,office-interop,com-interop,C#,Outlook,Interop,Office Interop,Com Interop,我有一个发送邮件的c#程序。 该过程调用mailItem.send()时很少会出错,但Outlook不会发送邮件,并且邮件也不会在Outlook的“to send”文件夹中创建 如何检测这个 代码如下: private void sendMail(String mail, String description) { RegistryKey key = Registry.ClassesRoot; RegistryKey subKey = key.OpenSubKey("Outlo

我有一个发送邮件的c#程序。 该过程调用mailItem.send()时很少会出错,但Outlook不会发送邮件,并且邮件也不会在Outlook的“to send”文件夹中创建

如何检测这个

代码如下:

private void sendMail(String mail, String description)
{

    RegistryKey key = Registry.ClassesRoot;
    RegistryKey subKey = key.OpenSubKey("Outlook.Application");

    if (subKey != null)
    {


        if ((Process.GetProcessesByName("Outlook").Length == 0) && (Process.GetProcessesByName("Outlook.exe").Length == 0))
        {
            System.Diagnostics.Process.Start("Outlook");
        }


        Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
        Microsoft.Office.Interop.Outlook.MailItem mailItem = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
        mailItem.Subject = "Release Notice";
        mailItem.To = mail;

        String bodyMessage = description;

        mailItem.Body = bodyMessage;

        mailItem.Display(false);
        mailItem.Send();

    }
    else
    {
        MessageBox.Show("Impossible to send mails. Contact system administrator.", "System Info", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
如果
send()
方法未发送任何错误或异常,则很可能无法检测是否存在任何错误/异常(至少从代码中)

将邮件移动到Outlook服务器后,您将无法检查其状态。您必须依靠Outlook服务器发送电子邮件


您可以参考Jeff Atwood的这篇文章:

尝试用try/catch来包围send方法,或者
mailItem.send()
给您一个返回值?如果mailItem.send()没有失败,那么我会检查电子邮件的发件箱,以确保它可以发送。@M.Schena mailItem.send()是无效方法