Python傳送郵件問題

2021-07-16 05:47:58 字數 1268 閱讀 4429

用python**發檔案,之前遇到很多次問題,也解決了很多次,後來沒有記錄,**也不見了,所以每次都弄到弄去很麻煩,還是記錄一下為妙。

(按理其他也一樣)

#!/usr/bin/python

# -*- coding: utf-8 -*-

import smtplib

from email.mime.text import mimetext

from email.header import header

# 第三方 smtp 服務

mail_host="smtp.sina.com"

#設定伺服器

mail_user="[email protected]"

#使用者名稱

mail_pass="password"

#口令

#this one is also username too.

sender = '[email protected]'

# 接收郵件,可設定為你的qq郵箱或者其他郵箱

receivers = ['[email protected]']

message = mimetext('this is content of the email', 'plain', 'utf-8')

#this on also must be the sender's address

message['from'] = "[email protected]"

message['to'] = "my fans"

#receiver's name could be customized

subject = 'be serious'

#title

message['subject'] = header(subject, 'utf-8')

try:

smtpobj = smtplib.smtp()

smtpobj.connect(mail_host, 25) # 25 為 smtp 埠號

smtpobj.login(mail_user,mail_pass)

smtpobj.sendmail(sender, receivers, message.as_string())

print

"郵件傳送成功"

except smtplib.smtpexception as e:

print

"error: cannot send my email"

print e

郵件傳送問題

environment.rb裡配置 require smtp tls actionmailer base.delivery method smtp 以簡單郵件傳送協議傳送郵件 actionmailer base.default charset gbk 設定郵件的預設編碼為國標碼否則傳送的郵件主題可能...

html傳送郵件 Python傳送郵件(三十)

簡單郵件傳輸協議 smtp 是一種協議,用於在郵件伺服器之間傳送電子郵件和路由電子郵件。python提供smtplib模組,該模組定義了乙個smtp客戶端會話物件,可用於使用smtp或esmtp偵聽器守護程式向任何網際網路機器傳送郵件。這是乙個簡單的語法,用來建立乙個smtp物件,稍後將演示如何用它...

python 傳送郵件

coding utf 8 import smtplib from email.mime.text import mimetext from email.header import header 檔案形式的郵件 def email file mail host smtp.qq.com 郵箱伺服器 ma...