Reputation: 25
$smtpServer = "smtp.gmail.com"
$smtpport = "465"
$username = "[email protected]"
$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 = "[email protected]"
$msg.Subject = "Local Environment DiskSpace Report for $titledate"
$msg.IsBodyHTML = $true
$msg.Body = get-content $diskReport
$smtp.Send($msg)
$body = ""
Here user is must be define in other way like
$users = "[email protected]"
But while run file of powershell bat file getting error:
$smtp.EnableSSL = $true
CategoryInfo : InvalidOperation:<:>[],RuntimeException
FullyQualifiedErrorId : PropertyNotFound
Same error also on :
$smtp.credentials = New-Object System.Net.NetworkCredential($Username, $Password);
Please solution on this.
Upvotes: 1
Views: 469
Reputation: 114481
First new the object, then set the properties:
$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);
Upvotes: 2