---
# System prepended metadata

title: send email example

---

# send email example

```python=
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib


content = MIMEMultipart()
content["subject"] = "標題"  #郵件標題
content["from"] = "你的免洗帳號@gmail.com"  #寄件者
content["to"] = "你的真實帳號@gmail.com" #收件者
content.attach(MIMEText("郵件內文"))

with smtplib.SMTP(host="smtp.gmail.com", port="587") as smtp:  # 設定SMTP伺服器
    try:
        smtp.ehlo()  # 驗證SMTP伺服器
        smtp.starttls()  # 建立加密傳輸
        smtp.login("你的免洗帳號@gmail.com", "應用程式密碼")  # 登入寄件者gmail
        smtp.send_message(content)  # 寄送郵件
        print("Success!")
    except Exception as e:
        print("Failed! Error is: ", e)

```