4
4

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

PowerShellからOffice365のExchange Web Services Managed APIを使用してメールを送る

Posted at

Exchange Web Services Managed APIの練習です。
Office365のExchange Web Services Managed APIをたたいてメールを送信します。

PowerShellからメールを送信するだけならもっと簡単な方法がありますので、この内容を使うことはないかな。

準備

Microsoft Exchange Web Services Managed API 2.2 をダウンロードしてインストールします。

ソース

someone@example.comあてに「Hello world」というサブジェクトのメールを送信します。

sendmail.ps1
Import-Module "$env:ProgramFiles\\Microsoft\\Exchange\\Web Services\\2.2\\Microsoft.Exchange.WebServices.dll"

# Office365に接続するログインIDとパスワードです
$user = "myname@myorg.apac.microsoftonline.com"
$pass = "mysecret"

# AutoDiscoveryURLのコールバックです。これがないとAutoDiscoveryURLで失敗します
# 引数として渡すために変数に保存しておきます
function RedirectionUrlValidationCallback($redirectionUrl) {
    return $true
}

$redirectionUrlValidationCallback = Get-Content Function:\RedirectionUrlValidationCallback

$cred = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials($user, $pass)

$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
# トレースログを有効にします。
$service.TraceEnabled = $true
$service.TraceEnablePrettyPrinting = $true
$service.TraceFlags = [Microsoft.Exchange.WebServices.Data.TraceFlags]::All
$service.Credentials = $cred
# 接続するURLは必ずAutodiscoverする必要があります
$service.AutodiscoverUrl($user, $redirectionUrlValidationCallback)

$email = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($service)
$email.ToRecipients.Add("someonea@example.com") # メール宛先
$email.Subject="Hello world" # メールサブジェクト
$email.Body = New-Object Microsoft.Exchange.WebServices.Data.MessageBody("Hello EWS API") # メール本文
$email.Send()

はまったこと

EWS APIのURLは、auto discoveryしないとダメです。

4
4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?