开启邮件SMTP服务
1. SMTP服务是什么?
SMTP(Simple Mail Transfer Protocol,简单邮件传输协议)是用于发送和接收电子邮件的通信协议,是电子邮件系统中最基础的协议之一。它负责在邮件服务器之间传输邮件,但在客户端中,通常只用于发送邮件。
简单来说,SMTP服务的作用是发送电子邮件,而开启SMTP服务可以让我们使用编程语言发送邮件
2. 开启SMTP服务
以QQ邮箱为例,登陆QQ邮箱,点击界面右上角账号与安全,也可能需要先点击右上角头像然后点击账号与安全(qq邮箱如果绑定了微信则界面会不一样)
随后点击 安全设置 -> 开启服务
微信扫码发送短信后点击我已发送
复制授权码(保存好这个授权码),然后点击返回就完成了
3. 发送邮件
普通文本
以python为例发送一个电子邮件,注意修改发、收件人和授权码
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart
smtp_server = 'smtp.qq.com' smtp_port = 465 sender_email = '2*****76@qq.com' receiver_email = '2*****76@qq.com' password = '修改为你的授权码'
subject = 'Test Email' body = 'This is a test email sent using Python.'
msg = MIMEMultipart() msg['From'] = sender_email msg['To'] = receiver_email msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
try: server = smtplib.SMTP_SSL(smtp_server, smtp_port) server.login(sender_email, password) server.sendmail(sender_email, receiver_email, msg.as_string()) server.quit() print('Email sent successfully!') except Exception as e: print(f'Failed to send email: {e}')
|
HTML内容
也可以发送html格式的文本内容,以表格为例,将正文改为表格,然后将MIMEText格式改为html就可以了
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart
smtp_server = 'smtp.qq.com' smtp_port = 465 sender_email = '230453176@qq.com' receiver_email = '230453176@qq.com' password = 'jlegvgancaltbijc'
subject = 'Test Email' body = ''' <html> <body> <h2>芝士表格</h2> <table border="1" cellpadding="5" cellspacing="0"> <tr> <th>姓名</th> <th>年龄</th> </tr> <tr> <td>clb</td> <td>18</td> </tr> <tr> <td>zs</td> <td>20</td> </tr> </table> </body> </html> '''
msg = MIMEMultipart() msg['From'] = sender_email msg['To'] = receiver_email msg['Subject'] = subject
msg.attach(MIMEText(body, 'html'))
try: server = smtplib.SMTP_SSL(smtp_server, smtp_port) server.login(sender_email, password) server.sendmail(sender_email, receiver_email, msg.as_string()) server.quit() print('Email sent successfully!') except Exception as e: print(f'Failed to send email: {e}')
|