<?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>Gmail</title>
	<atom:link href="https://javeo.jp/tag/gmail/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>Gmail</title>
	<link>https://javeo.jp</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【Python】PythonでGmailを送る</title>
		<link>https://javeo.jp/python-send-gmail/</link>
					<comments>https://javeo.jp/python-send-gmail/#respond</comments>
		
		<dc:creator><![CDATA[ジャベ雄]]></dc:creator>
		<pubDate>Thu, 23 May 2024 23:00:00 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Gmail]]></category>
		<guid isPermaLink="false">https://javeo.jp/?p=2226</guid>

					<description><![CDATA[目次 2段階承認プロセスを有効化アプリパスワードを生成サンプルプログラムあとがき 2段階承認プロセスを有効化 まずはGoogleアカウントページから&#8221;2 段階認証プロセス&#8220;ページへ 次ページで迷う [&#8230;]]]></description>
										<content:encoded><![CDATA[<div class="codoc-evacuations" style="display:none;" data-shortcode=""></div>
<div class="wp-block-cocoon-blocks-info-box block-box primary-box">
<p><a rel="noreferrer noopener follow" target="_blank" href="https://javeo.jp/vba_gmail_send/">マクロでGmailを送信する</a>方法は前にまとめましたが、同じくPythonでもGmailを送る要件が出てきたのでまとめました</p>



<p>API使うのが正攻法かもですがSMTPの方が楽なので今回はマクロの時と同じくSMTP版を紹介</p>
</div>




  <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">2段階承認プロセスを有効化</a></li><li><a href="#toc2" tabindex="0">アプリパスワードを生成</a></li><li><a href="#toc3" tabindex="0">サンプルプログラム</a></li><li><a href="#toc4" tabindex="0">あとがき</a></li></ol>
    </div>
  </div>

<h2 class="wp-block-heading"><span id="toc1">2段階承認プロセスを有効化</span></h2>



<p>まずは<a rel="noreferrer noopener nofollow" target="_blank" href="https://myaccount.google.com/security" data-type="link" data-id="https://myaccount.google.com/security">Googleアカウント</a>ページから&#8221;<strong>2 段階認証プロセス</strong>&#8220;ページへ</p>



<figure class="wp-block-image size-large"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h46_30.png"><img fetchpriority="high" decoding="async" width="1024" height="508" src="https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h46_30-1024x508.png" alt="" class="wp-image-2227" srcset="https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h46_30-1024x508.png 1024w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h46_30-300x149.png 300w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h46_30-150x74.png 150w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h46_30-768x381.png 768w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h46_30-1536x762.png 1536w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h46_30.png 1920w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>次ページで迷うことなく&#8221;<strong>2 段階認証プロセスを有効にする</strong>&#8220;を</p>



<figure class="wp-block-image size-large"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h54_55.png"><img decoding="async" width="1024" height="508" src="https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h54_55-1024x508.png" alt="" class="wp-image-2228" srcset="https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h54_55-1024x508.png 1024w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h54_55-300x149.png 300w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h54_55-150x74.png 150w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h54_55-768x381.png 768w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h54_55-1536x762.png 1536w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h54_55.png 1920w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>はい、完了</p>



<figure class="wp-block-image size-large"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h57_26.png"><img decoding="async" width="1024" height="508" src="https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h57_26-1024x508.png" alt="" class="wp-image-2229" srcset="https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h57_26-1024x508.png 1024w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h57_26-300x149.png 300w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h57_26-150x74.png 150w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h57_26-768x381.png 768w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h57_26-1536x762.png 1536w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h57_26.png 1920w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>ホームに戻るとそれらしき項目が変わったり増えたり</p>



<figure class="wp-block-image size-large"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h58_32.png"><img decoding="async" width="1024" height="508" src="https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h58_32-1024x508.png" alt="" class="wp-image-2230" srcset="https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h58_32-1024x508.png 1024w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h58_32-300x149.png 300w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h58_32-150x74.png 150w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h58_32-768x381.png 768w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h58_32-1536x762.png 1536w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-23_23h58_32.png 1920w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>



<h2 class="wp-block-heading"><span id="toc2">アプリパスワードを生成</span></h2>



<p>続いてアプリパスワードをするためもう一度&#8221;<strong>2 段階認証プロセス</strong>&#8220;画面を開いて下部にある&#8221;<strong>アプリ パスワード</strong>&#8220;メニューから作成・・・</p>



<figure class="wp-block-image size-large"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2024/05/image-15.png"><img decoding="async" width="1024" height="490" src="https://javeo.jp/wp-content/uploads/2024/05/image-15-1024x490.png" alt="" class="wp-image-2232" srcset="https://javeo.jp/wp-content/uploads/2024/05/image-15-1024x490.png 1024w, https://javeo.jp/wp-content/uploads/2024/05/image-15-300x144.png 300w, https://javeo.jp/wp-content/uploads/2024/05/image-15-150x72.png 150w, https://javeo.jp/wp-content/uploads/2024/05/image-15-768x368.png 768w, https://javeo.jp/wp-content/uploads/2024/05/image-15-1536x735.png 1536w, https://javeo.jp/wp-content/uploads/2024/05/image-15.png 1920w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>したい所ですが、いつの頃からか表示されなくなってしまいました・・・（アプリパスワードの設定がされていれば表示されます）</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>アプリ パスワードは推奨されておらず、ほとんどの場合は不要です。アカウントを安全に保つため、[Google でログイン] を使用してアプリを Google アカウントに接続してください。</p>
</blockquote>



<p>とあるのでGoogle的には非推奨なのでしょうが、直リンクなら今でも開けるので↓↓から</p>




<a rel="noopener noreferrer" target="_blank" href="https://myaccount.google.com/apppasswords" title="Sign in - Google Accounts" class="blogcard-wrap external-blogcard-wrap a-wrap cf"><div class="blogcard external-blogcard eb-left cf"><div class="blogcard-label external-blogcard-label"><span class="fa"></span></div><figure class="blogcard-thumbnail external-blogcard-thumbnail"><img decoding="async" src="https://s.wordpress.com/mshots/v1/https%3A%2F%2Fmyaccount.google.com%2Fapppasswords?w=320&#038;h=198" alt="" class="blogcard-thumb-image external-blogcard-thumb-image" width="320" height="198" /></figure><div class="blogcard-content external-blogcard-content"><div class="blogcard-title external-blogcard-title">Sign in - Google Accounts</div><div class="blogcard-snippet external-blogcard-snippet"></div></div><div class="blogcard-footer external-blogcard-footer cf"><div class="blogcard-site external-blogcard-site"><div class="blogcard-favicon external-blogcard-favicon"><img decoding="async" src="https://www.google.com/s2/favicons?domain=https://myaccount.google.com/apppasswords" alt="" class="blogcard-favicon-image external-blogcard-favicon-image" width="16" height="16" /></div><div class="blogcard-domain external-blogcard-domain">myaccount.google.com</div></div></div></div></a>



<p>&#8220;<strong>アプリ名</strong>&#8220;は適当な名前を設定して&#8221;<strong>作成</strong>&#8220;ボタンをクリック</p>



<figure class="wp-block-image size-large"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2024/05/2024-05-24_00h26_15.png"><img decoding="async" width="1024" height="508" src="https://javeo.jp/wp-content/uploads/2024/05/2024-05-24_00h26_15-1024x508.png" alt="" class="wp-image-2233" srcset="https://javeo.jp/wp-content/uploads/2024/05/2024-05-24_00h26_15-1024x508.png 1024w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-24_00h26_15-300x149.png 300w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-24_00h26_15-150x74.png 150w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-24_00h26_15-768x381.png 768w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-24_00h26_15-1536x762.png 1536w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-24_00h26_15.png 1920w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>無事16桁のパスワードが生成されました！</p>



<figure class="wp-block-image size-large"><a rel="follow noopener noreferrer" target="_blank" href="https://javeo.jp/wp-content/uploads/2024/05/2024-05-24_00h26_58.png"><img decoding="async" width="1024" height="508" src="https://javeo.jp/wp-content/uploads/2024/05/2024-05-24_00h26_58-1024x508.png" alt="" class="wp-image-2234" srcset="https://javeo.jp/wp-content/uploads/2024/05/2024-05-24_00h26_58-1024x508.png 1024w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-24_00h26_58-300x149.png 300w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-24_00h26_58-150x74.png 150w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-24_00h26_58-768x381.png 768w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-24_00h26_58-1536x762.png 1536w, https://javeo.jp/wp-content/uploads/2024/05/2024-05-24_00h26_58.png 1920w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>



<h2 class="wp-block-heading"><span id="toc3">サンプルプログラム</span></h2>



<p>早速↑↑で生成したパスワードを使ってプログラムを作ります</p>



<p>ちなみに、画面上のパスワードをコピーすると4文字毎にスペースがありますが、このスペースは残しても削除してもどちらでもOK</p>



<div class="hcb_wrap"><pre class="prism undefined-numbers lang-python" data-file="send_gmail.py" data-lang="Python"><code>import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication

def send_gmail(mail_subject: str, mail_body: str, mail_to: str, mail_cc: str = &#39;&#39;, file_path: str = &#39;&#39;):
    # アプリパスワードを生成したGoogleアカウント
    sendAddress = &#39;**********@gmail.com&#39;
    # 生成したアプリパスワード
    password = &#39;qwko ejar qsve jspg&#39;

    # メール情報をセット
    msg = MIMEMultipart()
    msg[&#39;Subject&#39;] = mail_subject
    msg[&#39;From&#39;] = sendAddress
    msg[&#39;To&#39;] = mail_to
    if mail_cc != &#39;&#39;:
        msg[&#39;cc&#39;] = mail_cc
    msg.attach(MIMEText(mail_body))

    # 添付ファイルがあれば
    if os.path.isfile(file_path) is True:
        # ファイル読込
        with open(file_path, &#39;rb&#39;) as f:
            attach = MIMEApplication(f.read())
        # ファイル添付
        attach.add_header(&#39;Content-Disposition&#39;, &#39;attachment&#39;, filename=file_path)
        msg.attach(attach)
    # SMTPサーバに接続
    smtp = smtplib.SMTP(&#39;smtp.gmail.com&#39;, 587)
    smtp.ehlo()
    smtp.starttls()
    smtp.login(sendAddress, password)
    # 作成したメールを送信
    smtp.send_message(msg)
    smtp.close()</code></pre></div>



<p>※宛先が複数とか、添付ファイルが複数とか全く考慮していないので必要に応じて修正推奨です・・</p>



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



<p>最近はLINEやChatwork、Teamsなどチャットツール向けに通知することが多いのですが、外部の個人が対企業向けに通知するには難しい場合があり、Gmailで通知する機会がありましてまとめてみました</p>



<p>途中にも書きましたがGoogle的には非推奨で、APIを使うべしなんでしょうが面倒で・・</p>



<p>SMTPはいつか完全に利用できなくなる日が訪れそうなのでその内APIを利用した送信方法もまとめたいです・・</p>
]]></content:encoded>
					
					<wfw:commentRss>https://javeo.jp/python-send-gmail/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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-4" checked><label class="toc-title" for="toc-checkbox-4">目次</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 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>
