使用Python发送电子邮件时出现SSL错误

时间:2020-03-25 12:57:53

标签: python html python-3.x ssl html-table

使用python发送电子邮件时出现错误 [SSL: SSLV3_ALERT_BAD_RECORD_MAC] sslv3 alert bad record mac (_ssl.c:2607)

我正在尝试发送HTML格式的电子邮件

我的代码

def sendingemail():
    html_part1 ="""\
        <!DOCTYPE html>
        <html>
        <head>
            <style>
                table, th, td {
                    border: 1px solid black;
                }
            </style>
        </head>
            <body>
                <h1 style="color:SlateGray;">NEW</h1>
                <table style="width:50%">
    """ 

    html_part3 ="""\
            </table>
            </body>
        </html>
    """

    html_part2="""\
        <tr>
            <td>Name</td>
            <td>ID</td>
        </tr>
    """

    file1 = open('mydata.txt', 'r') 
    Lines = file1.readlines()

    for line in Lines:
         html_part2=html_part2+'<tr>'+line.strip()+'</tr>'
         print(line.strip()) 

    msg = EmailMessage()
    msg['Subject'] = 'subject'
    msg['From'] = 'sender's emailaddress'
    msg['To'] = 'receiver's emailaddress'
    msg.set_content('content')
    msg.add_alternative(html_part1+html_part2+html_part3, subtype='html')

    file1.close()

    with smtplib.SMTP_SSL('smtp.gmail.com',465) as smtp:
        smtp.login('sender's emailaddress','mypassword')
        smtp.send_message(msg)

sendingemail()

何时出现此错误?: 只有在mydata.txt文件中的行数大于 10 时,我才收到此错误,否则它会很好地工作

我在这里想念什么?

整个错误:

Traceback (most recent call last):
  File "C:\Users\rohan\AppData\Local\Programs\Python\Python38-32\lib\smtplib.py", line 391, in getreply
    line = self.file.readline(_MAXLINE + 1)
  File "C:\Users\rohan\AppData\Local\Programs\Python\Python38-32\lib\socket.py", line 669, in readinto
    return self._sock.recv_into(b)
  File "C:\Users\rohan\AppData\Local\Programs\Python\Python38-32\lib\ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)
  File "C:\Users\rohan\AppData\Local\Programs\Python\Python38-32\lib\ssl.py", line 1099, in read
    return self._sslobj.read(len, buffer)
ssl.SSLError: [SSL: SSLV3_ALERT_BAD_RECORD_MAC] sslv3 alert bad record mac (_ssl.c:2607)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 55, in <module>
    sendingemail()
  File "main.py", line 53, in sendingemail
    smtp.send_message(msg)
  File "C:\Users\rohan\AppData\Local\Programs\Python\Python38-32\lib\smtplib.py", line 970, in send_message
    return self.sendmail(from_addr, to_addrs, flatmsg, mail_options,
  File "C:\Users\rohan\AppData\Local\Programs\Python\Python38-32\lib\smtplib.py", line 886, in sendmail
    (code, resp) = self.data(msg)
  File "C:\Users\rohan\AppData\Local\Programs\Python\Python38-32\lib\smtplib.py", line 573, in data
    (code, msg) = self.getreply()
  File "C:\Users\rohan\AppData\Local\Programs\Python\Python38-32\lib\smtplib.py", line 394, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed: "
smtplib.SMTPServerDisconnected: Connection unexpectedly closed: [SSL: SSLV3_ALERT_BAD_RECORD_MAC] sslv3 alert bad record mac (_ssl.c:2607)

mydata.txt文件如下所示:

<td> 'name' </td> <td> 'id' </td>
<td> 'name' </td> <td> 'id' </td>
<td> 'name' </td> <td> 'id' </td>
<td> 'name' </td> <td> 'id' </td>
<td> 'name' </td> <td> 'id' </td>
<td> 'name' </td> <td> 'id' </td>
<td> 'name' </td> <td> 'id' </td>
<td> 'name' </td> <td> 'id' </td>
<td> 'name' </td> <td> 'id' </td>
<td> 'name' </td> <td> 'id' </td>

0 个答案:

没有答案