獲取當前Server的公網IP地址,並郵件發送

#mail server configuration
$smtpServer = "smtp.XXXX.com"
$smtpuser = "[email protected]"
$smtpPassword = "Oa123456"
#Create the mail message 
$mail = New-Object System.Net.Mail.MailMessage
#Set the Addresses
$mailaddress = "[email protected]"
$users = @()
$users = Import-Csv -Path "c:\user.csv"
foreach ($user in $users)
{
    #$user = $users -split','
    $mail.from = New-Object System.Net.Mail.MailAddress($mailaddress)
    $mailtoaddress = $user.mailaddresslist
    $mail.to.Add($mailtoaddress)
    }
    #Detection the Public IP
    $ipurl = Invoke-WebRequest 'http://myip.dnsomatic.com' -UseBasicParsing
    $PublicIp = $ipurl.content 
    #set send mail
    $mail.Subject = "PowerShell Detection of Demo Server for the Public Network IP Address:"
    $mail.Priority = "High"
    $Nowtime= get-date -Format "yyyy.MM.dd-HH.mm.ss"
    $mail.Body = "日期\時間:$Nowtime  演示服務器的公網IP地址:$publicip"
    #send the message
    $smtp = New-Object System.Net.Mail.SmtpClient -ArgumentList $smtpServer
    $smtp.Credentials = New-Object System.Net.NetworkCredential -ArgumentList $smtpuser,$smtpPassword
    $smtp.Send($mail)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章