简体   繁体   中英

SSL credentials for sending mail from gmail account in powershell code

 $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 = ""

Here user is must be define in other way like

    $users = "scondgmailid@gmail.com"

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.

首先新建对象,然后设置属性:

$smtp.EnableSSL = $true $smtp.credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM