▼ バッチファイル ▼ スクリプト SSL/465 で暗号化通信をします。
Gmail は、smtp.gmail.com Yahoo メールは smtp.mail.yahoo.co.jp ( アカウントは @ の前の部分 )※ Gmail の場合、安全性の低いアプリの許可を『有効』にする必要があります
ファイルを添付する場合は、 Call Cdo.AddAttachment( "ファイルのフルパス" ) と追加します ※ ファイルのパスは、URL でも可能です。HTML メールも送りたい場合は、 Cdo.HTMLBody = "HTMLの記述" ですCC と BCC は、カンマで区切られた複数のアドレスを設定します Cdo.CC = """User 1""関連する記事 C# : TKMP.DLLを使った、メール送信テンプレート Windows10 で確認しました。この記事の後半にあるダウンロードとソースコードは昔のものですが、動作するのでそのままおいて置きますが、今回新たにテストしたのはメールを送る機能に特化したスクリプトです。 ただ、Windows として過去の経緯からして必ず動くと保証できないので、XAMPP + Fake sendmail という環境で PHP の mb_send_mail を使用する方法も視野にいれておくといいと思います。 バッチファイル, ""User 2"" "
@echo off cscript //Nologo mail.vbs "ここは 題名" "ここは 本文\nです。\n簡単なメッセージを送ります"
VBScript : mail.vbs
' **************************************************** ' 【 対象 : Windows XP 以降 】 ' ' HTML メールは Cdo.Htmlbody を使用します ' "<PRE>" & strBody & "</PRE>" をセットすると ' 良いでしょう ) ' ' 添付ファイルは ' Cdo.AddAttachment( "添付ファイルへのパス" ) を実行します ' **************************************************** strFrom = "username@gmail.com" strTo = "宛先" ' サーバ strServer = "smtp.gmail.com" nPort = 465 strUser = "username@gmail.com" strPass = "パスワード" Set Cdo = WScript.CreateObject("CDO.Message") ' 差出人 Cdo.From = "わたしです<" & strFrom & ">" ' 宛先 Cdo.To = strTo ' 件名 Cdo.Subject = WScript.Arguments(0) ' 本文 Cdo.Textbody = Replace( WScript.Arguments(1), "\n", vbCrLf ) ' ポート利用( 2 ) Cdo.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 Cdo.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strServer ' メールサーバの仕様に合わせる Cdo.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = nPort ' SSL 使用 Cdo.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true ' ユーザ/パスワードでログイン Cdo.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 Cdo.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = strUser Cdo.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = strPass Cdo.Configuration.Fields.Update on error resume next Cdo.Send if Err.Number <> 0 then strMessage = Err.Description else strMessage = "送信が完了しました" end if on error goto 0 Wscript.Echo strMessage
ここから古い記事の部分 Windows XP 以降で実行可能です。 ( Ping 部分 ) ※ Mail 送信部分は Windows 2000 以降です。
' **************************************************** ' 【 対象 : Windows XP 以降 】 ' Ping を実行して応答が返って来たら、 ' 指定アドレスにメールを送ります ' ' HTML メールは Cdo.Htmlbody を使用します ' ' 添付ファイルは ' Cdo.AddAttachment( "添付ファイルへのパス" ) を実行します ' **************************************************** strTarget = "192.168.1.1" strFrom = "xxxx@xxxxxxxxxx" strTo = "yyyy@yyyyyyyyy" strServer = "zzzzzzzzzzzzzz" nPort = 587 strUser = "aaaa" strPass = "????" ' **************************************************** ' WMI による PING 処理 ' 最大4回リトライ(正常なら1回) ' **************************************************** Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") bReply = false For I = 1 to 4 Set colItems = objWMIService.ExecQuery _ ("Select * from Win32_PingStatus " & _ "Where Address = '" & strTarget & "'") For Each objItem in colItems if objItem.StatusCode = 0 then bReply = true I = 4 Exit For end if Next Next if bReply then strSubject = strTarget & " は稼動中です" strBody = "よろしくお願いします" else strSubject = strTarget & " は問題があります" strBody = "調査をお願いします" end if Set Cdo = WScript.CreateObject("CDO.Message") Cdo.From = strFrom Cdo.To = strTo Cdo.Subject = strSubject Cdo.Textbody = strBody Cdo.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 Cdo.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strServer Cdo.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = nPort Cdo.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 Cdo.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendusername") = strUser Cdo.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = strPass Cdo.Configuration.Fields.Update on error resume next Cdo.Send if Err.Number <> 0 then strMessage = Err.Description else strMessage = "送信が完了しました" end if on error goto 0 Wscript.Echo strMessage
関連する Microsoft ドキュメント CdoSendUsing Enum 関連する記事 IE拡張メニューで取得したテキストをメールで送る(CDO.Message版) 更新履歴 初回投稿 : 不明 2013-01-06 : 部分変更 2013-07-26 : Gmail をベースにして、SSL/465 で暗号化処理 2013-07-31 : Yahoo でテストして、添付ファイル、HTMLメール、CC、BCC の使い方を追加
|
【VBScriptの最新記事】
- 【VBS】WshShell.Runによる外部プログラムの実行のバリエーションと注意事項
- VBScript で GUID 生成
- バックアップスクリプトを作るスクリプト
- バッチ処理の為の .wsf スケルトン / cscript.exe での実行を強制する / 自分自身の呼び出し
- 【VBS + Excel】シンプルな詳細設計書のフォーマット。1) Excel のダウンロード、2) VBScript で作成するサンプル
- VBScript : CAPICOM を使った通常文字列の Base64エンコードと SHA1 と MD5 と SHA256/384/512
- 【VBS汎用】SHIFT_JISデータを各種キャラクタセットのテキストファイルへ変換
- VBScript : デスクトップの列挙
- Excel シートから Basp21 を使用してメールの送受信をして、今後の何かに役立てる
- VBScript + clipコマンド + レジストリ登録で、エクスプローラのディレクトリの右クリックでファイル名のリストをクリップボードにコピーできるようにする
- VBScript(WMI) : OSの情報をコマンドプロンプトに一覧で表示する
- VBScript(WMI) : 共有一覧
- IE のお気に入りディレクトリにあるファイルの表示・非表示を設定する / HTA(HTML アプリケーション)
- VBScript : ディレクトリ内のディレクトリで使用している容量一覧
- VBScript(WMI) : システム環境変数の変更を監視する event_change_reg.wsf
- VBScript : スクリプトが存在するディレクトリをユーザ環境変数の PATH へ登録
- XMLファイルの書き換え (VBScript)
- VBScript から実行する PHPコードの PDF処理のサンプル
- Cscript.exe の引数と、Windows Script Host を無効にするレジストリ設定
- Access.Application ( docmd.TransferText ) でエクスポートとインポート