


Send-MailMessage cmdletはPowershell Ver.2.0から利用できますが、SMTPポートの変更ができません
もう一つの方法として System.Net.Mailを使用します
Sending mail with PowerShell
http://www.philerb.com/2011/11/sending-mail-with-powershell/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
$emailSmtpServer = "smtp.rootlinks.net" $emailSmtpServerPort = "587" $emailSmtpUser = "username" $emailSmtpPass = "password" $emailFrom = "test id1 <test-id2@rootlinks.net>" $emailTo = "test-id2@rootlinks.net, test-id3@rootlinks.net" $emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo ) $emailMessage.Subject = "テストメール" $emailMessage.IsBodyHtml = $false $emailMessage.Body = "これは日本語のテストメールです" $emailMessage.SubjectEncoding = [System.Text.Encoding]::GetEncoding("ISO-2022-JP") $emailMessage.BodyEncoding = [System.Text.Encoding]::GetEncoding("ISO-2022-JP") $SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort ) $SMTPClient.EnableSsl = $true $SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass ); $SMTPClient.Send( $emailMessage ) |
ISO-2022-JPでエンコードされたメールが送信できました
1 2 3 4 5 6 7 8 9 |
MIME-Version: 1.0 From: test id1 <k.matsuoka@rootlinks.net> To: test-id2@rootlinks.net, test-id3@rootlinks.net Date: 14 Sep 2013 21:22:04 +0900 Subject: =?iso-2022-jp?Q?=1B$B%F%9%H%a!<%k=1B(B?= Content-Type: text/plain; charset=iso-2022-jp Content-Transfer-Encoding: quoted-printable =1B$B$3$l$OF=7CK=5C8l$N%F%9%H%a!<%k$G$9=1B(B |
HTMLメールは下記のようになります
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
$emailSmtpServer = "smtp.rootlinks.net" $emailSmtpServerPort = "587" $emailSmtpUser = "username" $emailSmtpPass = "password" $emailFrom = "test id1 <test-id2@rootlinks.net>" $emailTo = "test-id2@rootlinks.net, test-id3@rootlinks.net" $emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo ) $emailMessage.Subject = "テストメール" $emailMessage.IsBodyHtml = $true $emailMessage.Body = @" <p>これは<strong>HTMLフォーマット</strong>のテストメールです。</p> <p>Powershellは楽しいですね</p> "@ $emailMessage.SubjectEncoding = [System.Text.Encoding]::GetEncoding("ISO-2022-JP") $emailMessage.BodyEncoding = [System.Text.Encoding]::GetEncoding("ISO-2022-JP") $SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort ) $SMTPClient.EnableSsl = $true $SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass ); $SMTPClient.Send( $emailMessage ) |
@”~”@は複数行の文字列を変数に代入する方法だそうです。
Windows PowerShell Tip of the Week
Using Windows PowerShell “Here-Strings”
http://technet.microsoft.com/ja-jp/library/ee692792.aspx