$smtpServer = "smtp.gmail.com"
$smtpport = "465"
$username = "gmailid@gmail.com"
$password = "gmailpassword"
$smtp.EnableSSL = $true
$smtp.credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$msg = New-Object Net.Mail.MailMessage
$msg.To.Add($user)
$msg.From = "gmailid@gmail.com"
$msg.Subject = "Local Environment DiskSpace Report for $titledate"
$msg.IsBodyHTML = $true
$msg.Body = get-content $diskReport
$smtp.Send($msg)
$body = ""
这里的用户必须用其他方式定义,比如
$users = "scondgmailid@gmail.com"
但是在运行 powershell bat 文件时出现错误:
$smtp.EnableSSL = $true
CategoryInfo : InvalidOperation:<:>[],RuntimeException
FullyQualifiedErrorId : PropertyNotFound
同样的错误也发生在:
$smtp.credentials = New-Object System.Net.NetworkCredential($Username, $Password);
请解决这个问题。
最佳答案
首先新建对象,然后设置属性:
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
关于email - 用于在 powershell 代码中从 gmail 帐户发送邮件的 SSL 凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31470666/