[python]SMTPによるメール送信のサンプルコード

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import smtplib
from email.MIMEText import MIMEText
from email.Header import Header
from email.Utils import formatdate

FROM_ADDR = "xxx@xxx.xxx.xxx.xxx"
TO_ADDR = "xxx@xxx.xxx.xxx.xxx"
ENCODING = "iso-2022-jp"

message = MIMEText(u"1行目\n2行目\n3行目".encode(ENCODING),"plain",ENCODING,)

message["Subject"] = str(Header(u"Subject",ENCODING))
message["From"] = "%s <%s>" %(str(Header(u"Hiroshi",ENCODING)),FROM_ADDR)
message["To"] = "%s <%s>" %(str(Header(u"To",ENCODING)),TO_ADDR)
message["Date"] = formatdate()

s = smtplib.SMTP("xxx.xxx.xxx.xxx", 587)
s.ehlo()
s.starttls()
s.login("h-ito@nb-npi.co.jp", "Hito5857")

s.sendmail(FROM_ADDR,[TO_ADDR],message.as_string(),)

s.close()

print "Success send mail"