0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Power Shell】メール送信(Outlook/Gmail)

Last updated at Posted at 2022-08-25

Powershellでメール送信

Powershellでメール送信を行う方法を紹介致します。
私は、プログラムの実行結果の取得や特定の時間にメール送信を行いたい場合などに使用しています。

SmtpClient/Send-MailMessageを使った方法を記載しています。

※Send-MailMessageでPort指定を行えない場合は、SmtpClientを使う事があります。
上記二つではなく、現在はMailKit等でメールする方法が推奨されているようです。(職場では諸事情で使えず、、)

下記スクリプトでメールを送信できます。

SmtpClient

$Address = "メールアドレス"  
$Subject = "件名"  
$Body = "本文"  

$SmtpServer = "smtp.office365.com"  Or "smtp.gmail.com"
$SmtpClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)  
$SmtpClient.EnableSsl = $true
$SmtpClient.Credentials = New-Object System.Net.NetworkCredential($Address, "パスワード")
$SmtpClient.Send($Address, $Address, $Subject, $Body) 

Send-MailMessage

$Address = "メールアドレス"
$Subject = "件名"  
$Body = "本文"
$SmtpServer = "smtp.office365.com"  Or "smtp.gmail.com"
$Pass = ConvertTo-SecureString "パスワード" -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential $Address,$Pass

Send-MailMessage  `
    -Encoding UTF8 `
    -From $Address `
    -to $Address `
    -subject $Subject `
    -body $Body `
    -smtpServer $SmtpServer `
    -Port 587 `
    -Credential $Credential `
    -UseSsl 
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?