1. Using the local SendMail protocol for Mail sending
Format (1): Smtpobj=smtplib. SMTP ([host [, Port [, Local_hostname]])
HOST:SMTP the IP address or domain name of the server host
Port: Port number of the service (default is 25)
Local_hostname: The address of the server (localhost by default)
Format (2): Smtp.sendmail (FROM_ADDR), To_addrs,msg[,mail_options,rcpt_options]
FROM_ADDR: Address sent by mail
TO_ADDR: Mail Receive address
Msg: Send Message
[Email protected] tmp]# vim smtplib.py
#!/bin/env python
#!-*-Coding:utf-8-*-
Import Smtplib
From Email.mime.text import Mimetext
From Email.header Import Header
sender= ' [email protected] '
receivers= ' [email protected] '
Message=mimetext (' Python Mail send Test ... ', ' plain ', ' utf-8 ')
message[' from ']=header (' Rookie tutorial ', ' Utf-8 ')
message[' to ']=header (' Test ', ' utf-8 ')
subject= ' Python SMTP mail test '
message[' Subject ']=header (Subject, ' utf-8 ')
Try
Smtpobj=smtplib. SMTP (' localhost ')
Smtpobj.sendmail (sender,receivers,message.as_string ())
Print "Mail sent successfully"
Except Smtplib. Smtpexception:
Print "Error: unable to send mail"
[email protected] tmp]# python smtplib.py
Message sent successfully
[Email protected] tmp]#
This article is from the "Days Together" blog, please be sure to keep this source http://tongcheng.blog.51cto.com/6214144/1766856
Python module: smtplib module