You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用SMTP发送邮件时出现编码问题,求解决方案

SMTP发送邮件编码问题求助

使用SMTP发送邮件时遇到编码问题,查阅多篇Stack Overflow帖子及多种修复方法均无效。以下是移除个人信息后的代码:

import requests
import pandas as pd
import smtplib
import datetime


STOCK = "AAPL"
COMPANY = 'Apple'
TODAY = datetime.date.today()
MY_EMAIL = 
PASSWORD = 
RECEIVER = 


API_KEY = 

parameters = {
    'function': 'TIME_SERIES_DAILY',
    'symbol': STOCK,
    'interval': '60min',
    'apikey': API_KEY,
}

response = requests.get(url='https://www.alphavantage.co/query', params=parameters)
response.raise_for_status()

data = response.json()
stock_data = data['Time Series (Daily)']

today = list(stock_data.values())[0]
yesterday = list(stock_data.values())[1]

prices = [float(today['4. close']), float(yesterday['4. close'])]

price_series = pd.Series(prices)
perc_change = price_series.pct_change()[1]


API_KEY2 = 

parameters2 = {
    'apiKey': API_KEY2,
    'q': COMPANY,
    'searchIn': 'title',
    'from': TODAY_DATE,
    'totalResults': 3,
    'language': 'en',
    'sortBy': 'popularity',
    'pageSize': 3,
    'page': 1,
}

response2 = requests.get(url='https://newsapi.org/v2/everything', params=parameters2)
response2.raise_for_status()

data = response2.json()
article_data = data['articles']

articles = [article_data[num] for num in range(3)]


if perc_change > 0.025 or perc_change < 0.025:
    if perc_change >= 0:
        symbol = '▲'
    else:
        symbol = '▼'

    subject = f'{STOCK}: {symbol} {round(perc_change, 3)}'

    message = f'''
    \033[1mAuthor\033[0m: {list(articles[0].values())[0]['name']}
    \033[1mBrief\033[0m: {list(articles[0].values())[3]}\n
    \033[1mAuthor\033[0m: {list(articles[1].values())[0]['name']}
    \033[1mBrief\033[0m: {list(articles[1].values())[3]}\n
    \033[1mAuthor\033[0m: {list(articles[2].values())[0]['name']}
    \033[1mBrief\033[0m: {list(articles[2].values())[3]}
    '''
    
    with smtplib.SMTP('smtp.gmail.com') as connection:
            connection.starttls()
            connection.login(user=MY_EMAIL, password=PASSWORD)
            connection.sendmail(from_addr=MY_EMAIL, to_addrs=RECEIVER, msg=f"Subject:{subject.encode('utf-8')}\n\n{message.encode('utf-8')}")

目前尝试的最优写法:

msg=f"Subject:{subject.encode('utf-8')}\n\n{message.encode('utf-8')}"

收到的邮件显示如下:

Subject: b'AAPL: \xe2\x96\xbc-0.021'

Body: b"\n \x1b[1mHeadline\x1b[0m: The Verge\n \x1b[1mBrief\x1b[0m: Samsung just announced the Galaxy Watch 5 Pro for outdoor athletes and Apple is also expected to release a rugged version of the Apple Watch this fall. But smartwatches can\xe2\x80\x99t rely too heavily on touchscreens to win over athletes.\n\n \x1b[1mHeadline\x1b[0m: Ars Technica\n \x1b[1mBrief\x1b[0m: Dealmaster also has the Xbox Series S, Apple Watch Series 7, and Nvidia Shield TV.\n\n \x1b[1mHeadline\x1b[0m: Boing Boing\n \x1b[1mBrief\x1b[0m: One Strange Thing is a podcast that investigates unexplainable news stories. I began listening to it last night on Apple Podcasts, and ended up playing 5 episodes. You can listen to a selection of free episodes on the podcast's website, too. \xe2\x80\x94 Read the rest\n " 

已尝试使用EmailMessage、Header、MIMEText等工具,也更换过不同邮件服务商,但问题依旧。请问哪里操作有误?


内容的提问来源于stack exchange,提问作者andrew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 10:54:14