一、注册163邮箱,略。
二、开启SMTP服务,获取授权码,使用代码连接邮箱的时候不是使用邮箱密码直接连接,而是使用授权码连接。
三、使用python编写代码,这里是把发送邮件封装成一个类了。
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText ''' python 使用163邮箱发送邮件的示例 ''' class EmailSender: #163邮箱的smtp服务地址:smtp.163.com 端口:25 def __init__(self, sender_email, password, smtp_server="smtp.163.com", smtp_port=25): self.sender_email = sender_email self.password = password self.smtp_server = smtp_server self.smtp_port = smtp_port def send_email(self, receiver_email, subject, body): message = MIMEMultipart() message["From"] = self.sender_email message["To"] = receiver_email message["Subject"] = subject message.attach(MIMEText(body, "plain")) with smtplib.SMTP(self.smtp_server, self.smtp_port) as server: server.starttls() server.login(self.sender_email, self.password) server.sendmail(self.sender_email, receiver_email, message.as_string()) # 使用示例 sender_email = "xxxxxxxxx@163.com" password = "你的授权码" email_sender = EmailSender(sender_email, password) receiver_email = "接收者邮箱" subject = "Test Email from Python" body = "Hello, this is a test email sent from Python!" email_sender.send_email(receiver_email, subject, body)s四
四、代码执行后可以在邮箱中看到已经发成功。
ps:其他的邮箱服务商的接入方式也类似,需要提供邮箱账号、密码/授权码、邮箱服务地址、邮箱服务端口,然后就收件人的信息和邮件内容了。
test