以下PowerShell命令发送电子邮件。我如何在PowerBuilder中使用这些命令?
$EmailFrom = "[email protected]"
$EmailTo = "[email protected]"
$Subject = "Notification from XYZ"
$Body = "this is a notification from XYZ Notifications.."
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("[email protected]", "mypass123");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
您可能想在旧的SAP PowerBuilder论坛上查看来自Bruce Armstrong的this post。它显示了如何调用Powershell.exe并让它从文本文件中执行命令。
我使用了Matt在链接中给出的类似方法。它对我有用。
/// Powershell commands to send email with attachment are saved in a file and that file is given to
/// powershell executeable file as parameter
Integer li_FileNum
String theCmd
li_FileNum = FileOpen("C:\Temp\cmd.ps1", LineMode!, Write!, LockWrite!, Replace!)
theCmd = "$fromaddress = ~""+SendFrom.Text+"~""
FileWrite(li_FileNum, theCmd)
theCmd = "$toaddress = ~""+SendTo.Text+"~""
FileWrite(li_FileNum, theCmd)
///theCmd = "$bccaddress = ~""+"BCC"+"~""
///theCmd = "$CCaddress = ~""+"CC"+"~""
theCmd = "$Subject = ~""+Subject.Text+"~""
FileWrite(li_FileNum, theCmd)
theCmd = "$body = ~""+Msg.Text+"~""
FileWrite(li_FileNum, theCmd)
theCmd = "$attachment = ~""+TheFile.Text+"~""
FileWrite(li_FileNum, theCmd)
theCmd = "$smtpserver = ~"smtp.gmail.com~""
FileWrite(li_FileNum, theCmd)
theCmd = "$message = new-object System.Net.Mail.MailMessage"
FileWrite(li_FileNum, theCmd)
theCmd = "$message.From = $fromaddress"
FileWrite(li_FileNum, theCmd)
theCmd = "$message.To.Add($toaddress)"
FileWrite(li_FileNum, theCmd)
//theCmd = "$message.CC.Add($CCaddress)"
//FileWrite(li_FileNum, theCmd)
//theCmd = "$message.Bcc.Add($bccaddress)"
//FileWrite(li_FileNum, theCmd)
theCmd = "$message.IsBodyHtml = $False"
FileWrite(li_FileNum, theCmd)
theCmd = "$message.Subject = $Subject"
FileWrite(li_FileNum, theCmd)
theCmd = "$attach = new-object Net.Mail.Attachment($attachment)"
FileWrite(li_FileNum, theCmd)
theCmd = "$message.Attachments.Add($attach)"
FileWrite(li_FileNum, theCmd)
theCmd = "$message.body = $body"
FileWrite(li_FileNum, theCmd)
theCmd = "$smtp = new-object Net.Mail.SmtpClient($smtpserver)"
FileWrite(li_FileNum, theCmd)
theCmd = "$smtp.EnableSsl = $true"
FileWrite(li_FileNum, theCmd)
theCmd = "$smtp.Credentials = New-Object System.Net.NetworkCredential(~""+SendFrom.Text+"~", ~""+Password.Text+"~");"
FileWrite(li_FileNum, theCmd)
theCmd = "$smtp.Send($message)"
FileWrite(li_FileNum, theCmd)
FileClose(li_FileNum)
int li_rc
string ls_command
string ls_directory
ls_directory = "C:\temp"
ls_command = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "&'
ls_command += " '" + ls_directory + "\cmd.ps1' "
li_rc = Run ( ls_command, Minimized! )