CDO(Collaboration Data Objects)とSMTPコンポーネントを使ってメールを送信することができる。
CDO、SMTPサービスはOSのバージョン・エディションによって標準搭載されているものといないものがあるので注意。
詳細は以下の通り。
OS CDO SMTPサービス _____________________________________________________________ Windows 2000 Professional ○ ○ Windows 2000 Server ○ ○ Windows XP Professional ○ ○ Windows XP Home Edition ○ × Windows Server 2003 ○ ○(マニュアル設定が必要)
外部SMTPサーバを利用したメール送信スクリプト(以下の例はGmailを利用)
>|||
'--------------------------------------------------------------------------------------
'
' メール送信スクリプト(外部SMTPサーバ利用)
'
'--------------------------------------------------------------------------------------
On Error Resume Next
'CDOのスキーマを定義
Const cdoSchemas = "http://schemas.microsoft.com/cdo/configuration/"
'利用するSMTPサーバの名前とポートの定義
Const smtpServer = "smtp.gmail.com"
Const smtpServerPort = 465
'SMTPサーバへのアクセスユーザ・パスワードの定義
Const smtpUserID= "xxxxx@gmail.com"
Const smtpPassword = "xxxxx"
Set oMsg = CreateObject("CDO.Message")
'送受信アドレス
oMsg.From = "xxxx@hoge"
oMsg.To = "xxxx@hoge.xx.jp"
'本文・メッセージ
oMsg.Subject = "SMTP利用テスト"
oMsg.TextBody = "テストメッセージ" & vbCrLf & Now
'外部SMTPサーバの利用を定義
oMsg.Configuration.Fields.Item(cdoSchemas & "sendusing") = 2
'SMTPサーバ、ポートを指定
oMsg.Configuration.Fields.Item(cdoSchemas & "smtpserver") = smtpServer
oMsg.Configuration.Fields.Item(cdoSchemas & "smtpserverport") = smtpServerPort
'SMTPサーバへの認証情報を指定
oMsg.Configuration.Fields.Item(cdoSchemas & "sendusername") = smtpUserID
oMsg.Configuration.Fields.Item(cdoSchemas & "sendpassword") = smtpPassword
oMsg.Configuration.Fields.Item(cdoSchemas & "smtpauthenticate") = 1
oMsg.Configuration.Fields.Item(cdoSchemas & "smtpusessl") = True
oMsg.Configuration.Fields.Update
oMsg.Send
If Err.Number = 0 Then
WScript.Echo "メールを送信しました。"
Else
WScript.Echo "メールの送信に失敗しました。" & _
"(" & Err.Description & ")"
End If
|