我正在尝试编辑一个文本文件,然后使用 python 脚本将其作为电子邮件正文发送,但我收到了 unicode 编码错误。经过一番研究,我发现解决方案是使用 .encode('utf-8') 方法,但这对我不起作用,因为 sendmail() 方法只发送字符串
这是我使用的python代码片段:
irtem = open('irtemplate.txt')
data = irtem.read().replace('(name)', eng_name).replace('(customer)',
cu_name).replace('(sr)', SR_num).replace('(problem)',
prob_description).replace('(email)', eng_email).replace('(details)',
details_req).replace('(tele)', eng_tele)
message_text = data
message = "From: %s\r\n" % fromaddr + "To: %s\r\n" % toaddr + "CC:
%s\r\n" % ",".join(cc) + "Subject: %s\r\n" % message_subject + "\r\n" +
message_text
toaddrs = [toaddr] + cc + bcc
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, message)
server.quit()
Run Code Online (Sandbox Code Playgroud)
追溯:
Traceback (most recent call last):
File "autoIR.py", line 39, in <module>
server.sendmail(fromaddr, toaddrs, message)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", …Run Code Online (Sandbox Code Playgroud) 调用函数时
testFunc(),我没有使用语法Apples.testFunc()。但是代码运行成功。为何如此?
class Apples {
public static void main(String args[] ) {
testFunc();
}
public static void testFunc() {
System.out.println("Hello world!");
}
}
Run Code Online (Sandbox Code Playgroud)