python 傳送郵件,附件郵件

2021-08-19 19:22:19 字數 2243 閱讀 5425

1,傳送郵件

import smtplib

from email.mime.text import mimetext

msg_from = '[email protected]'

# 傳送方郵箱

passwd = 'srui1028'

# 填入傳送方郵箱的授權碼

msg_to = '[email protected]'

# 收件人郵箱

subject = "python郵件測試"

# 主題

content = "這是我使用python smtplib及email模組傳送的郵件"

# 正文

msg = mimetext(content)

msg['subject'] = subject

msg['from'] = msg_from

msg['to'] = msg_to

try:

s = smtplib.smtp_ssl('smtp.163.com',465) # 郵件伺服器及埠號

# s = smtplib.smtp('smtp.163.com', 25) # 郵件伺服器及埠號

s.login(msg_from,passwd)

s.sendmail(msg_from, msg_to, msg.as_string())

print ("傳送成功")

except:

print ("傳送失敗")

2,傳送附件郵件

#coding=utf-8

import smtplib

from email.mime.text import mimetext # mimrtext()定義郵件正文

from email.mime.multipart import mimemultipart # mimemulipart模組構造帶附件

# 傳送郵件的伺服器

smtpserver = 'smtp.163.com'

# 傳送郵件使用者和授權碼

user = '[email protected]'

password = 'srui1028'

# 傳送者

sender = '[email protected]'

# 接收者

receiver = '[email protected]'

# 郵件主題

subject = "附件的郵件"

# 傳送附件

# sendfile = open('c:\\users\\sunruirui\\desktop\\11.txt', 'r').read() #傳送附件txt格式

sendfile = open('d:

\\python36

\\tingjiandan

\\result.html', 'rb').read() #傳送附件html格式

att = mimetext(sendfile, 'plain', 'utf-8')

# att["content-disposition"] = "attachment;filename = 'html.txt'" #傳送附件txt格式

att["content-disposition"] = "attachment;filename = 'html.html'"

#傳送附件html格式

# 正文

content = "這是我使用python smtplib及email模組傳送的郵件"

msgtext = mimetext(content)

msgroot = mimemultipart('related')

msgroot['subject'] = subject

msgroot['from'] = sender

msgroot['to'] = receiver

msgroot.attach(msgtext) #新增正文

msgroot.attach(att) #傳送檔案格式

try:

smtp = smtplib.smtp()

smtp.connect(smtpserver)

smtp.login(user, password)

smtp.sendmail(sender, receiver, msgroot.as_string())

print ('傳送成功')

smtp.quit()

except:

print ('傳送失敗')

c 傳送郵件 附件

winform窗體 如下 using system using system.collections.generic using system.componentmodel using system.data using system.drawing using system.linq using ...

C 傳送郵件(附件)類

本部落格主要利用c 中system.net.mail中的mailmessage 類進行郵件的傳送。話不多說直接上 首先需要新增引用,不清楚的可以去 上去檢視詳細介紹 using system using system.collections generic using system.configur...

php 通過sendcloud傳送郵件 附件功能

function send mail 配置檔案 method post 郵件傳送必須是post header content type multipart form data boundary mime boundary eol,content data context stream context...