科学の箱

科学・IT・登山の話題

Python

pythonからoffice365 smtpを利用してメールを送信

投稿日:2018年9月25日 更新日:

Office365の設定はこちらにある。

PythonにおけるSMTPの利用はこちらにある。

下記はサンプルコード


import smtplib
from email.mime.text import MIMEText

SMTP_SERVER="smtp.office365.com"
SMTP_PORT=25

MAIL_ACCOUNT="PLEASE MODIFY"
MAIL_PASS="PLEASE MODIFY"

MAIL_SUBJECT="This is Subject"
MAIL_BODY="This is Body"

def create_message():
  msg = MIMEText(MAIL_BODY)

  msg['Subject'] = MAIL_SUBJECT
  msg['From'] = MAIL_ACCOUNT
  msg['To'] = MAIL_ACCOUNT
  return msg

if __name__ == '__main__':
  smtp = smtplib.SMTP(SMTP_SERVER,SMTP_PORT)
  smtp.connect(SMTP_SERVER)
  smtp.ehlo()
  smtp.starttls()
  smtp.ehlo()
  smtp.login(MAIL_ACCOUNT, MAIL_PASS)
  msg = create_message()
  smtp.sendmail(MAIL_ACCOUNT, MAIL_ACCOUNT, msg.as_string())
  smtp.quit()

メタ情報

inarticle



メタ情報

inarticle



-Python

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

automated the boring – day9

https://automatetheboringstuff.com/chapter17/ >>> from PIL import Image &gt …

no image

Anaconda Jupyterで自動補完を使う手順

まずはnbexensionsをインストール。これは拡張モジュールを管理する機能。 conda install -y -c conda-forge jupyter_contrib_nbextension …

no image

seleniumでWebElement object is not iterableが出るときの対処方法

iterableなオブジェクトを想定して要素を取得、forループに取り込むと下記のようなWebElement object is not iterableが出る。 結論としては勘違い。 Seleniu …

no image

automated the boring – day 2

今日は簡単なUser認証のプロトタイプといえば恰好はよいが、 パスワードの入力 パスワードの突き合せ パスワードつきあわせの結果によりメッセージを変える name = ‘Mary’ password …

no image

automated the boring – day8

https://automatetheboringstuff.com/chapter15/ さてプログラムを実行しているときに案外出てくる要件が時間計測。 例えばアルゴリズム間でパフォーマンスを比較す …

2018年9月
月 火 水 木 金 土 日
« 6月   10月 »
 12
3456789
10111213141516
17181920212223
24252627282930

side bar top



アーカイブ

カテゴリー