【AIによる回答】
VBScriptで添付ファイル付きSMTPメールを送信するには、CDO.Messageオブジェクトを使用し、CreateObjectで作成したオブジェクトのプロパティを設定します。具体的には、SMTPserver、From、To、Subject、TextBody、そしてAttachmentFilenameプロパティを設定し、最後にSendメソッドを実行します。VBScriptコードの例vbscriptDim msg Set msg = CreateObject("CDO.Message") ' 送信元、宛先、件名、本文を設定 msg.From = "sender@example.com" msg.To = "recipient@example.com" msg.Subject = "添付ファイル付きメール" msg.TextBody = "添付ファイルをご確認ください。" ' SMTPサーバーの設定 msg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing").Value = 2 msg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate").Value = 1 msg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername").Value = "your_username" msg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword").Value = "your_password" msg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value = "your.smtp.server.com" msg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport").Value = 587 ' または25 msg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout").Value = 60 msg.Configuration.Fields.Update ' 添付ファイルの指定 msg.AttachmentFilename = "C:\path\to\your\file.txt" ' メール送信 msg.Send Set msg = Nothingコードの解説
CreateObject("CDO.Message"): CDO(Collaboration Data Objects)ライブラリのMessageオブジェクトを作成します。From,To,Subject,TextBody: 送信元、宛先、件名、本文を設定します。msg.Configuration.Fields: SMTPサーバーの設定を行います。
sendusing: 2に設定してSMTPで送信するようにします。smtpauthenticate: 1に設定して認証を有効にします。sendusername,sendpassword: SMTPサーバーの認証情報を設定します。smtpserver: 使用するSMTPサーバーのホスト名を指定します。smtpserverport: SMTPサーバーのポート番号を指定します(例: 587, 25)。smtpconnectiontimeout: 接続タイムアウト時間を秒単位で設定します。msg.AttachmentFilename: 送信する添付ファイルのパスを指定します。複数のファイルを添付したい場合は、msg.addattachmentメソッドを使用してください。msg.Send: メールを送信します。Set msg = Nothing: オブジェクトを解放します。注意:
- SMTPサーバーへの認証が必要な場合、
smtpauthenticate、sendusername、sendpasswordの設定が必要です。- ポート番号はサーバーによって異なる場合があります(例: 通常のSMTPは25、TLS/SSLを使用する場合は587)。
- セキュリティのため、パスワードはスクリプトに直接記述せず、より安全な方法で管理することをお勧めします。
技術レポート「Microsoft Collaboration Data Objectsを用いたメールの送信」|ソフテックだより|株式会社ソフテック
