Python Send mail

Source: Internet
Author: User

Use Python to finish sending messages

1, the basic information required by the mail

2. Python sends mail

1, the basic information required by the mail

Sender's server, sender, recipient (whether multiple), subject, content, whether there are attachments

2. Python sends mail

Import Smtplib
Import Mimetext  #此模块可以用于发送正文
Import Mimemultipart #此模块用于发送带附件的邮件


#邮件基础信息配置
"Smtp.163.com" #发件服务器, QQ mailbox for smtp.qq.com
0    #端口, QQ mailbox is 465
"[Email protected]" #发件箱
"XXXX"  #qq邮箱为授权码
"[Email protected]" #收件箱, multiple mailboxes for ["[Email protected]","[email protected]"]
Msg=mimemultipart ()
msg["from"]=sender
msg["to"]=receiver  #发给单个邮箱
#msg ["to"]=";". Join (receiver)  #发给多个邮箱
msg["subject"]="test"

Body=mimetext (Mail_body,"html","Utf-8") 
Msg.attach (body)                               #读取附件中的正文作为邮件正文

#附件信息的配置
File_path=r"D:\result.html"  #读取本地的一个一个测试报告
With open (File_path, asfp:
    Mail_body=fp.read ()
Att=mimetext (Mail_body,"Basse64","Utf-8") 
att["content-type"]="Application/octet-stream  "
att["content-disposition"]=' attachment; filename= "test_report.html" '  #将系统中的result. HTML rename as Attachment send 
Msg.attach (ATT)

#兼容一般邮箱和qq邮箱
Try
    SMTP = Smtplib. SMTP ()
    Smtp.connect (smtpserver)  #连接服务器, general mailbox, such as 163
    Smtp.login (SENDER,PSW)    #登录
Except
    Smtp=smtplib. Smtp_ssl (Smtpserver,port) #连接服务器, QQ mailbox
    Smtp.login (SENDER,PSW)    #登录
Smtp.sendmail (Sender,receiver,msg.as_string ()) #发送
Smtp.quit ()    #关闭





Python Send mail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.