VBScriptでメールの送り方を4パターン紹介

VBScript

VBScript(Visual Basic Scripting Edition)は、Windows環境でのタスク自動化に非常に便利なスクリプト言語です。

今回は、VBScriptを使用してメールを送信する方法を紹介します。

メール送信は、システム管理や通知機能を自動化するために役立ちます。

CDOSYSを使用したメール送信

CDOSYS (Collaboration Data Objects for Windows 2000) は、Windows 2000以降のバージョンに組み込まれているコンポーネントで、メールの送信に使用できます。

以下は、CDOSYSを使用してメールを送信する基本的なスクリプトです。

Dim objMessage

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "テストメール"
objMessage.From = "sender@example.com"
objMessage.To = "recipient@example.com"
objMessage.TextBody = "これはVBScriptから送信されたテストメールです。"

' SMTPサーバの設定
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.com"
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update

' メールの送信
objMessage.Send

Set objMessage = Nothing

SMTP認証を使用したメール送信

多くのSMTPサーバは認証を必要とします。

以下のスクリプトは、SMTP認証を使用してメールを送信する例です。

Dim objMessage

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "テストメール(認証付き)"
objMessage.From = "sender@example.com"
objMessage.To = "recipient@example.com"
objMessage.TextBody = "これはVBScriptから送信されたテストメール(SMTP認証付き)です。"

' SMTPサーバの設定
With objMessage.Configuration.Fields
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.com"
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your_username"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your_password"
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    .Update
End With

' メールの送信
objMessage.Send

Set objMessage = Nothing

HTMLメールの送信

次に、HTML形式のメールを送信する方法を紹介します。

これにより、リッチテキスト形式のメールを送信できます。

Dim objMessage

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "HTML形式のテストメール"
objMessage.From = "sender@example.com"
objMessage.To = "recipient@example.com"
objMessage.HTMLBody = "<h1>これはHTML形式のメールです</h1><p>VBScriptから送信されました。</p>"

' SMTPサーバの設定
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.com"
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update

' メールの送信
objMessage.Send

Set objMessage = Nothing

添付ファイル付きメールの送信

最後に、メールに添付ファイルを追加する方法を紹介します。

Dim objMessage

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "添付ファイル付きのテストメール"
objMessage.From = "sender@example.com"
objMessage.To = "recipient@example.com"
objMessage.TextBody = "これは添付ファイル付きのメールです。"
objMessage.AddAttachment "C:\path\to\your\file.txt"

' SMTPサーバの設定
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.com"
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update

' メールの送信
objMessage.Send

Set objMessage = Nothing

まとめ

VBScriptを使用すると、さまざまな形式のメールを送信できます。

ここで紹介した基本的なスクリプトを活用することで、システム管理や通知機能を効率的に自動化できます。

実際の環境でスクリプトを使用する際は、セキュリティと認証情報の管理に注意してください。

これで、VBScriptを使ったメール送信の基本をマスターできるはずです。

次のステップとして、これらのスクリプトを実際の業務やプロジェクトで活用し、さらに高度な操作やカスタマイズを試してみてください。