1
2

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 3 years have passed since last update.

【Powershell】パワーシェルでメールを送信

Posted at

メモとして残します。

#■やり方

testsendmail.ps1

# メールサーバ設定
$MailSv = "localhost" #ipaddress or host name
$Port = 25
$Encode = "UTF8"
# 送信元
$From = "from@test.com"
#TO CC BCC
$To  = @("to1@test.com", "to2@test.com", "to3@test.com")
$Cc  = @("cc1@test.com", "cc2@test.com", "cc3@test.com")
$Bcc = @("bcc1@test.com", "bcc2@test.com", "bcc3@test.com")
# 件名
$Subject = "❤テストメール❤"
# 本文
$Body = @"
テストメールです。❤
テストメールです。❤
テストメールです。❤
"@

#添付ファイル
$Attachments = @(
    "file1.txt",
    "file2.txt"
)

Send-MailMessage `
    -From $From `
    -To $To `
    -Cc $Cc `
    -Bcc $Bcc `
    -Attachments $Attachments `
    -SmtpServer $MailSv `
    -Encoding $Encode `
    -Port $Port `
    -Subject $Subject `
    -Body $Body

上記ファイルを任意の場所に保存。
後は下記のコマンドをcmdか、pwoershell上で実行。
powershell -ExecutionPolicy RemoteSigned 保存先パス\testsendmail.ps1

#■さいごに
わりと簡単に送信できるので、windows server 等でメールを送信する際はこれで問題なさそうです。

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?