Python implementation message send complete code (with attachment Send method)

Source: Internet
Author: User

Example one: Using SMTP and email to send mail, with attachments (full code)

__author__ = ' Administrator '
#coding =gb2312

From email. Header Import Header
From email. Mimetext Import Mimetext
From email. Mimemultipart Import Mimemultipart
Import Smtplib, DateTime


Def SendMailAttach ():

msg = Mimemultipart ()
ATT = mimetext (open (' Test. xlsx ', ' RB '). Read (), ' base64 ', ' gb2312 ')
att["Content-type"] = ' application/octet-stream '
att["content-disposition"] = ' attachment; Filename= "Test. xlsx"
Msg.attach (ATT)

Msg[' to '] = ' [email protected] '
Msg[' from '] = ' [email protected] '
msg[' CC ']= ' [email protected] '
msg[' Subject ' = Header (' Smoke test results (' + str (datetime.date.today ()) + ') ', ' gb2312 ')

BODY = "Python Test Mail"
Msg.attach (Mimetext (body, ' plain '))

Server = Smtplib. SMTP (' 58.62.220.52 ', 25)
Server.login ("User name", "password")
Msg_text=msg.as_string ()
Server.sendmail (msg[' from '], msg[' to '],msg_text)
Server.close

Example two: Using SMTP to achieve Simple mail delivery. (Send without attachment)

__author__ = ' Administrator '
#coding =utf-8
Import Smtplib
msg=["From:[email protected]", ' to:[email protected] ', "Subject:test Demo"]
Msgbody= "Hello World"
Smtp=smtplib. SMTP ()
Smtp.connect ("58.62.220.52", 25)
Smtp.login ("User name", "password")
Smtp.sendmail ("[Email protected]", "[email protected]", "\r\n\r\n". Join (("\ r \ n". Join (msg), msgbody)))
Print "Mail sent successfully! "
Smtp.quit ()

Python implementation message send complete code (with attachment Send method)

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.