尝试使用 Python 发送邮件时出现“UnicodeDecodeError”

问题描述

我正在尝试使用 python 发送电子邮件,但每次尝试发送时,我都会收到 UnicodeDecodeError。我哪里做错了?我无法通过在网上搜索来弄清楚。

这是我的完整代码

import smtplib
import random
import datetime

# Random name list
names = ['Jack','Kevin','Laura']

# Datetime get time
date = datetime.datetime.Now().strftime('%a,%b %d')

# Random order id's
r0 = random.randint(702,711)
r1 = random.randint(1111111,9999999)
r2 = random.randint(1111111,9999999)
r3 = round(random.uniform(13,25),4)

# Mail content
mailc = """Hi {}! You did it buddy: #{}
Service: Standard
*Profit: S$ {}
Send it no later than {}...
""".format(random.choice(names),{r0}-{r1}-{r2},{r3},date)

# Subject
sub = '(.LO) New toy: {}-{}-{}'.format({r0},{r1},{r2})

# Mail adreesses and password
senderAddress = '[email protected]'
senderPass = 'xxyyww'
receiverAddress = '[email protected]'

# Server
server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.login(senderAddress,senderPass)

# Message
message = 'Subject: {}\n\n{}'.format(sub,mailc)
server.sendmail(senderAddress,receiverAddress,message)
server.quit()

print("Mail sent.")

这是错误

Traceback (most recent call last):
  File "C:\Users\WINDOWS 8.1\Desktop\automaticOrderEmailSender.py",line 35,in
<module>
    server = smtplib.SMTP('smtp.gmail.com',587)
  File "C:\Users\WINDOWS\AppData\Local\Programs\Python\python37-32\lib\smtpl
ib.py",line 261,in __init__
    fqdn = socket.getfqdn()
  File "C:\Users\WINDOWS\AppData\Local\Programs\Python\python37-32\lib\socke
t.py",line 676,in getfqdn
    hostname,aliases,ipaddrs = gethostbyaddr(name)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf0 in position 1: invalid
continuation byte

希望能帮到你。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)