在python中接收和发送电子邮件 [英] Receive and send emails in python

查看:180
本文介绍了在python中接收和发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在python中收发电子邮件?一个'邮件服务器'的种类。

How can i receive and send email in python? A 'mail server' of sorts.

我正在研究制作一个应用程序,听取它是否收到一封发给foo@bar.domain.com的电子邮件,以及发送电子邮件给发件人。

I am looking into making an app that listens to see if it recieves an email addressed to foo@bar.domain.com, and sends an email to the sender.

现在,我能够在python中做到这一点,最好使用第三方库吗?

Now, am i able to do this all in python, would it be best to use 3rd party libraries?

推荐答案

这是一个非常简单的例子:

Here is a very simple example:

import smtplib

server = 'mail.server.com'
user = ''
password = ''

recipients = ['user@mail.com', 'other@mail.com']
sender = 'you@mail.com'
message = 'Hello World'

session = smtplib.SMTP(server)
# if your SMTP server doesn't need authentications,
# you don't need the following line:
session.login(user, password)
session.sendmail(sender, recipients, message)

有关更多选项,错误处理等,请查看 smtplib模块文档

For more options, error handling, etc, look at the smtplib module documentation.

这篇关于在python中接收和发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