Python-implemented Simple Mail sending script sharing and python mail sending script

Source: Internet
Author: User

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 );

Related Article

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.