Python SMTPLIB with proxy support, Gmail connection not allowed - python

I am succesfully sending emails through gmail app password, python and smtplib.
I am attempting to add proxy support to my script, but I am getting hit by the following error message:
socks.GeneralProxyError: Socket error: 0x02: Connection not allowed by ruleset
Here are the relevant parts of my script:
socks.set_default_proxy(socks.SOCKS5, host, port, True, user, passw)
socks.wrapmodule(smtplib)
session = smtplib.SMTP('smtp.gmail.com', 587)
session.login(sender_address, sender_pass)

Related

Sending email(outlook as server) in azure databricks notebook in python?

I tried to configure using localhost. I ran this in my cmd line.
(python -m smtpd -c DebuggingServer -n localhost:1025)
import smtplib, ssl
smtp_server = "localhost"
port = 1025 # For starttls
sender_email = "my outlook email"
password = input("Type your password and press enter: ")
# Create a secure SSL context
context = ssl.create_default_context()
# Try to log in to server and send email
try:
server = smtplib.SMTP(smtp_server,port)
server.ehlo() # Can be omitted
server.starttls(context=context) # Secure the connection
server.ehlo() # Can be omitted
server.login(sender_email, password)
print("server connected")
# TODO: Send email here
except Exception as e:
# Print any error messages to stdout
print(e)
finally:
server.quit()
But it is giving "connection refused" error.
To resolve connection refused error, you can try following ways:
Firewall might be blocking access to port 1025
Alternatively, you can try using port 25
As per documentation:
For Enterprise Dev/Test subscriptions, port 25 is blocked by default. It is possible to have this block removed. To request to have the block removed, go to the Cannot send email (SMTP-Port 25) section of the Diagnose and Solve blade in the Azure Virtual Network resource in the Azure portal and run the diagnostic. This will exempt the qualified enterprise dev/test subscriptions automatically.
You can refer to Send emails from Azure Databricks

How can I call the connection of a SMTP server outside the function in Python?

I wanted to send multiple messages with one connection using SMTPLIB, but I wanted to create a connection and send the messages dynamically.
I was wondering what i've done wrong, beucase many other libs can hold its connection.
import smtplib
def connect():
cred = {
'uid': 'xxxxxxx#hotmail.com',
'pwd': 'xxxxxxx'
}
with smtplib.SMTP('smtp.live.com', 587) as smtp:
smtp.ehlo()
smtp.starttls() #TLS used to encrypt the message
smtp.ehlo()
smtp.login(cred['uid'],
cred['pwd'])
return smtp
smtp = connect()
smtp.send_message( -Some generic message-)
Error : SMTPServerDisconnected: please run connect() first

How to set up a local SMTP server for sending mail using pythons smtplib module

I want to create a program ,which can send bulk emails to people (for company purposes).
For a few emails I used gmail.smtp.com for sending mails
s = smtplib.SMTP('smtp.gmail.com', 587)
# start TLS for security
#s.starttls()
# Authentication
s.login("myemail#gmail.com","mypassword")
# message to be sent
message="hello"
# sending the mail
s.sendmail("myemail#gmail.com", "targetemail#gmail.com", message)
# terminating the session
print("mail sent")
s.quit()
I know that I cannot use Gmail's SMTP server for sending bulk mail.
How do I set up a local SMTP server and use it in smtplib?

Sending email from Python throwing SSLError 'unknown protocol'

I was sending emails from flask-mail but since trying to use the mailservers at namecheap or bluehost, I'm getting the following error:
SSLError: [Errno 1] _ssl.c:510: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
So now I'm trying to send the email without flask-mail but I'm still getting the same error. Any fix?
My code is as follows:
from smtplib import SMTP
smtp = SMTP()
smtp.set_debuglevel(debuglevel)
smtp.connect('xxxxxx', 26)
smtp.login('noreply#xxx.com', 'xxxxxxx')
from_addr = "xxx <noreply#xxx.com>"
to_addr = rec#xxx.com
subj = "hello"
date = datetime.datetime.now.strftime( "%d/%m/%Y %H:%M" )
message_text = "Hello\nThis is a mail from your server\n\nBye\n"
msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( from_addr, to_addr, subj, date, message_text )
smtp.sendmail(from_addr, to_addr, msg)
smtp.quit()
My application is running on Ubuntu 14.04 on Amazon EC2.
Thanks.
The reason that this is giving you this error is because your mail server is not a SMTP server. Use Gmail or another smtp mail service to send the mail. Try sending it through a gmail account with the server being smtp.gmail.com and the port being 587. First though you will need to configure your account for it.

Python SMTP Server with SSL: handshake error

I use lib secure_smtpd for create SMTP server. When I generate certificate and use it for SSL connection I catch exception (for test I use Opera mail client and The Bat!):
SSLError: _ssl.c:489: The handshake operation timed out
When I test use python script everything is ok:
smtpObj = smtplib.SMTP_SSL('localhost',2000)
smtpObj.set_debuglevel(1)
smtpObj.login('testuser', '111111')
msg = MIMEMultipart('alternative')
msg['Subject'] = "my subj SSL"
msg['From'] = sender
msg['To'] = "username#site.com"
msg.attach(MIMEText("Hello world!",'html'))
smtpObj.sendmail(sender, [toemail], msg.as_string())
Can somebody help fix problem with handshake?
I use python 2.7.3
How you configured SMTP in Opera and The Bat! ? There is SSL mode (which you seems to use), where all connection is wrapped in SSL/TLS, and STARTTLS mode, where connection is plain but SSL/TLS excahnge is started after client issues STARTTLS command.

Categories