Powershell でメールを送る方法。のメモ。
#ホスト名 $hostname = hostname # 宛先メールアドレス $to = "to@example.com" # 送信元メールアドレス $from = "from@example.com" # SMTPサーバー(IP、ホスト名どちらでも) $smtp = "mail.example.com" # 件名 $subject = "件名" # 本文(コマンド) [string]$body = ((Get-Content -Path ファイル名.txt -ReadCount 0) -join "`r`n") # 添付ファイル $attache = (Convert-Path .) + "\添付ファイル.pdf" # メール送信コマンド Send-MailMessage -To $to -From $from -SmtpServer $smtp -Subject $subject -Body $body -Encoding UTF8 -Attachments $attache
認証が必要な場合は-Credential
を使う
$user = "ユーザーID" $pass = (ConvertTo-SecureString "パスワード" -AsPlainText -Force) $Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $pass Send-MailMessage -To $to -From $from -SmtpServer $smtp -Subject $subject -Body $body -Credential $Credentials