#code:utf-8

#code:utf-8

率先段代码:

python 发送邮件实例

文件格局的邮件

[python] view
plaincopy

  1. #!/usr/bin/env python3  
  2. #coding: utf-8  
  3. import smtplib  
  4. from email.mime.text import MIMEText  
  5. from email.header import Header  
  6.   
  7. sender = ‘***’  
  8. receiver = ‘***’  
  9. subject = ‘python email test’  
  10. smtpserver = ‘smtp.163.com’  
  11. username = ‘***’  
  12. password = ‘***’  
  13.   
  14. msg = MIMEText(‘你好’,’text’,’utf-8′)#华语需参数‘utf-8’,单字节字符不需要  
  15. msg[‘Subject’] = Header(subject, ‘utf-8’)  
  16.   
  17. smtp = smtplib.SMTP()  
  18. smtp.connect(‘smtp.163.com’)  
  19. smtp.login(username, password)  
  20. smtp.sendmail(sender, receiver, msg.as_string())  
  21. smtp.quit()  

HTML方式的邮件

[python] view
plaincopy

  1. #!/usr/bin/env python3  
  2. #coding: utf-8  
  3. import smtplib  
  4. from email.mime.text import MIMEText  
  5.   
  6. sender = ‘***’  
  7. receiver = ‘***’  
  8. subject = ‘python email test’  
  9. smtpserver = ‘smtp.163.com’  
  10. username = ‘***’  
  11. password = ‘***’  
  12.   
  13. msg = MIMEText(‘<html><h1>你好</h1></html>’,’html’,’utf-8′)  
  14.   
  15. msg[‘Subject’] = subject  
  16.   
  17. smtp = smtplib.SMTP()  
  18. smtp.connect(‘smtp.163.com’)  
  19. smtp.login(username, password)  
  20. smtp.sendmail(sender, receiver, msg.as_string())  
  21. smtp.quit()  

带图片的HTML邮件

[python] view
plaincopy

  1. #!/usr/bin/env python3  
  2. #coding: utf-8  
  3. import smtplib  
  4. from email.mime.multipart import MIMEMultipart  
  5. from email.mime.text import MIMEText  
  6. from email.mime.image import MIMEImage  
  7.   
  8. sender = ‘***’  
  9. receiver = ‘***’  
  10. subject = ‘python email test’  
  11. smtpserver = ‘smtp.163.com’  
  12. username = ‘***’  
  13. password = ‘***’  
  14.   
  15. 【澳门金沙国际】python发送邮件的实例代码,发送邮件案例。msgRoot = MIMEMultipart(‘related’)  
  16. msgRoot[‘Subject’] = ‘test message’  
  17.   
  18. msgText = MIMEText(‘<b>Some <i>HTML</i> text</b> and an image.<br><img src=”cid:image1″><br>good!’,’html’,’utf-8′)  
  19. msgRoot.attach(msgText)  
  20.   
  21. fp = open(‘h:\\python\\1.jpg’, ‘rb’)  
  22. msgImage = MIMEImage(fp.read())  
  23. fp.close()  
  24.   
  25. msgImage.add_header(‘Content-ID’, ‘<image1>’)  
  26. msgRoot.attach(msgImage)  
  27.   
  28. smtp = smtplib.SMTP()  
  29. smtp.connect(‘smtp.163.com’)  
  30. smtp.login(username, password)  
  31. smtp.sendmail(sender, receiver, msgRoot.as_string())  
  32. smtp.quit()  

带附件的邮件

[python] view
plaincopy

  1. #!/usr/bin/env python3  
  2. #coding: utf-8  
  3. import smtplib  
  4. from email.mime.multipart import MIMEMultipart  
  5. from email.mime.text import MIMEText  
  6. from email.mime.image import MIMEImage  
  7.   
  8. sender = ‘***’  
  9. receiver = ‘***’  
  10. subject = ‘python email test’  
  11. smtpserver = ‘smtp.163.com’  
  12. username = ‘***’  
  13. password = ‘***’  
  14.   
  15. msgRoot = MIMEMultipart(‘related’)  
  16. msgRoot[‘Subject’] = ‘test message’  
  17.   
  18. #布局附件  
  19. att = MIMEText(open(‘h:\\python\\1.jpg’, ‘rb’).read(), ‘base64’, ‘utf-8’)  
  20. att[“Content-Type”] = ‘application/octet-stream’  
  21. att[“Content-Disposition”] = ‘attachment; filename=”1.jpg”‘  
  22. msgRoot.attach(att)  
  23.           
  24. smtp = smtplib.SMTP()  
  25. smtp.connect(‘smtp.163.com’)  
  26. smtp.login(username, password)  
  27. smtp.sendmail(sender, receiver, msgRoot.as_string())  
  28. smtp.quit()  

