6

I am trying to connect with my company's email server using smtplib in python Below is my code snippet:

server = smtplib.SMTP('mail.mycompany.com',25)
server.ehlo()        
server.starttls()
server.login('my_email@mycompany.com', 'my_password')

When I run my code I get the following error:

smtplib.SMTPException: STARTTLS extension not supported by server.

After searching this error on google i found that my company's smtp server does not support starttls authentication so i have to remove server.starttls() this line from my code but when i remove this line I get following error:

smtplib.SMTPException: SMTP AUTH extension not supported by server.

I have spent whole day to search these errors but didn't find any solution.

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
  • What if you do ehlo before and after tls? – user193661 Nov 09 '15 at 07:16
  • What if you use smtplib.SMTPS? Port 465. You're sure you have the right port? – user193661 Nov 09 '15 at 07:16
  • I have used ehlo before and after too but still getting the same error _smtplib.SMTPException: STARTTLS extension not supported by server_ Yes the setting are provided by our email admin so port is correct also I have tried to connect using other ports (465,587) and getting timeout on them. – Husnain Taseer Nov 09 '15 at 07:24
  • http://stackoverflow.com/questions/9216127/smtp-auth-extension-not-supported-by-server-in-python-2-4 Does this help? Try connecting before doing anything. – user193661 Nov 09 '15 at 07:40
  • `server = smtplib.SMTP()` `server.connect(d_host,d_port)` `server.ehlo()` `server.starttls()` `server.ehlo()` `server.login(d_email, d_password)` Still No luck :( – Husnain Taseer Nov 09 '15 at 10:59

2 Answers2

9

Probably, you don't need to login to the server. I don't have to when I connect to my corporate SMTP server. So you could try something like:

server = smtplib.SMTP('mail.mycompany.com',25)
server.ehlo()        
server.sendmail('my_email@mycompany.com', 'my_email@mycompany.com', msg)
yeniv
  • 1,589
  • 11
  • 25
  • 2
    You just saved me from creating a redundant SO question, +1 for that! I dont know about the asker, but your solution worked for me, I too am working on corporate server and smtp.login() was the problem, and your answer solved it – Vishwanth Iron Heart Mar 28 '17 at 08:35
  • Worked for me too. Thanks :) – Atmesh Mishra Sep 28 '17 at 05:25
  • 1
    This will tend to work when you are inside a corporate network and want to use the company's outbound email server as a relay. Other relaying scenarios are basically universally blocked; you generally will not be able to connect to port 25 on public servers. – tripleee Aug 24 '21 at 13:29
0

I am able to resolve the issue with modifying below line of code, by adding port number with server name:

server = smtplib.SMTP('mail.mycompany.com:587')     
server.starttls()
server.login('my_email@mycompany.com', 'my_password')
SunilThorat
  • 1,672
  • 2
  • 13
  • 15