Python Send mail script

Source: Internet
Author: User

Tried a lot of times can not succeed, and then find this to send successfully!

#!/usr/bin/python
#coding: Utf-8
Import Smtplib
From Email.mime.text import Mimetext
From email.utils import formataddr
My_sender= "
My_user= "
DEF Mail ():
Ret=true
Try
Msg=mimetext (' fill in message contents ', ' Plain ', ' utf-8 ')
msg[' from ']=formataddr (["aaa", my_sender])
msg[' to ']=formataddr (["bbb", my_user])
msg[' Subject ']= "bbb"

Server=smtplib. SMTP ("smtp.163.com") #发件人邮箱中的SMTP服务器, Port is 25
Server.login (my_sender, "")
Server.sendmail (my_sender,[my_user,],msg.as_string ()) #括号中对应的是发件人邮箱账号, recipient's Email account, Email
Server.quit () #这句是关闭连接的意思
Except Exception: #如果try中的语句没有执行, The following ret=false is executed
Ret=false
return ret

Ret=mail ()
If Ret:
Print ("ok") #如果发送成功则会返回ok, wait 20 seconds or so to receive mail
Else
Print ("filed") #如果发送失败则会返回filed

Python Send mail script

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.