Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
ZJoe
Regular Visitor

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! 

 

1 ACCEPTED SOLUTION

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()

 

 

 

View solution in original post

5 REPLIES 5
FabianSchut
Solution Sage
Solution Sage

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.

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? 

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.

vjingzhanmsft_0-1731318044670.png

 

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!

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()

 

 

 

Great! Thank you for sharing the solution. Please accept your reply as solution, this will help other people find the solution quickly!

 

Best Regards,
Jing

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.