I'm using the following command to send an automated email on Powershell.
powershell -Command {$to = "myemail@mail.com"; $from = "senderemail@mail.com"; $subject = "subject goes here"; $body = "message body goes here"; $smtpServer = "smtp.server.com"; $smtpClient = New-Object Net.Mail.SmtpClient($smtpServer, 587); $smtpClient.EnableSsl = $true; $smtpClient.Credentials = New-Object System.Net.NetworkCredential("senderemail@mail.com", "Dnd912n9d2n1dsda90"); $smtpClient.Send($from, $to, $subject, $body);}";
Here it works. So now I want to use this code on a private way (avoiding showing passwords and emails).
So I made a console app with C# to open Powershell and execute the command, but it's not working.
strCmdText = "{$to = \"myemail@mail.com\"; $from = \"senderemail@mail.com\"; $subject = \"subject goes here\"; $body = \"message body goes here\"; $smtpServer = \"smtp.server.com\"; $smtpClient = New-Object Net.Mail.SmtpClient($smtpServer, 587); $smtpClient.EnableSsl = $true; $smtpClient.Credentials = New-Object System.Net.NetworkCredential(\"senderemail@mail.com\", \"Dnd912n9d2n1dsda90\"); $smtpClient.Send($from, $to, $subject, $body);}";
using var ps = PowerShell.Create();
var processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = "powershell.exe";
processStartInfo.Arguments = " powershell-Command " + strCmdText;
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardOutput = true;
using var process = new Process();
process.StartInfo = processStartInfo;
process.Start();
I'm not receiving any errors on visual studio. if I open .exe it closes inmediatly as if it would be working. But no email is sent or received.
I want to do it by this way because of the privacy the command would have avoiding passwords and emails being shown. Program has to activate when X conditions on computer happen, so it has to execute easy. Say that if I execute the command on CMD, i receive a different error, that's why I'm doing it on powershell.