AutoIt SMTPメール送信 CDO.Messageを使う

SMTPMail CDO.Message AutoIT

AutoItでメール自動送信をしたいと思ったらInet.au3がincludeフォルダに入っています。引数にメール設定パラメータ入れて送ればOKです。
せっかく自動化するのにOutlookやThunderbirdとかのメールクライアントを使うのはGUIによる余計なエラーを誘発しやすく、本来の目的であるメール送信を邪魔する可能性があります。Outlook起動して新規作成ボタンのコントロールID取得してクリックして・・なんてことをやるのは・・ですね。

スポンサーリンク

1. ダウンロードする・・というかスクリプトをコピペする

下記サイトに行ってスクリプトをコピーしまし、smtpmail.au3とか自分で決めたファイル名で保存しましょう。3のところです。If you want to send email with AutoIt, you have to choose..のUDF Autoit file: UDF_SMTP_eMail.au3 のところ。自分はこれ使っています。

Attention Required! | Cloudflare
[autoit title=”smtp mail “] ;============================================================================================================== ; Description : Send an email with a SMTP server by Microsoft CDO technology ; Parametere : $s_SmtpServer ; $s_FromName ; $s_FromAddress ; $s_ToAddress ; $s_Subject ; $as_Body ; $s_AttachFiles (path file to join) ; $s_CcAddress ; $s_BccAddress ; $s_Username ; $s_Password ; $IPPort ; $ssl ; Return : On success none ; On error code+msg ;============================================================================================================== Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Username = "", $s_Password = "",$IPPort=25, $ssl=0) Local $objEmail = ObjCreate("CDO.Message") Local $i_Error = 0 Local $i_Error_desciption = "" $objEmail.From = ‘"’ & $s_FromName & ‘" <‘ & $s_FromAddress & ‘>’ $objEmail.To = $s_ToAddress If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress $objEmail.Subject = $s_Subject If StringInStr($as_Body,"<") and StringInStr($as_Body,">") Then $objEmail.HTMLBody = $as_Body Else $objEmail.Textbody = $as_Body & @CRLF EndIf If $s_AttachFiles <> "" Then Local $S_Files2Attach = StringSplit($s_AttachFiles, ";") For $x = 1 To $S_Files2Attach[0] $S_Files2Attach[$x] = _PathFull ($S_Files2Attach[$x]) If FileExists($S_Files2Attach[$x]) Then $objEmail.AddAttachment ($S_Files2Attach[$x]) Else $i_Error_desciption = $i_Error_desciption &amp; @lf & ‘File not found to attach: ‘ & $S_Files2Attach[$x] SetError(1) return 0 EndIf Next EndIf $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort ;Authenticated SMTP If $s_Username <> "" Then $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password EndIf If $Ssl Then $objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True EndIf ;Update settings $objEmail.Configuration.Fields.Update ; Sent the Message $objEmail.Send if @error then SetError(2) return $oMyRet[1] EndIf EndFunc ;==>_INetSmtpMailCom ;=========================================================================================================== [/autoit]

Githubにもコード置きました。

https://github.com/cfautog/autoit/blob/master/sendmail.au3

1.1 引数のデフォルト値を入れて簡単にしておく

$IPPort=587, $ssl=0など環境に合わせて引数を変更、SmtpServerの順番も後にするなどして楽をすればいいかと。
で、#include “smtpmail.au3″ でincludeして使えばよいと思います。

1.2 使ってみる

変数にセットして呼ぶだけです。

$mailserver = "asefefe.server.jp"
$portno = 587
$ssl = 0
$from = "cfautog@cfautog.tokyo"
$to = "toaddr@gmail.com"
$subj = "件名をここに"
$body = "添付もできるよ"
$filepath = @ScriptDir & "\attach.xlsx"
$muser = "cfautog"
$mpass = "loginpass"

Local $ret = _INetSmtpMailCom($mailserver, "", $from, $to, $subj, $body, $filepath, "", "", $muser, $mpass, $portno, $ssl )
If @error Then 
  ConsoleWrite("-- sendmail error ")
EndIf
ConsoleWrite("- finish")

添付ファイルも同じディレクトリにattach.xlsxとか用意して送ってみましょう。

2. CDO.Messageでググると良い

関数では最初に$objEmail = ObjCreate(“CDO.Message”) としています。CDO(Microsoft Collaboration Data Objects)を使っていますね。
AutoItスクリプト書いていると、ググればググるほどVBAベースの記事が出てきます。
記述方法をちょっと変更(Sub->Func, Set xx= -> $xx=など)すれば結構動きます。

もうここまで来たらVBA覚えたほうが早いのでは!?と思うくらいですが、無償でExe化出来るAutoItを自分は使っていきたいと思います。

3. SMTP Mailer UDFはより細かい設定が出来る模様

SMTP Mailer UDFはより細かい設定が出来るようですが、自分にはとりあえずは必要ないので、今回はstackoverflowに載っていたのを使いました。

SMTP Mailer UDF
I very long time was using In the end, I found that at present my needs I need to modify it. And here it is: #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w...

4. Gmailから送信するにはgoogleアカウントでの設定が必要

AutoItで作成したアプリはGoogleアカウントに作成したアプリを許可していないので、送信できません。エラーが返ってきます。

Google アカウントで「安全性の低いアプリのアクセス」を有効にしてから、gmailのsmtp設定をして送信関数を呼ぶとメールが送られます。
googleログインして下記にアクセスし、有効にしましょう。

Account settings: Your browser is not supported.
googleアカウント 安全性の低いアプリのアクセス

あとは設定をし再度関数呼ぶといけると思います。

  • portno=465
  • ssl=1 (true)
  • server= smtp.gmail.com
  • username = あなたのgmail
  • password =あなたのgmailパスワード

コメント

タイトルとURLをコピーしました