Python Mail Send script (linux,windows) generic

Source: Internet
Author: User
Tags email account

Script
#!/usr/bin/python#-*-coding:utf-8-*-#Python Mail for chenglee#if fileformat=dos, update fileformat=unix#code:set Fileformat=unix#check:set ff? import smtplibfrom email.mime.text import mimetextfrom email.utils import Formataddrmy_ Sender= ' [email protected] ' # sender email account my_pass = ' xxxxxxxxxxx ' # Sender's mailbox password (at that time request SMTP password) my_user= ' [EMAIL&NB        Sp;protected] ' # recipient email account, my side sent to myself Def Mail (): Ret=true try:msg=mimetext (' 11 fill in the message content ', ' plain ', ' utf-8 ')              msg[' from ']=formataddr (["Sender nickname", My_sender]) # corresponding sender's mailbox nickname in parentheses, Sender's mailbox account msg[' to ']=formataddr (["Recipient nickname", My_user]) # The corresponding recipient's mailbox nickname in parentheses, the recipient's email account msg[' Subject ']= "11 mail subject-Test" # The subject of the message, can also be said to be the title SERVER=SMTPLIB.S        Mtp_ssl ("smtp.qq.com", 465) # The SMTP server in the sender's mailbox, the port is 465 server.login (My_sender, my_pass) # brackets correspond to the sender's mailbox account, email password  Server.sendmail (My_sender,[my_user,],msg.as_string ()) # in parentheses corresponds to the sender's mailbox account, recipient's mailbox account, outgoing mail server.quit () # Close connection except exception:# if the statement in the try is not executed, the following Ret=false Ret=false return Retret=mail () if Ret:print ("message sent successfully") Else:print ("message sent Failed")   

When used under Windows, the first two lines can be left!

Test Results

Windows

Linux

Tips

As a sender, two services are required.

Python Mail Send script (linux,windows) generic

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.