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.

最新文章

  1. 关于shiro
  2. shell中对字符串的处理
  3. 8.0/9.0 Email 设置
  4. Asp.Net 配置IISExpress允许外部访问
  5. hdu 2092
  6. linux 进程综合指令
  7. ASP 调用dll(VB)及封装dll实例
  8. Eclipse3.6 添加JUnit源代码
  9. Linux环境进程间通信(三):消息队列
  10. 开发日记:JsonCSharpHelp
  11. 实验五:任意输入10个int类型数据,排序输出,再找出素数
  12. o.s.b.d.LoggingFailureAnalysisReporter
  13. RabbitMQ使用技巧
  14. MySQL5.7修改默认密码、随机密码
  15. How to ignore files and directories in subversion?
  16. 转 Java的 BigDecimal类型比较大小
  17. C#获取并修改文件扩展名的方法
  18. MySQL注射绕过技巧(二)
  19. SNOI2017(BZOJ5015~5018)泛做
  20. 【推荐】CentOS安装vsftpd-3.0.3+安全配置

热门文章

  1. pipe_wait问题_转
  2. Ubuntu安装新版本nodejs的5种姿势
  3. Oracle SQL Developer 日期格式显示设置
  4. 当input被选中时候获取改input的多个属性值
  5. 响应式网页设计:rem、em设置网页字体大小自适应
  6. Gradle学习系列之一——Gradle快速入门(转)
  7. python笔记2-数据类型:字符串常用操作
  8. 第一百四十五节,JavaScript,同步动画
  9. oracle 里 插入空字符串会被转成null插入
  10. 【POJ】2942 Knights of the Round Table(双连通分量)