群邮件

[python] view
plaincopy

  1. #!/usr/bin/env python3  
  2. #coding: utf-8  
  3. import smtplib  
  4. from email.mime.text import MIMEText  
  5.   
  6. sender = ‘***’  
  7. receiver = [‘***’,’****’,……]  
  8. subject = ‘python email test’  
  9. smtpserver = ‘smtp.163.com’  
  10. username = ‘***’  
  11. password = ‘***’  
  12.   
  13. msg = MIMEText(‘你好’,’text’,’utf-8′)  
  14.   
  15. msg[‘Subject’] = subject  
  16.   
  17. smtp = smtplib.SMTP()  
  18. smtp.connect(‘smtp.163.com’)  
  19. smtp.login(username, password)  
  20. smtp.sendmail(sender, receiver, msg.as_string())  
  21. smtp.quit()  

各个要素都富含的邮件

[python] view
plaincopy

  1. #!/usr/bin/env python3  
  2. #coding: utf-8  
  3. import smtplib  
  4. from email.mime.multipart import MIMEMultipart  
  5. from email.mime.text import MIMEText  
  6. from email.mime.image import MIMEImage  
  7.   
  8. sender = ‘***’  
  9. receiver = ‘***’  
  10. subject = ‘python email test’  
  11. smtpserver = ‘smtp.163.com’  
  12. username = ‘***’  
  13. password = ‘***’  
  14.   
  15. # Create message container – the correct MIME type is multipart/alternative.  
  16. msg = MIMEMultipart(‘alternative’)  
  17. msg[‘Subject’] = “Link”  
  18.   
  19. # Create the body of the message (a plain-text and an HTML version).  
  20. text = “Hi!\nHow are you?\nHere is the link you wanted:\n”  
  21. html = “””\ 
  22. <html> 
  23.   <head></head> 
  24.   <body> 
  25.     <p>Hi!<br> 
  26.        How are you?<br> 
  27.        Here is the <a href=”; you wanted. 
  28.     </p> 
  29.   </body> 
  30. </html> 
  31. “””  
  32.   
  33. # Record the MIME types of both parts – text/plain and text/html.  
  34. part1 = MIMEText(text, ‘plain’)  
  35. part2 = MIMEText(html, ‘html’)  
  36.   
  37. # Attach parts into message container.  
  38. # According to RFC 2046, the last part of a multipart message, in this case  
  39. # the HTML message, is best and preferred.  
  40. msg.attach(part1)  
  41. msg.attach(part2)  
  42. #结构附件  
  43. att = MIMEText(open(‘h:\\python\\1.jpg’, ‘rb’).read(), ‘base64’, ‘utf-8’)  
  44. att[“Content-Type”] = ‘application/octet-stream’  
  45. att[“Content-Disposition”] = ‘attachment; filename=”1.jpg”‘  
  46. msg.attach(att)  
  47.      
  48. smtp = smtplib.SMTP()  
  49. smtp.connect(‘smtp.163.com’)  
  50. smtp.login(username, password)  
  51. smtp.sendmail(sender, receiver, msg.as_string())  
  52. smtp.quit()  

基于SSL的邮件

