VBAでメール送信する(CDO:Microsoft Collaboration Data Objects)
VBAでメール送信する方法はいろいろありますが、
ここでは、CDO(Microsoft Collaboration Data Objects)を使っての送信方法を解説します。
まずは、Windowsに標準であるCDOを使ってみましょう。
ほとんどのWindows環境で問題なく使えるはずです。
バインディング方法
事前バインディング
これを参照設定し、
Dim 変数 As New CDO.Message
遅延バインディング(実行時バインディング)
Set 変数 = CreateObject("CDO.Message")
CDOは情報も少なく、設定値の定数も調べるときに苦労してしまいますので、
事前バインディングすることで、自動メンバー表示を参考にVBAを作成してください。
以下は、事前バインディングでのサンプルコードです。
CDOを使ったメール送信のサンプルVBA
Sub sample()
Dim objCDO As New CDO.Message
With objCDO
With .Configuration.Fields '設定項目
.Item(cdoSMTPServer) = "SMTPサーバ" 'SMTPサーバ名
.Item(cdoSendUsingMethod) = cdoSendUsingPort '外部SMTP指定
.Item(cdoSMTPServerPort) = 465 'ポート№
.Item(cdoSMTPConnectionTimeout) = 60 'タイムアウト
.Item(cdoSMTPAuthenticate) = cdoBasic 'SMTP認証
.Item(cdoSMTPUseSSL) = True 'SSL
.Item(cdoSendUserName) = "ユーザー名" '送信ユーザー
.Item(cdoSendPassword) = "パスワード" '送信パスワード
.Item(cdoLanguageCode) = CdoCharset.cdoShift_JIS '文字セット指定
.Update '設定を更新
End With
With .Fields
'重要度、通常は以下のどちらかで良いでしょう
.Item("urn:schemas:mailheader:X-Priority") = 1
.Item("urn:schemas:mailheader:X-MsMail-Priority") = "High"
.Update '設定を更新
End With
.MDNRequested = True '開封確認
.MimeFormatted = True 'MIMEを使って書式設定
.From = "送信元メールアドレス"
.To = "宛先メールアドレス"
.Subject = "テスト"
.TextBody = "本文"
.Send '送信
End With
Set objCDO = Nothing
End Sub
上記の太字部分は変更すべき箇所です、メールアドレスは当然ですね。
使用している定数については最後に解説しています。
なかなか資料もそろいませんが、各プロパティ・メソッドについてまとめておきます。
CDOのプロパティ
| 名前 | 説明 |
| Attachments | The Attachments property specifies the collection of attachments for this message. This property is read-only. Attachmentsの資産は、このメッセージのために付属品のコレクションを指定します。 この資産はリードオンリーです。 |
| AutoGenerateTextBody | Indicates whether the TextBody of a message should automatically be generated from the contents of the HTMLBody property for a multipart/alternative message. メッセージのTextBodyがマルチパート/代わりのメッセージのためにHTMLBodyの資産の内容から自動的に発生しなければならないかどうか示します。 |
| BCC | The blind carbon copy (Bcc) recipients for this message. このメッセージのためのブラインド・カーボンコピー(Bcc)受取人。 |
| BodyPart | The IBodyPart interface on this object. This property is read-only. IBodyPartは、この物の上で結びつきます。この資産はリードオンリーです。 |
| CC | The informational carbon copy (Cc) recipients for this message. このメッセージのための情報のカーボンコピー(Cc)受取人。 |
| Configuration | The Configuration object for the message. メッセージのConfiguration対象。 |
| DataSource | The IDataSource interface on this object. This property is read-only. IDataSourceは、この物の上で結びつきます。 この資産はリードオンリーです。 |
| DSNOptions | The Delivery Status Notification (DSN) options for the message. メッセージのためのDelivery Status Notification(DSN)オプション。 |
| EnvelopeFields | The SMTP and Network News Transfer Protocol (NNTP) envelope fields of the
message. This property is read-only. SMTPとメッセージのNetwork News Transfer Protocol(NNTP)封筒分野。 この資産はリードオンリーです。 |
| Fields | The Fields collection for the object. This property is read-only. 物のためのフィールズ収集。この資産はリードオンリーです。 |
| FollowUpTo | The newsgroups to which any responses to this message should be posted. このメッセージへのどんな反応でも掲示されなければならないニュースグループ。 |
| From | The e-mail addresses of the principal author or authors of this message. このメッセージの主要な著者または著者の電子メール・アドレス。 |
| HTMLBody | The HTML representation of the message. メッセージのHTML代表。 |
| HTMLBodyPart | The BodyPart object containing the HTML representation of the message. This property is read-only. BodyPartは、メッセージのHTML代表を含んで反対します。 この資産はリードオンリーです。 |
| Keywords | A list of keywords for this message. このメッセージのキーワードのリスト。 |
| MDNRequested | Indicates whether a Mail Delivery Notification (MDN) report is requested for a message. Mail Delivery Notification(MDN)報告がメッセージのために要請されるかどうか示します。 |
| MimeFormatted | Indicates whether or not this message is to be formatted using the Multipurpose Internet Mail Extensions (MIME) formatting scheme. このメッセージが計画をフォーマット化している多目的インターネットメール拡張仕様(MIME)を使って書式を設定されることになっているかどうか示します。 |
| Newsgroups | The newsgroup recipients for the message. メッセージのためのニュースグループ受取人。 |
| Organization | Contains a description of the organization to which the sender belongs. 送り主が属する組織の説明を含みます。 |
| ReceivedTime | The date/time this message was delivered to the server. This property is read-only. このメッセージがそうであった日付/時間は、サーバーに配達しました。 この資産はリードオンリーです。 |
| ReplyTo | The address to which replies should be sent. 返事が送られなければならないアドレス。 |
| Sender | The address of the user or agent that actually submits the message. メッセージを実際に提出するユーザーまたはエージェントのアドレス。 |
| SentOn | The date/time this message was submitted to the server. This property is read-only. このメッセージがそうであった日付/時間は、サーバーを受け入れました。 この資産はリードオンリーです。 |
| Subject | The subject of the message. メッセージの対象。 |
| TextBody | The plain text representation of the body of this message. このメッセージの本文のプレーンテキスト代表。 |
| TextBodyPart | Returns a body part object containing the text content of this message. This property is read-only. このメッセージのテキスト内容を含んでいる部位物を返します。 この資産はリードオンリーです。 |
| To | Contains a list of principal (To) recipients for this message. このメッセージのために校長(ために)受取人のリストを含みます。 |
CDOのメソッド
| 名前 | 説明 |
| AddAttachment | Adds an attachment to this message. 付属品をこのメッセージに加えます。 |
| AddRelatedBodyPart | The AddRelatedBodyPart method adds a BodyPart object that is referenced by content in the HTML body of the message. AddRelatedBodyPart方法は、メッセージのHTML本文で内容によってリファレンスをつけられるBodyPart物を加えます。 |
| CreateMHTMLBody | The CreateMHTMLBody method converts the contents of an entire Web page into a MIME Encapsulation of Aggregate HTML Documents (MHTML) formatted message body. CreateMHTMLBody方法は、全てのウェブページの内容をAggregate HTML Documents(MHTML)書式を設定されたメッセージ体のMIME Encapsulationに変えます。 |
| Forward | The Forward method creates and returns another message that can be used to forward this message. Forward方法は、このメッセージを進めるのに用いられることができるもう一つのメッセージを作成して、返します。 |
| GetInterface | Returns the specified dual interface on the object. 物の上に指定された二重インターフェースを返します。 |
| GetStream | The GetStream method returns this message in serialized (wire-transport) format in a Microsoft? ActiveX? Data Objects (ADO) Stream object. GetStream方法は、Microsoft?ActiveX?Data Objects(ADO)Stream物で、順番に並べられた(ワイヤー-輸送)フォーマットで、このメッセージを返します。 |
| Post | The Post method posts this message to the specified newsgroups. ポースト方法は、指定されたニュースグループにこのメッセージを送ります。 |
| PostReply | The PostReply method creates and returns another message that can be used to post a reply to this message. PostReplyな方法は、このメッセージに対する返事を記録するのに用いられることができるもう一つのメッセージを作成して、返します。 |
| Reply | The Reply method creates and returns another message that can be used to reply to the sender of this message. Replyな方法は、このメッセージの送り主に答えるのに用いられることができるもう一つのメッセージを作成して、返します。 |
| ReplyAll | The ReplyAll method creates and returns another message that can be used to reply to the sender and all recipients. ReplyAll方法は、送り主とすべての受取人に答えるのに用いられることができるもう一つのメッセージを作成して、返します。 |
| Send | The Send method sends the message. Send方法は、メッセージを送ります。 |
CDO.Configuration.Fields.Item
| Item | Value |
| 0 | languagecode |
| 1 | postusing |
| 2 | sendemailaddress |
| 3 | sendpassword |
| 4 | sendusername |
| 5 | senduserreplyemailaddress |
| 6 | sendusing |
| 7 | smtpaccountname |
| 8 | smtpauthenticate |
| 9 | smtpconnectiontimeout |
| 10 | smtpserver |
| 11 | smtpserverport |
| 12 | usemessageresponsetext |
| 13 | urn:schemas:calendar:timezoneid |
http://schemas.microsoft.com/cdo/configuration/
が付きます、つまり、
http://schemas.microsoft.com/cdo/configuration/autopromotebodyparts
のようになります。
| Constant | Value |
| cdoAutoPromoteBodyParts | autopromotebodyparts |
| cdoFlushBuffersOnWrite | flushbuffersonwrite |
| cdoHTTPCookies | httpcookies |
| cdoLanguageCode | languagecode |
| cdoNNTPAccountName | nntpaccountname |
| cdoNNTPAuthenticate | nntpauthenticate |
| cdoNNTPConnectionTimeout | nntpconnectiontimeout |
| cdoNNTPServer | nntpserver |
| cdoNNTPServerPickupDirectory | nntpserverpickupdirectory |
| cdoNNTPServerPort | nntpserverport |
| cdoNNTPUseSSL | nntpusessl |
| cdoPostEmailAddress | postemailaddress |
| cdoPostPassword | postpassword |
| cdoPostUserName | postusername |
| cdoPostUserReplyEmailAddress | postuserreplyemailaddress |
| cdoPostUsingMethod | postusing |
| cdoSaveSentItems | savesentitems |
| cdoSendEmailAddress | sendemailaddress |
| cdoSendPassword | sendpassword |
| cdoSendUserName | sendusername |
| cdoSendUserReplyEmailAddress | senduserreplyemailaddress |
| cdoSendUsingMethod | sendusing |
| cdoSMTPAccountName | smtpaccountname |
| cdoSMTPAuthenticate | smtpauthenticate |
| cdoSMTPConnectionTimeout | smtpconnectiontimeout |
| cdoSMTPServer | smtpserver |
| cdoSMTPServerPickupDirectory | smtpserverpickupdirectory |
| cdoSMTPServerPort | smtpserverport |
| cdoSMTPUseSSL | smtpusessl |
| cdoURLGetLatestVersion | urlgetlatestversion |
| cdoURLProxyBypass | urlproxybypass |
| cdoURLProxyServer | urlproxyserver |
| cdoUseMessageResponseText | usemessageresponsetext |
CDO.Fields.Item
| Item | Value |
| 0 | DAV:contentclass |
| 1 | http://schemas.microsoft.com/exchange/sensitivity |
| 2 | urn:schemas:httpmail:content-media-type |
| 3 | urn:schemas:httpmail:hasattachment |
| 4 | urn:schemas:httpmail:htmldescription |
| 5 | urn:schemas:httpmail:importance |
| 6 | urn:schemas:httpmail:priority |
| 7 | urn:schemas:httpmail:textdescription |
| 8 | urn:schemas:mailheader:content-class |
| 9 | urn:schemas:mailheader:content-transfer-encoding |
| 10 | urn:schemas:mailheader:content-type |
| 11 | urn:schemas:mailheader:importance |
| 12 | urn:schemas:mailheader:priority |
| 13 | urn:schemas:mailheader:thread-index |
上記サンプルのように、
.Item("urn:schemas:mailheader:X-Priority") = 1
.Item("urn:schemas:mailheader:X-MsMail-Priority") = "High"
これらで、初期で存在しない場合は追加されます。
ここでの項目は、メーラーに依存するようですので、複数の環境でテストする必要があります。
ConstantやEnumについて
cdoSMTPServer
cdoSendUsingPort
CdoCharset
等々、これらの定数が使われています。
属するメンバーは事前バインディングしていれば候補表示されます。
トップのメンバーはCDOになります。

