从命令提示符使用powershell发送电子邮件

时间:2018-04-22 07:32:41

标签: powershell command-prompt

我使用此PowerShell脚本发送电子邮件:

param($EmailFrom,$EmailTo,$Subject,$Body)

$Username = 'user@gmail.com';
$Password = 'pass';
$emailMsg = New-Object System.Net.Mail.MailMessage
$emailMsg.From = $EmailFrom
$emailMsg.To.Add($EmailTo)
$emailMsg.Subject = $Subject
$emailMsg.Body = $Body
$smtpClient = New-Object System.Net.Mail.SmtpClient
$smtpClient.Host = 'smtp.gmail.com'
$smtpClient.Port = 587
$smtpClient.EnableSsl = $true
$smtpClient.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtpClient.Send($emailMsg);
write-host "Mail Sent"

从这样的命令提示符调用它(我发送电子邮件给自己,然后进入我的收件箱):

powershell -command "& {C:\path\sendGmailPSS.ps1 -EmailFrom mail -EmailTo mail2 -Subject 'test' -Body 'test'}"

但是把它放在这样的bat文件中:

@echo off

powershell -command "& {C:\path\sendGmailPSS.ps1 -EmailFrom %1 -EmailTo %2 -Subject '%~3' -Body '%~4'}"

并将其称为:

sendGmail.bat mail mail2 "test" "test"

不起作用(我将电子邮件发送给自己,即使在cmd中我收到" Mail Sent"消息,我的收件箱中也没有任何内容)

我做错了什么?

0 个答案:

没有答案