Loading [MathJax]/jax/output/HTML-CSS/config.js

Python smtplibでメール送信

Table of Contents

目的

pythonからSMTPサーバにメールを送信する。

環境

  • Windows10
  • Python 3.7.3 (Anaconda 4.7.10)

コード

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# stmplibの読み込み
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
 
 
fromaddr = 'fromaddress@python-mail.com'
toaddr = 'toaddress@python-mail.com'
 
# MIMEMultipartのインスタンス作成
msg = MIMEMultipart()
 
#送信元アドレス設定
msg['From'] = fromaddr
 
#送信先アドレス設定
msg['To'] = toaddr
 
#メール件名設定
msg['Subject'] = "Test email"
 
#本文設定
body = "maint_text"
msg.attach(MIMEText(body,'plain'))
 
#送信先SMTPサーバの指定
server = smtplib.SMTP('hostname',port_number)
 
#送信処理
server.send_message(msg)
 
#終了
server.quit()

以上

おすすめ

コメントを残す

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