Niket Patil
Niket Patil

Reputation: 25

SSL credentials for sending mail from gmail account in powershell code

 $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

Answers (1)

jessehouwing
jessehouwing

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

Related Questions