PowerShell

[PowerShell]メール送信用関数

本記事で使用しているSmtpClientは2018年現在非推奨となっています。
下記記事で紹介しているMailKitをご使用ください。


PowerShellを使ってメール送信する機会が増えてきたので、処理を関数化してみました。

function sendMail(
    $UserID,                            #ユーザーID
    $UserPassword,                      #パスワード
    $MailFrom,                          #From
    $MailToAry,                         #To(配列で指定)
    $MailCcAry,                         #Cc(配列で指定)
    $MailBccAry,                        #Bcc(配列で指定)
    $MailSubject,                       #件名
    $MailBody,                          #本文
    $MailAtthPathAry,                   #添付ファイルのパス(配列で指定)
    $MailEncoding = "utf-8",            #本文と件名のエンコード
    $MailServer = "smtp.office365.com", #メールサーバー
    $MailPort = 587,                    #ポート番号
    $SSLFlg = $true                     #SSLを使用するかどうかを指定
  ){
  
  #Message
  $Msg = New-Object System.Net.Mail.MailMessage
  $Msg.From = $MailFrom
  foreach($MailTo in $MailToAry){
    $Msg.To.Add($MailTo)
  }
  if($MailCcAry -is [array]){
    foreach($MailCc in $MailCcAry){
      $Msg.CC.Add($MailCc)
    }
  }
  if($MailBccAry -is [array]){
    foreach($MailBcc in $MailBccAry){
      $Msg.Bcc.Add($MailBcc)
    }
  }
  $Msg.Subject = $MailSubject
  $Msg.SubjectEncoding = [System.Text.Encoding]::GetEncoding($MailEncoding);
  $Msg.Body = $MailBody
  $Msg.BodyEncoding = [System.Text.Encoding]::GetEncoding($MailEncoding);
  if($MailAtthPathAry -is [array]){
    foreach($MailAtthPath in $MailAtthPathAry){
      if([System.IO.File]::Exists($MailAtthPath)){
        $MailAtth = New-Object System.Net.Mail.Attachment($MailAtthPath)
        $Msg.Attachments.Add($MailAtth)
      }
    }
  }
  
  #Server
  $Smtp = New-Object System.Net.Mail.SmtpClient($MailServer, $MailPort)
  $Smtp.Credentials = New-Object System.Net.NetworkCredential($UserID, $UserPassword)
  $Smtp.EnableSsl = $SSLFlg
  $Smtp.Send($Msg)
  
  $Msg.Dispose()
  $Smtp.Dispose()
  return
}

sendMail "user@hogehogexxx.onmicrosoft.com" "passxxxx" "from@hogehogexxx.onmicrosoft.com" @("to@hogehogexxx.onmicrosoft.com") "" "" "テストメール" "本文1`r`n本文2" @("C:\Test\Test1.docx", "C:\Test\Test2.docx")

Send-MailMessageコマンドレットの方が簡単なのですが、細かく制御できるSmtpClientクラスの方を使っています。

また、標準ではOffice 365のサーバーからメール送信するように初期値を設定しています。

参考Webページ

[Windows 10]Update Complianceで端末のアップデート管理を行う方法前のページ

【アイカツフレンズ!】フレンズスカウト(日向エマ)に参加しました。次のページ

関連記事

  1. Office関連

    Excel REST APIをPowerShellから呼び出す方法

    以前Excel REST APIをVBAから呼び出す方法を紹介しました…

  2. Office関連

    PowerShellからNetOfficeを使ってExcelを操作する方法

    先日、Excel MVPの伊藤さんがPowerShellからExcel…

  3. Power Automate for desktop

    Power Automate Desktopを更新するPowerShellスクリプト

    公式ブログを見れば分かる通り、Power Automate Deskt…

  4. Windows 10

    PowerShellでMicrosoft Edgeを操作する

    前回の記事でWebDriverを使ってMicrosoft Edgeを操…

  5. Windows 10

    ストアアプリを起動するPowerShellコード

    ストアアプリ(UWPアプリ)は実行ファイルをダブルクリックする等して直…

コメント

  • コメント (0)

  • トラックバックは利用できません。

  1. この記事へのコメントはありません。

Time limit is exhausted. Please reload CAPTCHA.

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP