Python-implemented Simple Mail sending script sharing and python mail sending script
Recently, some things need to be monitored and sent to the police. Then I found some materials on the Internet and wrote a simple email sending script, mainly using python's smtplib module. Let's share it with you:
Copy codeThe Code is as follows:
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
# Import smtplib and MIMEText
Import smtplib, sys
From email. mime. text import MIMEText
Def send_mail (sub, content ):
#############
# To whom, send one person here
Mailto_list = ["wangwei03@jb51.net"]
#####################
# Set the server, user name, password, and mailbox suffix
Mail_host = "mail.gyyx.cn"
Mail_user = "wangwei03@jb51.net"
Mail_pass = "123456677890"
Mail_postfix = "gyyx.cn"
######################
'''''
To_list: to whom
Sub: topic
Content: content
Send_mail ("aaa@126.com", "sub", "content ")
'''
Me = mail_user + "<" + mail_user + "@" + mail_postfix + ">"
Msg = MIMEText (content, _ charset = 'gbk ')
Msg ['subobject'] = sub
Msg ['from'] = me
Msg ['to'] = ";". join (mailto_list)
Try:
S = smtplib. SMTP ()
S. connect (mail_host)
S. login (mail_user, mail_pass)
S. sendmail (me, mailto_list, msg. as_string ())
S. close ()
Return True
Except t Exception, e:
Print str (e)
Return False
If _ name _ = '_ main __':
If send_mail (u'this is a python test mail', u'python send mail '):
Print u'sent successfully'
Else:
Print u'sending failed'
Q: Can I use a PYTHON script to send an email? I am a beginner and want to learn more.
Simple smtplib-based
Server = smtplib. SMTP ('10. 30.17.99 ')
Server. set_debuglevel (1)
Server. ehlo ('localhost ')
Server. starttls ()
Server. ehlo ('localhost ')
Server. sendmail (fromaddr, toaddrs, msg)
Server. quit ()
If it is based on the notes COM interface
Def SendEmail (SendTo, CC, BCC, Subject, Body, Attachment = None, Pass = None ):
If SendTo = None:
Return
Session = Dispatch ("Lotus. NotesSession ")
If Pass:
Session. Initialize (Pass)
Server = session. GetEnvironmentString ("MailServer", True)
MaildbName = session. GetEnvironmentString ("MailFile", True)
Db = session. GetDatabase (Server, MaildbName)
Doc = db. CreateDocument ()
Doc. ReplaceItemValue ("Form", "Memo ")
If SendTo:
Doc. ReplaceItemValue ("SendTo", SendTo)
If CC:
Doc. ReplaceItemValue ("CopyTo", SendTo)
If BCC:
Doc. ReplaceItemValue ("BlindCopyTo", SendTo)
If Subject:
Doc. ReplaceItemValue ("Subject", Subject)
Stream = session. CreateStream ()
Stream. WriteText (Body)
BodyMime = doc. CreateMIMEEntity ()
BodyMime. SetContentFromText (stream, "text/html; charsets = iso-8859-1", False)
If Attachment:
RichTextItem = doc. CreateRichTextItem ("Attachment ")
For fn in Attachment:
RichTextItem. EmbedObject (1454, "", fn, "Attachment ")
Doc. Send (False)... the remaining full text>
Q: I have a python-written py script. How can I use a button on winform to trigger the py script? Do not copy or paste
If the python interpreter has been installed and the. py file is associated with the interpreter program, the simple method is:
String pyFile = @ "c: \ test. py ";
System. Diagnostics. Process. Start (pyFile );