[python] view
plaincopy

  1. #!/usr/bin/env python3  
  2. #coding: utf-8  
  3. import smtplib  
  4. from email.mime.text import MIMEText  
  5. from email.header import Header  
  6. sender = ‘***’  
  7. receiver = ‘***’  
  8. subject = ‘python email test’  
  9. smtpserver = ‘smtp.163.com’  
  10. username = ‘***’  
  11. password = ‘***’  
  12.   
  13. msg = MIMEText(‘你好’,’text’,’utf-8′)#华语需参数‘utf-8’,单字节字符不要求  
  14. msg[‘Subject’] = Header(subject, ‘utf-8’)  
  15.   
  16. smtp = smtplib.SMTP()  
  17. smtp.connect(‘smtp.163.com’)  
  18. smtp.ehlo()  
  19. smtp.starttls()  
  20. smtp.ehlo()  
  21. smtp.set_debuglevel(1)  
  22. smtp.login(username, password)  
  23. smtp.sendmail(sender, receiver, msg.as_string())  
  24. smtp.quit()  

    文件方式的邮件

    [python] view
    plaincopy

1.  \#!/usr/bin/env python3  
2.  \#coding: utf-8  
3.  import smtplib  
4.  from email.mime.text import MIMEText  
5.  from email.header import Header  
6.    
7.  sender = '\*\*\*'  
8.  receiver = '\*\*\*'  
9.  subject = 'python email test'  
10. smtpserver = 'smtp.163.com'  
11. username = '\*\*\*'  
12. password = '\*\*\*'  
13.   
14. msg = MIMEText('你好','text','utf-8')\#中文需参数‘utf-8’,单字节字符不需要  
15. msg\['Subject'\] = Header(subject, 'utf-8')  
16.   
17. smtp = smtplib.SMTP()  
18. smtp.connect('smtp.163.com')  
19. smtp.login(username, password)  
20. smtp.sendmail(sender, receiver, msg.as\_string())  
21. smtp.quit()  

**HTML形式的邮件**