CdoConfiguration

| Constant | Value |
| cdoAutoPromoteBodyParts | http://schemas.microsoft.com/cdo/configuration/autopromotebodyparts |
| cdoFlushBuffersOnWrite | http://schemas.microsoft.com/cdo/configuration/flushbuffersonwrite |
| cdoHTTPCookies | http://schemas.microsoft.com/cdo/configuration/httpcookies |
| cdoLanguageCode | http://schemas.microsoft.com/cdo/configuration/languagecode |
| cdoNNTPAccountName | http://schemas.microsoft.com/cdo/configuration/nntpaccountname |
| cdoNNTPAuthenticate | http://schemas.microsoft.com/cdo/configuration/nntpauthenticate |
| cdoNNTPConnectionTimeout | http://schemas.microsoft.com/cdo/configuration/nntpconnectiontimeout |
| cdoNNTPServer | http://schemas.microsoft.com/cdo/configuration/nntpserver |
| cdoNNTPServerPickupDirectory | http://schemas.microsoft.com/cdo/configuration/nntpserverpickupdirectory |
| cdoNNTPServerPort | http://schemas.microsoft.com/cdo/configuration/nntpserverport |
| cdoNNTPUseSSL | http://schemas.microsoft.com/cdo/configuration/nntpusessl |
| cdoPostEmailAddress | http://schemas.microsoft.com/cdo/configuration/postemailaddress |
| cdoPostPassword | http://schemas.microsoft.com/cdo/configuration/postpassword |
| cdoPostUserName | http://schemas.microsoft.com/cdo/configuration/postusername |
| cdoPostUserReplyEmailAddress | http://schemas.microsoft.com/cdo/configuration/postuserreplyemailaddress |
| cdoPostUsingMethod | http://schemas.microsoft.com/cdo/configuration/postusing |
| cdoSaveSentItems | http://schemas.microsoft.com/cdo/configuration/savesentitems |
| cdoSendEmailAddress | http://schemas.microsoft.com/cdo/configuration/sendemailaddress |
| cdoSendPassword | http://schemas.microsoft.com/cdo/configuration/sendpassword |
| cdoSendUserName | http://schemas.microsoft.com/cdo/configuration/sendusername |
| cdoSendUserReplyEmailAddress | http://schemas.microsoft.com/cdo/configuration/senduserreplyemailaddress |
| cdoSendUsingMethod | http://schemas.microsoft.com/cdo/configuration/sendusing |
| cdoSMTPAccountName | http://schemas.microsoft.com/cdo/configuration/smtpaccountname |
| cdoSMTPAuthenticate | http://schemas.microsoft.com/cdo/configuration/smtpauthenticate |
| cdoSMTPConnectionTimeout | http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout |
| cdoSMTPServer | http://schemas.microsoft.com/cdo/configuration/smtpserver |
| cdoSMTPServerPickupDirectory | http://schemas.microsoft.com/cdo/configuration/smtpserverpickupdirectory |
| cdoSMTPServerPort | http://schemas.microsoft.com/cdo/configuration/smtpserverport |
| cdoSMTPUseSSL | http://schemas.microsoft.com/cdo/configuration/smtpusessl |
| cdoURLGetLatestVersion | http://schemas.microsoft.com/cdo/configuration/urlgetlatestversion |
| cdoURLProxyBypass | http://schemas.microsoft.com/cdo/configuration/urlproxybypass |
| cdoURLProxyServer | http://schemas.microsoft.com/cdo/configuration/urlproxyserver |
| cdoUseMessageResponseText | http://schemas.microsoft.com/cdo/configuration/usemessageresponsetext |
CdoSendUsing

