Recently, I'm working on a small program which needs to send emails to specific accounts. When I want

to add the function of "Copy to", I encountered a problem that the recipients specified in the Message["CC"]

CANNOT receive the emails. So I search the problem on Stack Overflow:

My code is as follows:

 from email.message import Message
import smtplib def sendEmail(subject, content):
"""
Send Email.
"""
try:
smtpServer = "your smtp server such as smtp.qq.com"
userName = "liuxiaowei@mail.com"
password = "my_passwd_is_abc" fromAddr = "liuxiaowei@mail.com"
toAddrs = ["1@mail.com", "2@mail.com"]
ccAddrs = ["3@mail.com", "4@mail.com"] message = Message()
message["Subject"] = subject
message["From"] = fromAddr
message["To"] = ";".join(toAddrs)
#Copy to
#message["CC"] is only for display, to send the email we must specify it in the method "SMTP.sendmail".
message["CC"] = "3@mail.com;4@mail.com"
message.set_payload(content)
message.set_charset("utf-8")
msg = message.as_string() sm = smtplib.SMTP(smtpServer)
sm.set_debuglevel(0)
sm.ehlo()
sm.starttls()
sm.ehlo()
sm.login(userName, password) sm.sendmail(fromAddr, toAddrs+ccAddrs, msg)
time.sleep(5)
sm.quit()
except Exception, e:
writeLog("EMAIL SENDING ERROR", "", traceback.format_exc())
else:
writeLog("EMAIL SENDING SUCCESS", "", "")

Note the following lines:

#message["CC"] is only for display, to send the email we must specify it in the method "SMTP.sendmail".
message["CC"] = "3@mail.com;4@mail.com"

message["CC"] is only for display. If we want to send the email to anybody, we must specify it in

the second parameter of the method "SMTP.sendmail"
:

sm.sendmail(fromAddr, toAddrs+ccAddrs, msg)

'toAddrs+ccAddrs', This is the key point.

[Python] Send emails to the recepients specified in Message["CC"]的更多相关文章

  1. python send email

    #!/usr/bin/python # -*- coding: UTF-8 -*- # coding:utf8 from smtplib import SMTP_SSL from email.head ...

  2. Python之操作Redis、 RabbitMQ、SQLAlchemy、paramiko、mysql

    一.Redis Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.Redis是一个key-value存储系统.和 ...

  3. Try to write a script to send e-mail but failed

    #-*-coding: utf-8 -*- '''使用Python去发送邮件但是不成功,运行后,等待一段时间, 返回[Errno 10060] A connection attempt failed ...

  4. Step by step configuration of Outgoing Emails from SharePoint to Microsoft Online

    First of all your SharePoint server should be added to Microsoft online safe sender list, so that Sh ...

  5. 转:Python 的 Socket 编程教程

    这是用来快速学习 Python Socket 套接字编程的指南和教程.Python 的 Socket 编程跟 C 语言很像. Python 官方关于 Socket 的函数请看 http://docs. ...

  6. Python用smtplib发送邮件

    参照了下面: 1. 先随便照着试试这个: http://blog.csdn.net/zhaoweikid/article/details/1638349 2. 这个写了一个很简洁的代码,看过NO.1就 ...

  7. 使用python发送简单的邮件

    from:http://blog.csdn.net/zhaoweikid/article/details/125898 前些时间,论坛上有人讨论怎么用python发送需要认证的邮件,我在我的FreeB ...

  8. Python一路走来 RabbitMQ

    一:介绍:(induction) Rabbitmq 是一个消息中间件.他的思想就是:接收和发送消息.你可以把它想成一个邮政局.当你把你的邮件发送到邮箱的,首先你需要确认的是:邮政员先生能把你的邮件发送 ...

  9. python实现邮件发送

    实例补充: #**************************利用STMP自动发送邮件******************************import smtplibsmtp = smtp ...

随机推荐

  1. async and await 简单的入门

    如果有几个Uri,需要获取这些Uri的所有内容的长度之和,你会如何做? 很简单,使用WebClient一个一个的获取uri的内容长度,进行累加. 也就是说如果有5个Uri,请求的时间分别是:1s 2s ...

  2. Unity中的特殊文件夹

    看了雨松MOMO的 Unity3D研究院之手游开发中所有特殊的文件夹学习了,做个笔记. 1.Editor Editor文件夹可以在根目录下,也可以在子目录里,只要名子叫Editor即可.不过我比较喜欢 ...

  3. 如何设置python的编码格式为utf-8

    重装了系统(ubuntu 14.04)原来正常可用的OpenERP项目在切换到开发者模式的时候报错: UnicodeDecodeError: 'ascii' codec can't decode by ...

  4. lnmp集成开发环境安装pdo_dblib扩展

    php连接mssql,获取的结果中文乱码,pdo_dblib扩展使用的是apt-get install php5-sybase方法安装的,尝试了修改freetds.conf php.ini 文件编码 ...

  5. windows 短文件名/短路径名规则

    How Windows Generates 8.3 File Names from Long File Names Windows generates short file names from lo ...

  6. JAVA图像缩放处理

    http://www.blogjava.net/kinkding/archive/2009/05/23/277552.html ———————————————————————————————————— ...

  7. SecureCRT超级终端使用说明

    SecureCRT超级终端使用说明 一.连接POS机 1.运行SecureCRT,选择‘文件’菜单,在下拉菜单中选择‘快速连接’菜单: 2.在弹出的对话框中按如下图选择参数: 3.POS端开机,且数据 ...

  8. 什么是ORM,以及在php上的使用?

    ORM:object relation mapping,即对象关系映射,简单的说就是对象模型和关系模型的一种映射.为什么要有这么一个映射?很简单,因为现在的开发语言基本都是oop的,但是传统的数据库却 ...

  9. kaggle比赛之youtube视频分类示例

    1.训练模型:建bucket,建job,提交运行. BUCKET_NAME=gs://${USER}_yt8m_train_bucket_logisticmodel # (One Time) Crea ...

  10. Oracle安装完成后,如何用命令行启动和关闭数据库?

    Oracle安装完成后,如何用命令行启动和关闭数据库? 解答: 打开:STARTUP [FORCE] [RESTRICT] [PFILE= filename] [OPEN [RECOVER][ dat ...