用python写一个发送邮件的代码
时间: 2023-03-24 15:00:54 浏览: 74
好的,下面是用 Python 写的一个发送邮件的代码示例:
```python
import smtplib
# 发件人的邮箱地址和密码
from_addr = 'xxxx@xxx.com'
password = 'xxxx'
# 收件人的邮箱地址
to_addr = 'xxxx@xxx.com'
# 邮件主题
subject = '这是一封测试邮件'
# 邮件内容
content = '这是一封测试邮件,请忽略'
# 连接到 SMTP 服务器
server = smtplib.SMTP('smtp.gmail.com', 587) # gmail 使用的是 587 端口
server.starttls()
# 登录到邮箱
server.login(from_addr, password)
# 发送邮件
server.sendmail(from_addr, to_addr, f'Subject: {subject}\n{content}')
# 退出服务器
server.quit()
```
这里我们使用了 Python 内置的 `smtplib` 库来实现发送邮件的功能。该库支持使用 SMTP 协议发送邮件。
在代码中,我们需要填入发件人的邮箱地址和密码,收件人的邮箱地址,邮件的主题和内容,然后就可以连接到 SMTP 服务器,登录到邮箱并发送邮件了。
注意:这里使用了 Gmail 邮箱作为例子,因此连接的是 Gmail 的 SMTP 服务器,如果使用其他邮箱,需要更换对应的 SMTP 服务器地址和端口。
相关问题
用Python写一个异步邮件发送代码
以下是用Python写一个异步邮件发送代码的示例:
```python
import asyncio
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.utils import COMMASPACE, formatdate
async def send_email(from_addr, to_addrs, subject, message, attachments=None):
message = MIMEMultipart()
message['From'] = from_addr
message['To'] = COMMASPACE.join(to_addrs)
message['Date'] = formatdate(localtime=True)
message['Subject'] = subject
text = MIMEText(message)
message.attach(text)
if attachments:
for attachment in attachments:
with open(attachment, 'rb') as file:
img_data = file.read()
image = MIMEImage(img_data, name=attachment.split('/')[-1])
message.attach(image)
smtp_server = smtplib.SMTP('smtp.gmail.com', 587)
smtp_server.starttls()
smtp_server.login(from_addr, 'password')
await smtp_server.sendmail(from_addr, to_addrs, message.as_string())
smtp_server.quit()
loop = asyncio.get_event_loop()
loop.run_until_complete(send_email('sender@gmail.com', ['recipient@gmail.com'], 'Test email', 'This is a test email', ['image.png']))
loop.close()
```
此代码使用Python标准库中的smtplib和asyncio模块。在send_email函数中,我们创建了一个MIMEMultipart对象,该对象包含了邮件的文本和附件。然后我们连接到SMTP服务器,使用starttls()方法启用TLS加密,并登录到SMTP服务器。最后,我们使用sendmail()方法发送邮件,并使用quit()方法关闭SMTP连接。
在主程序中,我们使用asyncio模块的get_event_loop()方法获取事件循环,然后使用run_until_complete()方法运行异步任务。最后,我们使用close()方法关闭事件循环。
用python写一个自动定时发送邮件代码
好的,以下是用 Python 实现的自动定时发送邮件的代码:
```python
import smtplib
import datetime
import time
# 发送方的邮箱信息
sender_email = "your_email@example.com"
sender_password = "your_email_password"
# 接收方的邮箱信息
receiver_email = "recipient_email@example.com"
# 邮件主题和内容
subject = "Test Email"
body = "This is a test email sent by Python."
# 发送邮件的函数
def send_email():
# 连接 SMTP 服务器
server = smtplib.SMTP("smtp.gmail.com", 587) # 以 Gmail 为例
server.ehlo()
server.starttls()
server.login(sender_email, sender_password)
# 构造邮件内容
message = f"Subject: {subject}\n\n{body}"
# 发送邮件
server.sendmail(sender_email, receiver_email, message)
print("Email sent successfully!")
# 断开连接
server.quit()
# 设置定时发送邮件的时间
scheduled_time = datetime.datetime(2022, 8, 1, 9, 0, 0) # 发送时间为 2022 年 8 月 1 日早上 9 点
# 等待到达发送时间
while datetime.datetime.now() < scheduled_time:
time.sleep(1)
# 发送邮件
send_email()
```
以上代码会在设定的时间自动发送一封邮件,你可以根据需要修改邮件内容、收发邮箱等参数。