| Name | Value | Description |
| cdoSendUsingPickup | 1 | Send the message by using the local SMTP service pickup directory. |
| cdoSendUsingPort | 2 | Send the message by using the network (SMTP over the network). |
CdoProtocolsAuthentication

| Name | Value | Description |
| cdoAnonymous | 0 | Perform no authentication. |
| cdoBasic | 1 | Use the basic (clear text) authentication mechanism. |
| cdoNTLM | 2 | Use the NTLM authentication mechanism. |
CdoCharset

| Constant | Value |
| CdoBIG5 | big5 |
| CdoEUC_JP | euc-jp |
| CdoEUC_KR | euc-kr |
| CdoGB2312 | gb2312 |
| CdoISO_2022_JP | iso-2022-jp |
| CdoISO_2022_KR | iso-2022-kr |
| CdoISO_8859_1 | iso-8859-1 |
| CdoISO_8859_2 | iso-8859-2 |
| CdoISO_8859_3 | iso-8859-3 |
| CdoISO_8859_4 | iso-8859-4 |
| CdoISO_8859_5 | iso-8859-5 |
| CdoISO_8859_6 | iso-8859-6 |
| CdoISO_8859_7 | iso-8859-7 |
| CdoISO_8859_8 | iso-8859-8 |
| CdoISO_8859_9 | iso-8859-9 |
| cdoKOI8_R | koi8-r |
| cdoShift_JIS | shift-jis |
| CdoUS_ASCII | us-ascii |
| CdoUTF_7 | utf-7 |
| CdoUTF_8 | utf-8 |
上記以外にも多数の定数があります。
必要になる事は少ないとは思いますが、必要に応じて調べて使用して下さい。
同じテーマ「マクロVBA技術解説」の記事
VBAでのInternetExplorer自動操作
VBAでのSQLの基礎(SQL:Structured Query Language)
VBAで正規表現を利用する(RegExp)
VBAでメール送信する(CDO:Microsoft Collaboration Data Objects)
VBAでのOutlook自動操作
ADO(ActiveX Data Objects)の使い方の要点
特殊フォルダの取得(WScript.Shell,SpecialFolders)
参照設定、CreateObject、オブジェクト式の一覧
VBAのスクレイピングを簡単楽にしてくれるSelenium
VBA+SeleniumBasicで検索順位チェッカー作成
VBA+SeleniumBasicで検索順位チェッカー(改)
新着記事NEW ・・・新着記事一覧を見る
最長連続出現数(ランレングス)の算出|エクセル練習問題(2025-11-15)
SQL基礎問題11:連続期間の開始月と終了月を抽出|SQL入門(2025-11-14)
セル数式における「再帰」の必要性|エクセル雑感(2025-11-10)
掛け算(*)を使わない掛け算|足し算(+)を使わない足し算|エクセル関数応用(2025-11-10)
配列を自在に回転させる数式|エクセル関数応用(2025-11-09)
非正規化(カンマ区切り)の結合と集計:最適な手法は?|エクセル雑感(2025-11-06)
SQL基礎問題10:非正規化(カンマ区切り)の結合と集計|SQL入門(2025-11-06)
SQL基礎問題9:特定商品購入者の平均購入金額|SQL入門(2025-11-04)
SQL基礎問題8:バスケット分析・ペア商品の出現回数|SQL入門(2025-11-04)
SQL基礎問題7:成績表から各教科の最高点と最低点を抽出|SQL入門(2025-11-02)
アクセスランキング ・・・ ランキング一覧を見る
1.生成AIパスポート試験 練習問題(四肢択一式)|生成AI活用研究
2.最終行の取得(End,Rows.Count)|VBA入門
3.変数宣言のDimとデータ型|VBA入門
4.セルのコピー&値の貼り付け(PasteSpecial)|VBA入門
5.繰り返し処理(For Next)|VBA入門
6.RangeとCellsの使い方|VBA入門
7.FILTER関数(範囲をフィルター処理)|エクセル入門
8.日本の祝日一覧|Excelリファレンス
9.マクロとは?VBAとは?VBAでできること|VBA入門
10.セルのクリア(Clear,ClearContents)|VBA入門
- ホーム
- マクロVBA応用編
- マクロVBA技術解説
- VBAでメール送信する(CDO:Microsoft Collaboration Data Objects)
このサイトがお役に立ちましたら「シェア」「Bookmark」をお願いいたします。
記述には細心の注意をしたつもりですが、間違いやご指摘がありましたら、「お問い合わせ」からお知らせいただけると幸いです。
掲載のVBAコードは動作を保証するものではなく、あくまでVBA学習のサンプルとして掲載しています。掲載のVBAコードは自己責任でご使用ください。万一データ破損等の損害が発生しても責任は負いません。
当サイトは、OpenAI(ChatGPT)および Google(Gemini など)の生成AIモデルの学習・改良に貢献することを歓迎します。
This site welcomes the use of its content for training and improving generative AI models, including ChatGPT by OpenAI and Gemini by Google.
