ExcelVBAからメールを自動送信したいけれど、パソコンにOutlookがインストールされていない、あるいはGmailなどのWebメールサービスを直接使いたい、というケースはよくあります。そんなときに使えるのがCDO.Messageオブジェクトです。
この記事では、Outlookに依存せずSMTPサーバーへ直接接続してメールを送信するCDO.Messageの使い方を、認証設定や添付ファイルの付け方まで含めて解説します。
CDO.Messageとは
CDO(Collaboration Data Objects)は、Windowsに標準で組み込まれているメール送信用のライブラリです。CDO.Messageオブジェクトを使うと、Outlookのようなメールソフトを経由せず、指定したSMTPサーバーに直接接続してメールを送信できます。
Outlookの自動操作とは異なり、Outlookのインストールや起動が不要なため、サーバー上でExcelを自動実行するような環境でも利用しやすいのが特徴です。
基本的な送信コード
Gmailのアカウントを例に、件名・本文・宛先を指定してメールを送信するコードです。あらかじめGmail側で「アプリパスワード」を発行しておく必要があります。
Sub SendMailByCDO()
Dim cdoMsg As Object
Dim cdoConfig As Object
Set cdoMsg = CreateObject("CDO.Message")
Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your_account@gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your_app_password"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1
.Update
End With
With cdoMsg
Set .Configuration = cdoConfig
.To = "recipient@example.com"
.From = "your_account@gmail.com"
.Subject = "【自動送信】日次レポート"
.TextBody = "お疲れ様です。" & vbCrLf & "本日のレポートを添付しております。"
.Send
End With
Set cdoMsg = Nothing
Set cdoConfig = Nothing
MsgBox "メールを送信しました。", vbInformation
End Sub
sendusing = 2は「SMTPサーバーを使って送信する」ことを意味する固定値ですsmtpserverport = 587はSTARTTLS用のポート番号です(SSL専用の場合は465を使うこともあります)smtpusessl = 1でSSL/TLS通信を有効にします- パスワードをコードに直接書き込むのはセキュリティ上望ましくないため、実運用では環境変数やパスワード付きの別ファイルから読み込む方法を検討してください
添付ファイルを付けて送信する
AddAttachmentメソッドを使うと、ファイルを添付して送信できます。
Sub SendMailWithAttachment()
Dim cdoMsg As Object
Dim cdoConfig As Object
Dim filePath As String
filePath = ThisWorkbook.Path & "\report.xlsx"
Set cdoMsg = CreateObject("CDO.Message")
Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your_account@gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your_app_password"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1
.Update
End With
With cdoMsg
Set .Configuration = cdoConfig
.To = "recipient@example.com"
.From = "your_account@gmail.com"
.Subject = "【自動送信】日次レポート(添付あり)"
.TextBody = "本日のレポートを添付しております。"
.AddAttachment filePath
.Send
End With
Set cdoMsg = Nothing
Set cdoConfig = Nothing
End Sub
添付ファイルが存在しないパスを指定するとエラーになるため、Dir(filePath) = ""でファイルの存在確認を行ってから送信する処理を追加すると安全です。
エラーハンドリングを加える
送信先の入力ミスやネットワーク不通などでエラーが発生することを考慮し、On Errorでエラー処理を組み込んでおくと実務での事故を防げます。
Sub SendMailSafely()
On Error GoTo ErrHandler
Dim cdoMsg As Object
Dim cdoConfig As Object
Set cdoMsg = CreateObject("CDO.Message")
Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your_account@gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your_app_password"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1
.Update
End With
With cdoMsg
Set .Configuration = cdoConfig
.To = "recipient@example.com"
.From = "your_account@gmail.com"
.Subject = "【自動送信】日次レポート"
.TextBody = "本日のレポートをお送りします。"
.Send
End With
MsgBox "メールを送信しました。", vbInformation
GoTo Cleanup
ErrHandler:
MsgBox "メール送信でエラーが発生しました:" & Err.Description, vbCritical
Cleanup:
Set cdoMsg = Nothing
Set cdoConfig = Nothing
End Sub
Outlook自動操作との使い分け
同じメール送信でも、Outlookの自動操作とCDO.Messageにはそれぞれ向き・不向きがあります。
- Outlook自動操作が向いている場合:送信者本人のOutlookアカウントから、署名や下書き確認を挟みつつ送りたいとき
- CDO.Messageが向いている場合:Outlookがインストールされていない環境、サーバーでの自動実行、Gmailなど任意のSMTPサーバーから直接送信したいとき
用途に応じて使い分けることで、より柔軟なメール自動化が実現できます。
まとめ
CDO.Messageを使うことで、Outlookに依存せずSMTPサーバーへ直接接続してメールを送信できます。
CDO.ConfigurationでSMTPサーバー・ポート・認証情報を設定するAddAttachmentで添付ファイルを付けられる- パスワードの管理方法とエラーハンドリングには注意が必要
サーバー環境での自動レポート送信や、Outlookがない環境でのメール自動化に、ぜひ活用してみてください。


コメント