2013-05-16 44 views
2

我編寫了一個C#.net可執行文件,該文件通過Outlook交換服務器發送電子郵件。當我手動運行它時,一切正常,但是當我使用計劃任務調用可執行文件時,它不會發送電子郵件。其他一切正常,但電子郵件不會被髮送。我將計劃任務設置爲以我的用戶帳戶運行。任務運行時,我可以在任務管理器中看到可執行文件正在我的用戶名下運行。這排除了任何明顯的權限問題。由任務計劃程序調用時,可執行文件無法發送電子郵件

在調試過程中,我使程序輸出一些文本到Exchange上運行的同一臺計算機上的網絡共享上的文件。這個文件輸出很好,所以我知道該程序可以連接到該機器。

任何人都可以幫忙嗎?

+0

SMTPClient的外觀如何。特別向我們展示了client.UseDefaultCredentials,client.Credentials和client.DeliveryMethod的一般構造。是DeliveryMethod SmtpDeliveryMethod.Network?需要一些代碼。 – ApolloSoftware

+0

感謝您的快速回復,@AmitApollo。 Outlook.Application myOutlook = new Outlook.Application(); Outlook.MailItem mailMessage =(Outlook.MailItem)myOutlook.CreateItem(Outlook.OlItemType.olMailItem); Outlook.NameSpace olNameSpace = myOutlook.GetNamespace(「MAPI」); Outlook.Accounts olAccounts = olNameSpace.Accounts; Outlook.Account myAccount = new Outlook.Account(); (olAccounts中的Outlook.Account olAccount){if(olAccount.SmtpAddress ==「[email protected]」){myAccount = olAccount; }} – user2320861

+0

我似乎在這個評論框中格式化代碼時遇到了問題......我幾乎沒有發佈就做了回車。無論如何,我會繼續挖掘。 – user2320861

回答

1

好的,正如你上面看到的,我試圖通過運行的Outlook實例發送郵件。儘管我不能在沒有評論框的情況下發布代碼,但是沒有拉出我的頭髮@amitapollo給了我使用System.Net.Mail命名空間的線索。在一天結束的時候,我得到了它的工作。以下是我的代碼:

System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient("myExchangeServerIPAddress"); 
     smtpClient.UseDefaultCredentials = false; 
     smtpClient.Credentials = new System.Net.NetworkCredential("myDomain\\myUsername", "myPassword"); 
     smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;    
     smtpClient.EnableSsl = true; 

System.Security.Cryptography.X509Certificates.X509Store xStore = new System.Security.Cryptography.X509Certificates.X509Store(); 
     System.Security.Cryptography.X509Certificates.OpenFlags xFlag = System.Security.Cryptography.X509Certificates.OpenFlags.ReadOnly; 
     xStore.Open(xFlag); 
     System.Security.Cryptography.X509Certificates.X509Certificate2Collection xCertCollection = xStore.Certificates; 
     System.Security.Cryptography.X509Certificates.X509Certificate xCert = new System.Security.Cryptography.X509Certificates.X509Certificate(); 
     foreach (System.Security.Cryptography.X509Certificates.X509Certificate _Cert in xCertCollection) 
     { 
      if (_Cert.Subject.Contains("[email protected]")) 
      {      
       xCert = _Cert; 
      } 
     } 

smtpClient.ClientCertificates.Add(xCert); 

//I was having problems with the remote certificate no being validated so I had to override all security settings with this line of code... 

System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object s, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { return true; }; 
smtpClient.Send("[email protected]", "[email protected]", "mySubject", "myBody"); 
相關問題