Forum Discussion

ZJoe's avatar
ZJoe
Frequent Visitor
1 year ago
Solved

Send email from notebook getting "SMTPHeloError: (501, b'5.5.4 Invalid domain name....."

Hi, I'm trying to send email with a python notebook, using smtplib.

all the code works fine on my desktop using a py script, but when I migrate it to the notebook, it failed with error below: 

 

SMTPHeloError: (501, b'5.5.4 Invalid domain name [BY5PR17CA0023.namprd17.prod.outlook.com 2024-11-11T00:38:48.583Z 08DD01A7D38EEEA9]')

 

Here is the debug info with "set_debuglevel(1)":

 

send: 'ehlo vm-db444797.\r\n'
reply: b'501 5.5.4 Invalid domain name [BY5PR17CA0023.namprd17.prod.outlook.com 2024-11-11T00:38:43.583Z 08DD01A7D38EEEA9]\r\n'
reply: retcode (501); Msg: b'5.5.4 Invalid domain name [BY5PR17CA0023.namprd17.prod.outlook.com 2024-11-11T00:38:43.583Z 08DD01A7D38EEEA9]'
send: 'helo vm-db444797.\r\n'
reply: b'501 5.5.4 Invalid domain name [BY5PR17CA0023.namprd17.prod.outlook.com 2024-11-11T00:38:48.583Z 08DD01A7D38EEEA9]\r\n'
reply: retcode (501); Msg: b'5.5.4 Invalid domain name [BY5PR17CA0023.namprd17.prod.outlook.com 2024-11-11T00:38:48.583Z 08DD01A7D38EEEA9]'
send: 'QUIT\r\n'
reply: b'221 2.0.0 Service closing transmission channel\r\n'
reply: retcode (221); Msg: b'2.0.0 Service closing transmission channel'

 

and the code snippet I used is as follows: 

 

with smtplib.SMTP(self.__smtp_host, self.__smtp_port) as s:
    if debug:
        s.set_debuglevel(1)
    s.starttls()
    s.login(self._from, self.__pwd) 
    s.send_message(self.msg)

 

Is this a notebook related issue? and how should we fix it?

 

Thanks! 

 

  • ZJoe's avatar
    ZJoe
    1 year ago

    Thanks for your try! but finally I need to add this notebook to a pipeline and make it running automatically, so using the VS Code may not help.

     

    And as you said, an invalid domain name may be generated in a Fabric portal, and I had found a same issue in stackoverflow (but it's not in a Fabric notebook) . 

    I tried to add the codes it mentioned, and it works, by explicitly setting sender host name in the ehlo command to anything lowercase.

     

    import smtplib
    server_365 = smtplib.SMTP('smtp.office365.com', '587')
    server_365.ehlo('mylowercasehost')
    server_365.starttls()
    server_365.ehlo('mylowercasehost')

     

     what's more, I found another way to write the code in the docs of smtplib, it also works, as follows:

     

    import smtplib
    server_365 = smtplib.SMTP('smtp.office365.com', '587', local_hostname='mylowercasehost')
    server_365.starttls()

     

     

     

6 Replies

  • Hi, what SMTP server are you using to set up your SMTP client? It seems like the server you provided may be only available on your local device and not in the Python Notebook.

    • ZJoe's avatar
      ZJoe
      Frequent Visitor

      Thanks for the reply, the SMTP sever info I used is as follows:

      {
          "SMTP": {
              "sever": "smtp.office365.com",
              "port": 587,
              "encrypt": "TLS"
          }
      }

       As you said,  is there another different SMTP sever available in notebook? 

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi ZJoe 

         

        I tried the codes from this blog (How to send emails using Python and Office365 SMTP server | by Vipul Bajaj | Medium) in a Fabric notebook and got the same "SMTPHeloError: (501, b'5.5.4 Invalid domain name....." error in Fabric portal. 

         

        However, when I ran the same Fabric notebook in the VS Code with Synapse VS Code extension, and used Fabric runtime 1.2, I sent the email successfully! You may give this a try.

         

        It seems when running the notebook in Fabric portal, it automatically generates an invalid domain name which has some timestamp and other information at the end. However a valid domain name should probably ends with ".com". Maybe we can consider update the domain name in the notebook to provide a valid one, but I haven't figure out whether this is possible. 

         

        SMTPHeloError: (501, b'5.5.4 Invalid domain name [BY5PR17CA0023.namprd17.prod.outlook.com 2024-11-11T00:38:48.583Z 08DD01A7D38EEEA9]')

         

        Best Regards,
        Jing
        If this post helps, please Accept it as Solution to help other members find it. Appreciate your Kudos!

  • Great Job!!! Thank you for sharing the solution. 

    Best Regards,

    Hugo