<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mail</title>
	<atom:link href="https://javeo.jp/tag/mail/feed/" rel="self" type="application/rss+xml" />
	<link>https://javeo.jp</link>
	<description>ほどほどレベルのプログラミング</description>
	<lastBuildDate>Sun, 09 Jun 2024 16:39:13 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://javeo.jp/wp-content/uploads/2025/08/cropped-ExcelPython_future-32x32.jpg</url>
	<title>Mail</title>
	<link>https://javeo.jp</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【VBA】マクロでGmailを送信する</title>
		<link>https://javeo.jp/vba-gmail-send/</link>
					<comments>https://javeo.jp/vba-gmail-send/#comments</comments>
		
		<dc:creator><![CDATA[ジャベ雄]]></dc:creator>
		<pubDate>Sun, 25 Sep 2022 23:00:00 +0000</pubDate>
				<category><![CDATA[VBA・Excel]]></category>
		<category><![CDATA[Gmail]]></category>
		<category><![CDATA[Mail]]></category>
		<category><![CDATA[VBA]]></category>
		<guid isPermaLink="false">https://javeo.jp/?p=891</guid>

					<description><![CDATA[昔作ったツールを引っ張りだして来たら使えなくなっていたので改善方法も含めて改めてプログラムや設定方法をまとめてみました Outlookなら専用のライブラリがあるのでそっちを使った方がいいですが、GmailならCDO(Mi [&#8230;]]]></description>
										<content:encoded><![CDATA[<div class="codoc-evacuations" style="display:none;" data-shortcode=""></div>
<p>昔作ったツールを引っ張りだして来たら使えなくなっていたので改善方法も含めて改めてプログラムや設定方法をまとめてみました</p>



<p>Outlookなら専用のライブラリがあるのでそっちを使った方がいいですが、GmailならCDO(Microsoft Collaboration Data Objects)を使って送信することになるのでその方法になります。</p>




  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-2" checked><label class="toc-title" for="toc-checkbox-2">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">最初にやる一番キモなGmailの事前設定</a><ol><li><a href="#toc2" tabindex="0">安全性の低いアプリのアクセス・・・が使用できない！？</a></li><li><a href="#toc3" tabindex="0">今は&#8221;アプリパスワード&#8221;の生成が必要</a><ol><li><a href="#toc4" tabindex="0">まずは2段階認証プロセスをオンにする</a></li><li><a href="#toc5" tabindex="0">アプリパスワードを生成する</a></li><li><a href="#toc6" tabindex="0">発行された16桁のパスワードが&#8221;アプリパスワード&#8221;</a></li></ol></li></ol></li><li><a href="#toc7" tabindex="0">VBAの参照設定</a></li><li><a href="#toc8" tabindex="0">実際のVBAプログラム</a><ol><li><a href="#toc9" tabindex="0">プログラムソース</a></li><li><a href="#toc10" tabindex="0">ざっくり説明</a></li></ol></li><li><a href="#toc11" tabindex="0">あとがき</a></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">最初にやる一番キモなGmailの事前設定</span></h2>



<h3 class="wp-block-heading"><span id="toc2">安全性の低いアプリのアクセス・・・が使用できない！？</span></h3>



<figure class="wp-block-image size-full"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h09_16.png"><img fetchpriority="high" decoding="async" width="861" height="212" src="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h09_16.png" alt="" class="wp-image-896" srcset="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h09_16.png 861w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h09_16-300x74.png 300w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h09_16-150x37.png 150w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h09_16-768x189.png 768w" sizes="(max-width: 861px) 100vw, 861px" /></a></figure>



<p>一昔前はこの設定を有効化することがGmail側の設定だったのにいつの間にか使用できなくなっていました</p>



<p>（結局この部分が昔は利用できたツールが利用できなくなっていた原因でした）</p>



<p>と言うか気づいていなかっただけでしっかりGoogleからの<span class="marker-under">2022年5月30日</span>より利用できなくなる事前アナウンスがあったのはうっかり</p>



<figure class="wp-block-image aligncenter size-full"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_22h29_40.png"><img decoding="async" width="509" height="825" src="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_22h29_40.png" alt="" class="wp-image-898" srcset="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_22h29_40.png 509w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_22h29_40-185x300.png 185w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_22h29_40-93x150.png 93w" sizes="(max-width: 509px) 100vw, 509px" /></a></figure>



<h3 class="wp-block-heading"><span id="toc3">今は&#8221;アプリパスワード&#8221;の生成が必要</span></h3>



<p>設定するページはこちらから</p>



<p><a rel="noreferrer noopener" target="_blank" href="https://myaccount.google.com/security">https://myaccount.google.com</a><a rel="noopener noreferrer" target="_blank" href="https://myaccount.google.com/security">/security</a></p>



<h4 class="wp-block-heading"><span id="toc4">まずは2段階認証プロセスをオンにする</span></h4>



<figure class="wp-block-image size-full"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2022/09/2022-09-25_15h26_42.png"><img decoding="async" width="854" height="327" src="https://javeo.jp/wp-content/uploads/2022/09/2022-09-25_15h26_42.png" alt="" class="wp-image-913" srcset="https://javeo.jp/wp-content/uploads/2022/09/2022-09-25_15h26_42.png 854w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-25_15h26_42-300x115.png 300w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-25_15h26_42-150x57.png 150w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-25_15h26_42-768x294.png 768w" sizes="(max-width: 854px) 100vw, 854px" /></a></figure>



<p>初期では2段階認証プロセスがオフになっているのでオンにする</p>



<figure class="wp-block-image aligncenter size-full"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h25_17.png"><img decoding="async" width="679" height="823" src="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h25_17.png" alt="" class="wp-image-900" srcset="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h25_17.png 679w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h25_17-248x300.png 248w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h25_17-124x150.png 124w" sizes="(max-width: 679px) 100vw, 679px" /></a></figure>



<p>オンにするのは次ページで&#8221;使ってみる&#8221;ボタンを押下するだけ</p>



<h4 class="wp-block-heading"><span id="toc5">アプリパスワードを生成する</span></h4>



<figure class="wp-block-image size-full"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h27_08.png"><img decoding="async" width="855" height="329" src="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h27_08.png" alt="" class="wp-image-901" srcset="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h27_08.png 855w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h27_08-300x115.png 300w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h27_08-150x58.png 150w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h27_08-768x296.png 768w" sizes="(max-width: 855px) 100vw, 855px" /></a></figure>



<p>2段階認証プロセスをオンにしたことでアプリパスワードの選択肢が増えているのでここで設定する</p>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow" style="flex-basis:50%">
<div class="wp-block-group is-layout-flow wp-block-group-is-layout-flow">
<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow" style="flex-basis:100%">
<figure class="wp-block-image size-full"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h53_33.png"><img decoding="async" width="671" height="351" src="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h53_33.png" alt="" class="wp-image-903" srcset="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h53_33.png 671w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h53_33-300x157.png 300w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h53_33-150x78.png 150w" sizes="(max-width: 671px) 100vw, 671px" /></a></figure>
</div>
</div>
</div>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow" style="flex-basis:50%">
<div class="wp-block-columns are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow" style="flex-basis:100%">
<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow" style="flex-basis:100%">
<figure class="wp-block-image size-full"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h38_01.png"><img decoding="async" width="674" height="241" src="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h38_01.png" alt="" class="wp-image-902" srcset="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h38_01.png 674w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h38_01-300x107.png 300w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_23h38_01-150x54.png 150w" sizes="(max-width: 674px) 100vw, 674px" /></a></figure>
</div>
</div>
</div>
</div>
</div>
</div>



<p>アプリパスワードの名前になるだけなのでそれらしい選択をすればいいが&#8221;その他&#8221;を選ぶとフリー入力に変わる</p>



<p>&#8220;生成&#8221;ボタンが活性化するので押下</p>



<h4 class="wp-block-heading"><span id="toc6">発行された16桁のパスワードが&#8221;アプリパスワード&#8221;</span></h4>



<figure class="wp-block-image aligncenter size-full"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2022/09/2022-09-20_00h00_53.png"><img decoding="async" width="655" height="556" src="https://javeo.jp/wp-content/uploads/2022/09/2022-09-20_00h00_53.png" alt="" class="wp-image-905" srcset="https://javeo.jp/wp-content/uploads/2022/09/2022-09-20_00h00_53.png 655w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-20_00h00_53-300x255.png 300w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-20_00h00_53-150x127.png 150w" sizes="(max-width: 655px) 100vw, 655px" /></a></figure>



<p>右下の完了クリックすると2度と表示されないのでパスワードを控えておく</p>



<h2 class="wp-block-heading"><span id="toc7">VBAの参照設定</span></h2>



<p><strong>Microsoft CDO for Windows 2000 Library</strong></p>



<figure class="wp-block-image aligncenter size-full"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_22h38_51-1.png"><img decoding="async" width="455" height="317" src="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_22h38_51-1.png" alt="" class="wp-image-895" srcset="https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_22h38_51-1.png 455w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_22h38_51-1-300x209.png 300w, https://javeo.jp/wp-content/uploads/2022/09/2022-09-19_22h38_51-1-150x105.png 150w" sizes="(max-width: 455px) 100vw, 455px" /></a></figure>



<h2 class="wp-block-heading"><span id="toc8">実際のVBAプログラム</span></h2>



<h3 class="wp-block-heading"><span id="toc9">プログラムソース</span></h3>



<div class="hcb_wrap"><pre class="prism undefined-numbers lang-vba" data-lang="Visual Basic + VBA"><code>Function sendMail(MailSubject As String, MailBody As String, ToAddress As String, _
                    Optional CcAddress As String = vbNullString, Optional BccAddress As String = vbNullString, Optional attFile As Variant = vbNullString)

Const mailUser As String = &quot;&lt;Gmailのメアド&gt;&quot;
Const mailPass As String = &quot;&lt;アプリパスワード&gt;&quot;

Dim cdoMsg As Object: Set cdoMsg = CreateObject(&quot;CDO.Message&quot;)
Dim objFso As Object: Set objFso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Dim i As Long

With cdoMsg
    With .Configuration.Fields
        .Item(&quot;http://schemas.microsoft.com/cdo/configuration/languagecode&quot;) = &quot;shift-jis&quot; &#39;---文字コード指定
        .Item(&quot;http://schemas.microsoft.com/cdo/configuration/sendusing&quot;) = 2 &#39;---外部SMTP指定
        .Item(&quot;http://schemas.microsoft.com/cdo/configuration/smtpserver&quot;) = &quot;smtp.gmail.com&quot; &#39;---SMTPサーバ名
        .Item(&quot;http://schemas.microsoft.com/cdo/configuration/smtpserverport&quot;) = &quot;465&quot; &#39;---ポート№
        .Item(&quot;http://schemas.microsoft.com/cdo/configuration/smtpauthenticate&quot;) = True &#39;---SMTP認証
        .Item(&quot;http://schemas.microsoft.com/cdo/configuration/smtpusessl&quot;) = True &#39;---SSL
        .Item(&quot;http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout&quot;) = 60 &#39;---タイムアウト
        .Item(&quot;http://schemas.microsoft.com/cdo/configuration/sendusername&quot;) = mailUser &#39;---ユーザー
        .Item(&quot;http://schemas.microsoft.com/cdo/configuration/sendpassword&quot;) = mailPass &#39;---パスワード
        .Update
    End With
    .Subject = MailSubject
    .TextBody = MailBody
    .TextBodyPart.Charset = &quot;ISO-2022-JP&quot;
    .From = mailUser
    .To = ToAddress
    .CC = CcAddress
    .BCC = BccAddress
    
    &#39;---添付ファイルが複数（配列形式）かの分岐
    If IsArray(attFile) = True Then
        For i = LBound(attFile) To UBound(attFile)
            &#39;---ファイルが存在してれば添付する
            If objFso.FileExists(attFile(i)) Then
                .AddAttachment attFile(i)
            End If
        Next
    Else
        &#39;---ファイルが存在してれば添付する
        If objFso.FileExists(attFile(i)) Then
            .AddAttachment attFile(i)
        End If
    End If
    .Send
End With

Set cdoMsg = Nothing
Set objFso = Nothing
End Function</code></pre></div>



<p>余談ですがなぜ参照設定をしたのに&#8221;cdoMsg&#8221;のデータ型を&#8221;Object&#8221;にするのかと言うとこれならコピペするだけで（≒参照設定をしなくても）プログラムが利用できるから</p>



<p>自分好みに改修したい時は変更をおススメします</p>



<h3 class="wp-block-heading"><span id="toc10">ざっくり説明</span></h3>



<p>今回のプログラムは特に解説することはないので、このプログラムを使うときのために引数の説明だけ</p>



<p>改修するなら添付ファイルが存在しなかった時の分岐ぐらいでしょうか</p>



<figure class="wp-block-table alignwide"><div class="scrollable-table"><table><thead><tr><th>必須 / 任意</th><th>変数名</th><th>備考</th></tr></thead><tbody><tr><td>必須</td><td>MailSubject</td><td>件名</td></tr><tr><td>必須</td><td>MailBody</td><td>本文</td></tr><tr><td>必須</td><td>ToAddress</td><td>宛先（To）</td></tr><tr><td>任意</td><td>CcAddress </td><td>宛先（Cc）</td></tr><tr><td>任意</td><td>BccAddress </td><td>宛先（Bcc）</td></tr><tr><td>任意</td><td>attFile</td><td>添付ファイル※複数なら配列型に</td></tr></tbody></table></div></figure>



<h2 class="wp-block-heading"><span id="toc11">あとがき</span></h2>



<p>今はAPIやWebhookでLINEとかにも通知できるので出番は減った気もしますがメール通知もまだまだ現役で、きっとGmailが利用頻度が高いのではとまとめてみました</p>



<p>お仕事の中ではOutlookを使う機会も多いと思うのでOutlook版は改めて作ります</p>
]]></content:encoded>
					
					<wfw:commentRss>https://javeo.jp/vba-gmail-send/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
