プログラミング学習サイト

プログラミングの学習を開始される方を対象としたプログラミング入門サイトです。

poewrshellでemailを送る

Powershell概要

minegishirei.hatenablog.com

poewrshellでemailを送る

なるべくシンプルなサンプルコード

#初期変数
[string[]] $to = "---@gmail.com", "-----@gmail.com"
[string] $subject = "<No Subject>",
[string] $body = "サンプルテキスト",
[string] $smtpHost = "メールサーバーを指定(gmailならsmtp.gmail.com)",
[string] $from = "$($env:UserName)@example.com"

## メール本文作成
$email = New-Object System.Net.Mail.MailMessage

## メールを全ての to に送る
foreach($mailTo in $to)
{
    $email.To.Add($mailTo)
}
$email.From = $from
$email.Subject = $subject
$email.Body = $body

## メールを送る
$client = New-Object System.Net.Mail.SmtpClient $smtpHost
$client.UseDefaultCredentials = $true
$client.Send($email)

元々のコード

############################################################################## 
## 
##  Send-MailMessage.ps1
## 
##  Illustrate the techniques used to send an email in PowerShell.
## 
##  Example:
## 
##   PS >$body = @"
##   >> Hi from another satisfied customer of The PowerShell Cookbook!
##   >> "@
##   >>
##   PS >$to = "guide_feedback@leeholmes.com"
##   PS >$subject = "Thanks for all of the scripts."
##   PS >$mailHost = "mail.leeholmes.com"
##   PS >Send-MailMessage $to $subject $body $mailHost
## 
############################################################################## 
param(
    [string[]] $to = $(throw "Please specify the destination mail address"),
    [string] $subject = "<No Subject>",
    [string] $body = $(throw "Please specify the message content"),
    [string] $smtpHost = $(throw "Please specify a mail server."),
    [string] $from = "$($env:UserName)@example.com"
)
##  Create the mail message
$email = New-Object System.Net.Mail.MailMessage
##  Populate its fields
foreach($mailTo in $to)
{
    $email.To.Add($mailTo)
}
$email.From = $from
$email.Subject = $subject
$email.Body = $body
##  Send the mail
$client = New-Object System.Net.Mail.SmtpClient $smtpHost
$client.UseDefaultCredentials = $true
$client.Send($email)

参考:海外版のpowershell pdfファイル

URL:http://index-of.co.uk/Microsoft-Windows-Ebooks/OReilly.Windows.PowerShell.Cookbook.Oct.2007.pdf

title:powershellでメール自動配信 description:海外版のpowershellクックブックを参考に、powershellで自動メール配信システムを作成 category_script:page_name.startswith("2") img:https://boeprox.files.wordpress.com/2016/01/image2.png

page:https://minegishirei.hatenablog.com/entry/2024/05/22/120409