Я пытаюсь отправить электронное письмо из кода c ++, используя скрипт powershell, как показано ниже
string sSubject = "Test Mail";
string sPsCmd = "Powershell.exe -executionpolicy remotesigned -File ";
string sMailBody = "Please find attachment";
string sFile = "C:\temp\test.txt";
string sMailPsFile = "C:\temp\Mail.ps1"
string sUsermailID = test@test.com
string sCmd = sPsCmd+sMailPsFile+" "+sUsermailID+" "+sFile+" "+sSubject+" "+sMailBody;
system(sCmd.c_str());`
Скрипт Powershell
param(
[string]$MailtTo,
[string]$FileToSend ,
[string]$MailSubject,
[string]$Body
)
$SmtpServer = 'smtp.server'
$SmtpPort = '25'
$SmtpUser = 'testUser'
$smtpPassword = 'password'
$MailFrom = 'test@test.com'
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $SmtpUser,
$($smtpPassword | ConvertTo-SecureString -AsPlainText -Force)
$MailCount=0
Send-MailMessage -To $MailtTo -from $MailFrom -Subject $MailSubject -Body $Body -Attachment
$FileToSend -SmtpServer $SmtpServer -Port $SmtpPort -UseSsl -Credential $Credentials
$Host.Exit
Но скрипт принимает параметры, указанные ниже
param1 MailtTo = test@test.com
param2 FileToSend = filePath
param3 MailSubject = Test
param4 Body = Mail