2016-09-12 3 views
-1

나는 powershell을 통해 사람들에게 전자 메일을 보내려는 스크립트 작업을하고 있습니다. 그것은 어떤 컴퓨터에서는 잘 보내지 만 다른 컴퓨터에서는 전혀 보내지 않습니다. 두 컴퓨터가 똑같이 구성되어 있지 않다는 것을 압니다.powershell을 통해 전자 메일 보내기

나는 이유를 추측 할 수 없다.

명령 끝에 2 >> c : \ error.txt를 추가하려고 시도했지만 아무 것도 기록하지 않습니다.

내 코드 (I는 기밀을 이유로 SMTP 이메일을 체인저) :

  File.Delete(@"C:\Temp\Email2Send.ps1"); 
      var strVar_EMAIL_BOX = "[email protected]"; 
      TextWriter tw = new StreamWriter(@"C:\Temp\Email2Send.ps1"); 
      tw.WriteLine("$OutputEncoding = [Console]::OutputEncoding"); 
      tw.WriteLine("$smtp = \"forwarder.mail.xxxxxx.com\""); 
      tw.WriteLine("$to = \"" + To + "\""); 
      tw.WriteLine("$from = \"" + strVar_EMAIL_BOX + "\""); 
      tw.WriteLine("$enc = [System.Text.Encoding]::UTF8"); 
      tw.WriteLine("$subject = \"Suivi\""); 
      tw.WriteLine("$body = \"" + Body + "\""); 
      tw.WriteLine(@"send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Encoding $enc 2>> c:\errors.txt"); 
      tw.Close(); 
     } 

     System.Diagnostics.Process process = new System.Diagnostics.Process(); 
     System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); 
     startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
     startInfo.FileName = "cmd.exe"; 
     startInfo.Arguments = @"/C powershell -ExecutionPolicy Bypass -WindowStyle Hidden -command c:\Temp\Email2Send.ps1"; 
     process.StartInfo = startInfo; 
     process.Start(); 

편집 : 신경 끄시 고 내가 게시 한 후 솔루션을 2 초 발견했다. 어떤 이유로 Powershell이 ​​Windows 경로의 일부 컴퓨터에 올바르게 연결되어 있지 않습니다. PowerShell을 다음으로 변경했습니다. % SYSTEMROOT % \ System32 \ WindowsPowerShell \ v1.0 \ powershell.exe

+1

왜 cmd를 사용하여 powershell을 실행하고 있습니까? powershell은 이미 실행 파일입니다. PowerShell을 실행하기 위해 cmd를 실행하는 것은 불필요합니다. –

+1

C#에서 완벽하게 좋은 [SmtpClient] (https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient (v = vs.110))가있을 때 C#을 사용하여 PowerShell을 통해 전자 메일을 보내는 이유는 무엇입니까?) .aspx) 클래스? – itsme86

+0

네,이 네트워크에서 C#으로 이메일 전송이 차단되었으므로 Powershell을 사용합니다. 그것은 단지 이상합니다 ... – Guillaume

답변

0

게시 후 2 초가 지나면 해결책을 찾았습니다. 어떤 이유로 Powershell이 ​​Windows 경로의 일부 컴퓨터에 올바르게 연결되어 있지 않습니다. PowerShell을 다음과 같이 변경했습니다 : % SYSTEMROOT % \ System32 \ WindowsPowerShell \ v1.0 \ powershell.exe

관련 문제