**\[python\]** [view
plain](http://blog.csdn.net/wyuan8913/article/details/6917873)[copy](http://blog.csdn.net/wyuan8913/article/details/6917873)


1.  \#!/usr/bin/env python3  
2.  \#coding: utf-8  
3.  import smtplib  
4.  from email.mime.text import MIMEText  
5.    
6.  sender = '\*\*\*'  
7.  receiver = '\*\*\*'  
8.  subject = 'python email test'  
9.  smtpserver = 'smtp.163.com'  
10. username = '\*\*\*'  
11. password = '\*\*\*'  
12.   
13. msg = MIMEText('&lt;html&gt;&lt;h1&gt;你好&lt;/h1&gt;&lt;/html&gt;','html','utf-8')  
14.   
15. msg\['Subject'\] = subject  
16.   
17. smtp = smtplib.SMTP()  
18. smtp.connect('smtp.163.com')  
19. smtp.login(username, password)  
20. smtp.sendmail(sender, receiver, msg.as\_string())  
21. smtp.quit()  

**带图片的HTML邮件**

**\[python\]** [view
plain](http://blog.csdn.net/wyuan8913/article/details/6917873)[copy](http://blog.csdn.net/wyuan8913/article/details/6917873)


1.  \#!/usr/bin/env python3  
2.  \#coding: utf-8  
3.  import smtplib  
4.  from email.mime.multipart import MIMEMultipart  
5.  from email.mime.text import MIMEText  
6.  from email.mime.image import MIMEImage  
7.    
8.  sender = '\*\*\*'  
9.  receiver = '\*\*\*'  
10. subject = 'python email test'  
11. smtpserver = 'smtp.163.com'  
12. username = '\*\*\*'  
13. password = '\*\*\*'  
14.   
15. msgRoot = MIMEMultipart('related')  
16. msgRoot\['Subject'\] = 'test message'  
17.   
18. msgText = MIMEText('&lt;b&gt;Some &lt;i&gt;HTML&lt;/i&gt; text&lt;/b&gt; and an image.&lt;br&gt;&lt;img src="cid:image1"&gt;&lt;br&gt;good!','html','utf-8')  
19. msgRoot.attach(msgText)  
20.   
21. fp = open('h:\\\\python\\\\1.jpg', 'rb')  
22. msgImage = MIMEImage(fp.read())  
23. fp.close()  
24.   
25. msgImage.add\_header('Content-ID', '&lt;image1&gt;')  
26. msgRoot.attach(msgImage)  
27.   
28. smtp = smtplib.SMTP()  
29. smtp.connect('smtp.163.com')  
30. smtp.login(username, password)  
31. smtp.sendmail(sender, receiver, msgRoot.as\_string())  
32. smtp.quit()  

**带附件的邮件**

**\[python\]** [view
plain](http://blog.csdn.net/wyuan8913/article/details/6917873)[copy](http://blog.csdn.net/wyuan8913/article/details/6917873)


1.  \#!/usr/bin/env python3  
2.  \#coding: utf-8  
3.  import smtplib  
4.  from email.mime.multipart import MIMEMultipart  
5.  from email.mime.text import MIMEText  
6.  from email.mime.image import MIMEImage  
7.    
8.  sender = '\*\*\*'  
9.  receiver = '\*\*\*'  
10. subject = 'python email test'  
11. smtpserver = 'smtp.163.com'  
12. username = '\*\*\*'  
13. password = '\*\*\*'  
14.   
15. msgRoot = MIMEMultipart('related')  
16. msgRoot\['Subject'\] = 'test message'  
17.   
18. \#构造附件  
19. att = MIMEText(open('h:\\\\python\\\\1.jpg', 'rb').read(), 'base64', 'utf-8')  
20. att\["Content-Type"\] = 'application/octet-stream'  
21. att\["Content-Disposition"\] = 'attachment; filename="1.jpg"'  
22. msgRoot.attach(att)  
23.           
24. smtp = smtplib.SMTP()  
25. smtp.connect('smtp.163.com')  
26. smtp.login(username, password)  
27. smtp.sendmail(sender, receiver, msgRoot.as\_string())  
28. smtp.quit()  

**群邮件**

**\[python\]** [view
plain](http://blog.csdn.net/wyuan8913/article/details/6917873)[copy](http://blog.csdn.net/wyuan8913/article/details/6917873)


1.  \#!/usr/bin/env python3  
2.  \#coding: utf-8  
3.  import smtplib  
4.  from email.mime.text import MIMEText  
5.    
6.  sender = '\*\*\*'  
7.  receiver = \['\*\*\*','\*\*\*\*',……\]  
8.  subject = 'python email test'  
9.  smtpserver = 'smtp.163.com'  
10. username = '\*\*\*'  
11. password = '\*\*\*'  
12.   
13. msg = MIMEText('你好','text','utf-8')  
14.   
15. msg\['Subject'\] = subject  
16.   
17. smtp = smtplib.SMTP()  
18. smtp.connect('smtp.163.com')  
19. smtp.login(username, password)  
20. smtp.sendmail(sender, receiver, msg.as\_string())  
21. smtp.quit()  

**各种元素都包含的邮件**

**\[python\]** [view
plain](http://blog.csdn.net/wyuan8913/article/details/6917873)[copy](http://blog.csdn.net/wyuan8913/article/details/6917873)


1.  \#!/usr/bin/env python3  
2.  \#coding: utf-8  
3.  import smtplib  
4.  from email.mime.multipart import MIMEMultipart  
5.  from email.mime.text import MIMEText  
6.  from email.mime.image import MIMEImage  
7.    
8.  sender = '\*\*\*'  
9.  receiver = '\*\*\*'  
10. subject = 'python email test'  
11. smtpserver = 'smtp.163.com'  
12. username = '\*\*\*'  
13. password = '\*\*\*'  
14.   
15. \# Create message container - the correct MIME type is multipart/alternative.  
16. msg = MIMEMultipart('alternative')  
17. msg\['Subject'\] = "Link"  
18.   
19. \# Create the body of the message (a plain-text and an HTML version).  
20. text = "Hi!\\nHow are you?\\nHere is the link you wanted:\\nhttp://www.python.org"  
21. html = """\\ 
22. &lt;html&gt; 
23.   &lt;head&gt;&lt;/head&gt; 
24.   &lt;body&gt; 
25.     &lt;p&gt;Hi!&lt;br&gt; 
26.        How are you?&lt;br&gt; 
27.        Here is the &lt;a href="http://www.python.org"&gt;link&lt;/a&gt; you wanted. 
28.     &lt;/p&gt; 
29.   &lt;/body&gt; 
30. &lt;/html&gt; 
31. """  
32.   
33. \# Record the MIME types of both parts - text/plain and text/html.  
34. part1 = MIMEText(text, 'plain')  
35. part2 = MIMEText(html, 'html')  
36.   
37. \# Attach parts into message container.  
38. \# According to RFC 2046, the last part of a multipart message, in this case  
39. \# the HTML message, is best and preferred.  
40. msg.attach(part1)  
41. msg.attach(part2)  
42. \#构造附件  
43. att = MIMEText(open('h:\\\\python\\\\1.jpg', 'rb').read(), 'base64', 'utf-8')  
44. att\["Content-Type"\] = 'application/octet-stream'  
45. att\["Content-Disposition"\] = 'attachment; filename="1.jpg"'  
46. msg.attach(att)  
47.      
48. smtp = smtplib.SMTP()  
49. smtp.connect('smtp.163.com')  
50. smtp.login(username, password)  
51. smtp.sendmail(sender, receiver, msg.as\_string())  
52. smtp.quit()  

**基于SSL的邮件**

**\[python\]** [view
plain](http://blog.csdn.net/wyuan8913/article/details/6917873)[copy](http://blog.csdn.net/wyuan8913/article/details/6917873)


1.  \#!/usr/bin/env python3  
2.  \#coding: utf-8  
3.  import smtplib  
4.  from email.mime.text import MIMEText  
5.  from email.header import Header  
6.  sender = '\*\*\*'  
7.  receiver = '\*\*\*'  
8.  subject = 'python email test'  
9.  smtpserver = 'smtp.163.com'  
10. username = '\*\*\*'  
11. password = '\*\*\*'  
12.   
13. msg = MIMEText('你好','text','utf-8')\#中文需参数‘utf-8’,单字节字符不需要  
14. msg\['Subject'\] = Header(subject, 'utf-8')  
15.   
16. smtp = smtplib.SMTP()  
17. smtp.connect('smtp.163.com')  
18. smtp.ehlo()  
19. smtp.starttls()  
20. smtp.ehlo()  
21. smtp.set\_debuglevel(1)  
22. smtp.login(username, password)  
23. smtp.sendmail(sender, receiver, msg.as\_string())  
24. smtp.quit()   

 

 

 

G

M

T

 

Detect language Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bengali Bosnian Bulgarian Catalan Cebuano Chichewa Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Esperanto Estonian Filipino Finnish French Galician Georgian German Greek Gujarati Haitian Creole Hausa Hebrew Hindi Hmong Hungarian Icelandic Igbo Indonesian Irish Italian Japanese Javanese Kannada Kazakh Khmer Korean Lao Latin Latvian Lithuanian Macedonian Malagasy Malay Malayalam Maltese Maori Marathi Mongolian Myanmar (Burmese) Nepali Norwegian Persian Polish Portuguese Punjabi Romanian Russian Serbian Sesotho Sinhala Slovak Slovenian Somali Spanish Sundanese Swahili Swedish Tajik Tamil Telugu Thai Turkish Ukrainian Urdu Uzbek Vietnamese Welsh Yiddish Yoruba Zulu   Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bengali Bosnian Bulgarian Catalan Cebuano Chichewa Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Esperanto Estonian Filipino Finnish French Galician Georgian German Greek Gujarati Haitian Creole Hausa Hebrew Hindi Hmong Hungarian Icelandic Igbo Indonesian Irish Italian Japanese Javanese Kannada Kazakh Khmer Korean Lao Latin Latvian Lithuanian Macedonian Malagasy Malay Malayalam Maltese Maori Marathi Mongolian Myanmar (Burmese) Nepali Norwegian Persian Polish Portuguese Punjabi Romanian Russian Serbian Sesotho Sinhala Slovak Slovenian Somali Spanish Sundanese Swahili Swedish Tajik Tamil Telugu Thai Turkish Ukrainian Urdu Uzbek Vietnamese Welsh Yiddish Yoruba Zulu          

 

 

 

Text-to-speech function is limited to 200 characters

 

  Options : History : Feedback : Donate Close

import smtplib from email.mime.text import
MIMEText

import smtplib from email.mime.text import
MIMEText

复制代码 代码如下:

from email.mime.multipart import
MIMEMultipart

from email.mime.multipart import
MIMEMultipart

#!/usr/bin/python
# -*- coding: utf-8 -*-

from email.header import Header

from email.header import Header

import email
import mimetypes
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
import smtplib

smtpserver = ‘smtp.163.com’

smtpserver = ‘smtp.163.com’

def sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText):

smtp_user = ‘*******’

smtp_user = ‘*******’

        strFrom = fromAdd
        strTo = ‘, ‘.join(toAdd)

smtp_pass = ‘*******’

smtp_pass = ‘*******’

        server = authInfo.get(‘server’)
        user = authInfo.get(‘user’)
        passwd = authInfo.get(‘password’)

from_addr=’**********@163.com’

from_addr=’**********@163.com’

        if not (server and user and passwd) :
                print ‘incomplete login info, exit now’
                return

to_addr= ‘********@qq.com’

to_addr= ‘********@qq.com’

        # 设定root信息
        msgRoot = MIMEMultipart(‘related’)
        msgRoot[‘Subject’] = subject
        msgRoot[‘From’] = strFrom
        msgRoot[‘To’] = strTo
        msgRoot.preamble = ‘This is a multi-part message in MIME
format.’

subject = ‘休假申请’

subject = ‘休假申请’

        # Encapsulate the plain and HTML versions of the message body
in an
        # ‘alternative’ part, so message agents can decide which they
want to display.
        msgAlternative = MIMEMultipart(‘alternative’)
        msgRoot.attach(msgAlternative)

#message = MIMEMultipart()

#message = MIMEMultipart()

        #设定纯文本信息
        msgText = MIMEText(plainText, ‘plain’, ‘utf-8’)
        msgAlternative.attach(msgText)

message =
MIMEText(‘那是今天的假期申请’,’plain’,’utf-8′)

message =
MIMEText(‘那是明天的假期申请’,’plain’,’utf-8′)

        #设定HTML信息
        msgText = MIMEText(htmlText, ‘html’, ‘utf-8’)
        msgAlternative.attach(msgText)

message[‘Subject’] =
Header(subject,’utf-8′)

message[‘Subject’] =
Header(subject,’utf-8′)

       #设定内置图片新闻
        fp = open(‘test.jpg’, ‘rb’)
        msgImage = MIMEImage(fp.read())
        fp.close()
        msgImage.add_header(‘Content-ID’, ‘<image1>’)
        msgRoot.attach(msgImage)

message[‘From’] = from_addr

message[‘From’] = from_addr

       #出殡邮件
        smtp = smtplib.SMTP()
       #设定调节级别,依景况而定
        smtp.set_debuglevel(1)
        smtp.connect(server)
        smtp.login(user, passwd)
        smtp.sendmail(strFrom, strTo, msgRoot.as_string())
        smtp.quit()
        return

message[‘To’] = to_addr

message[‘To’] = to_addr

if __name__ == ‘__main__’ :
        authInfo = {}
        authInfo[‘server’] = ‘smtp.somehost.com’
        authInfo[‘user’] = ‘username’
        authInfo[‘password’] = ‘password’
        fromAdd =
‘[email protected]’
        toAdd =
[‘[email protected]’,
‘[email protected]’]
        subject = ‘邮件主题’
        plainText = ‘那里是普普通通文本’
        htmlText = ‘<B>HTML文本</B>’
        sendEmail(authInfo, fromAdd, toAdd, subject, plainText,
htmlText)

#诠释部分为带附件的代码

#表明部分为带附件的代码

文本格局的邮件

#message.attach(message_text)

#message.attach(message_text)

复制代码 代码如下:

#att1=MIMEText(open(‘test.txt’,’rb’).read(),’base64′,’utf-8′)

#att1=MIMEText(open(‘test.txt’,’rb’).read(),’base64′,’utf-8′)

#!/usr/bin/env python3  
#coding: utf-8  
澳门金沙国际,import smtplib  
from email.mime.text import MIMEText  
from email.header import Header  

#att1[‘Content-Type’] =
‘application/octet-stream’

#att1[‘Content-Type’] =
‘application/octet-stream’

sender = ‘***’  
receiver = ‘***’  
subject = ‘python email test’  
smtpserver = ‘smtp.163.com’  
username = ‘***’  
password = ‘***’  

#att1[‘Content-Disposition’] =
‘attachment:filename=”test.txt”‘

#att1[‘Content-Disposition’] =
‘attachment:filename=”test.txt”‘

msg =
MIMEText(‘你好’,’text’,’utf-8′)#汉语需参数‘utf-8’,单字节字符不需求  
msg[‘Subject’] = Header(subject, ‘utf-8’)  

#message.attach(att1)

#message.attach(att1)

smtp = smtplib.SMTP()  
smtp.connect(‘smtp.163.com’)  
smtp.login(username, password)  
smtp.sendmail(sender, receiver, msg.as_string())  
smtp.quit()  

sm=smtplib.SMTP()

sm=smtplib.SMTP()

HTML方式的邮件

sm.connect(smtpserver,25)

sm.connect(smtpserver,25)

复制代码 代码如下:

sm.login(smtp_user,smtp_pass)

sm.login(smtp_user,smtp_pass)

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText

sm.sendmail(from_addr,to_addr,message.as_string())

sm.sendmail(from_addr,to_addr,message.as_string())

sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtp.163.com’
username = ‘***’
password = ‘***’

sm.quit()

sm.quit()

msg =
MIMEText(‘<html><h1>你好</h1></html>’,’html’,’utf-8′)

msg[‘Subject’] = subject

smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

带图片的HTML邮件

复制代码 代码如下:

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtp.163.com’
username = ‘***’
password = ‘***’

msgRoot = MIMEMultipart(‘related’)
msgRoot[‘Subject’] = ‘test message’

msgText = MIMEText(‘<b>Some <i>HTML</i> text</b>
and an image.<br><img
src=”cid:image1″><br>good!’,’html’,’utf-8′)
msgRoot.attach(msgText)

fp = open(‘h:\\python\\1.jpg’, ‘rb’)
msgImage = MIMEImage(fp.read())
fp.close()

msgImage.add_header(‘Content-ID’, ‘<image1>’)
msgRoot.attach(msgImage)

smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()

带附件的邮件

复制代码 代码如下:

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtp.163.com’
username = ‘***’
password = ‘***’

msgRoot = MIMEMultipart(‘related’)
msgRoot[‘Subject’] = ‘test message’

#结构附件
att = MIMEText(open(‘h:\\python\\1.jpg’, ‘rb’).read(), ‘base64’,
‘utf-8’)
att[“Content-Type”] = ‘application/octet-stream’
att[“Content-Disposition”] = ‘attachment; filename=”1.jpg”‘
msgRoot.attach(att)

smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()

群邮件

复制代码 代码如下:

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText

sender = ‘***’
receiver = [‘***’,’****’,……]
subject = ‘python email test’
smtpserver = ‘smtp.163.com’
username = ‘***’
password = ‘***’

msg = MIMEText(‘你好’,’plain’,’utf-8′)

msg[‘Subject’] = subject

smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

各样要素都包罗的邮件

复制代码 代码如下:

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtp.163.com’
username = ‘***’
password = ‘***’

# Create message container – the correct MIME type is
multipart/alternative.
msg = MIMEMultipart(‘alternative’)
msg[‘Subject’] = “Link”

# Create the body of the message (a plain-text and an HTML version).
text = “Hi!\nHow are you?\nHere is the link you
wanted:\n”
html = “””\
<html>
  <head></head>
  <body>
    <p>Hi!<br>
       How are you?<br>
       Here is the <a href=”;
you wanted.
    </p>
  </body>
</html>
“””

# Record the MIME types of both parts – text/plain and text/html.
part1 = MIMEText(text, ‘plain’)
part2 = MIMEText(html, ‘html’)

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this
case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
#社团附件
att = MIMEText(open(‘h:\\python\\1.jpg’, ‘rb’).read(), ‘base64’,
‘utf-8’)
att[“Content-Type”] = ‘application/octet-stream’
att[“Content-Disposition”] = ‘attachment; filename=”1.jpg”‘
msg.attach(att)

smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

基于SSL的邮件

复制代码 代码如下:

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtp.163.com’
username = ‘***’
password = ‘***’

msg =
MIMEText(‘你好’,’plain’,’utf-8′)#汉语需参数‘utf-8’,单字节字符不须要
msg[‘Subject’] = Header(subject, ‘utf-8’)

smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.set_debuglevel(1)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

复制代码 代码如下:
#!/usr/bin/python # -*- coding: utf-8 -*- import email import
mimetypes from email.MIMEMultipart import MIMEMultipart from
email.MIMET…

相关文章