0

I'm trying to send an email via SMTP server at my university. I wrote some python code. The code itself works fine, but I think there's a problem with SMTP server. I think I should contact the administrator but what should I tell him? Is it really a server problem? My code looks like following:

import smtplib
import string

fromaddr = str('from@subunidomain.server.com')
password = str('secretpass')

toaddrs  = 'to@unidomain.com'
server_smtp = 'smtp.server.com'
port_smtp = 465

msg = 'There was a terrible error that occured and I wanted you to know'

BODY = string.join((
        "From: %s" % fromaddr,
        "To: %s" % toaddrs,
        "Subject: %s" % 'Hello' ,
        "",
        'Hello'
        ), "\r\n")

try :

    server = smtplib.SMTP_SSL(host=server_smtp, port=port_smtp)
    server.set_debuglevel(True)
    server.login(fromaddr,password)
    server.sendmail(fromaddr, toaddrs, str(BODY))
    server.quit()

except smtplib.SMTPServerDisconnected :
    print "smtplib.SMTPServerDisconnected"
except smtplib.SMTPResponseException, e:
    print "smtplib.SMTPResponseException: " + str(e.smtp_code) + " " + str(e.smtp_error)
except smtplib.SMTPSenderRefused:
    print "smtplib.SMTPSenderRefused"
except smtplib.SMTPRecipientsRefused:
    print "smtplib.SMTPRecipientsRefused"
except smtplib.SMTPDataError:
    print "smtplib.SMTPDataError"
except smtplib.SMTPConnectError:
    print "smtplib.SMTPConnectError"
except smtplib.SMTPHeloError:
    print "smtplib.SMTPHeloError"
except smtplib.SMTPAuthenticationError:
    print "smtplib.SMTPAuthenticationError"
except Exception :
    print "Exception"

And I have such response from server:

send: 'ehlo [127.0.1.1]\r\n'
reply: '250-smtp.server.com Hello [127.0.1.1] [219.104.12.12]\r\n'
reply: '250-SIZE 78643200\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-AUTH PLAIN LOGIN\r\n'
reply: '250 HELP\r\n'
reply: retcode (250); Msg: smtp.server.com Hello [127.0.1.1] [219.104.12.12]
SIZE 78643200
PIPELINING
AUTH PLAIN LOGIN
HELP
send: 'AUTH PLAIN AGFpQGluZm9ybWF0aWNhLnVtY3MubHVibGluLnBsAGFubmFsZXN1bWNzMjAxNQ==\r\n'
reply: '535 Incorrect authentication data\r\n'
reply: retcode (535); Msg: Incorrect authentication data
smtplib.SMTPResponseException: 535 Incorrect authentication data

What's the problem and how to solve it? My password and email are 100% correct.

Here's the output from https://pingability.com/smtptest.jsp (username: from - when I try to write from@subunidomain.server.com instead, it does not work):

smtp.server.com
SMTP Username from
Output  

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.server.com", port 25, isSSL false
220-smtp.server.com ESMTP Exim 4.69 #1 Wed, 29 Apr 2015 23:05:53 +0200 
220-We do not authorize the use of this system to transport unsolicited, 
220 and/or bulk e-mail.
DEBUG SMTP: connected to host "smtp.server.com", port: 25

EHLO localhost
250-smtp.server.com Hello pingability.com [72.249.37.67]
250-SIZE 78643200
250-PIPELINING
250-AUTH PLAIN LOGIN
250-STARTTLS
250 HELP
DEBUG SMTP: Found extension "SIZE", arg "78643200"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "HELP", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
AUTH LOGIN
334 VXNlcm5hbWU6
YWk=
334 UGFzc3dvcmQ6
YW5uYWxlc3VtY3MyMDE1
235 Authentication succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<smtptester@pingability.com>
250 OK
RCPT TO:<smtptester@pingability.com>
250 Accepted
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   smtptester@pingability.com
DATA
354 Enter message, ending with "." on a line by itself
Date: Wed, 29 Apr 2015 21:05:53 +0000 (UTC)
From: smtptester@pingability.com
To: smtptester@pingability.com
Message-ID: <8486466.51545.1430341553138.JavaMail.tomcat@localhost>
Subject: Pingability Test
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

Pingability Test
.
250 OK id=1YnZB5-0006N0-Nd
QUIT
221 smtp.server.com closing connection
yawn
  • 5
  • 4

1 Answers1

0

Perhaps you can try sending your message with yagmail.

Install:

pip install yagmail

Then import, make connection object and send the email:

import yagmail
yag = yagmail.Connect('user', 'pass', server_smtp, port_smtp, set_debuglevel = 1)
yag.send(toaddrs, Subject = 'Hello', Body = 'Hello')

Let me know if you face any errors.

Disclaimer/Background: I'm the maintainer/developer of yagmail. The purpose of the package is to make a lot of things very convenient in sending an email. For example, you can use its features not to write your password in the script, but store it in your keyring (very safe, you can read about it on github!). It also has a few ways to make it a lot shorter, for example:

from yagmail import Connect
yag = Connect(host = server_smtp, port = port_smtp, set_debuglevel = 1) 
yag.send(toaddrs, 'Hello', 'Hello')

Attachments (such as HTML/images) are really easy to use, but I won't go there as it wasn't asked. Lastly, whenever it goes out of scope, it automatically quits itself.

PascalVKooten
  • 20,643
  • 17
  • 103
  • 160
  • Thanks, tried it - seems that it didn't work, application just 'hangs' and I dont see any debug messages, so I think that's not the issue. – yawn Apr 29 '15 at 20:27
  • @yawn Hmmm, that's odd. Are you sure it is not the host/port settings? Perhaps try the code without giving the host and port. – PascalVKooten Apr 29 '15 at 20:29
  • @yawn Does the `yag` object get created prior to the attempt of sending a message? – PascalVKooten Apr 29 '15 at 20:35
  • In your original approach, you could perhaps try adding `server.esmtp_features['auth'] = 'LOGIN AUTH'`? Or instead of `'LOGIN AUTH'`, try `'LOGIN PLAIN'` – PascalVKooten Apr 29 '15 at 20:37
  • Both `'LOGIN AUTH'` and `'LOGIN PLAIN'` give same result = no email is sent. However, when I try to log in to my account on my university SMTP server (its web front end), using ONLY the username, for instance, `yawn` instead of `yawn@subunidomain.server.com` it .. works, I can log in. When I try to use only the username in my original python code, it fails and says that I need a domain ... – yawn Apr 29 '15 at 21:41