Error when connecting to Google Mail over SMTP using smtplib in Python
Question: My issue is that when I run the following code, I get a WinError 10061, and
from all of my searching it looks like this is a result of the foreign machine
not being set up properly, but I assume that Google has that taken care of for
gmail, so the error lies on my side. All of the other examples I could find
were using localhost and getting this error, and it was because they did not
have a local mail server set up. Would that still be the case with this
problem? I am sure I am missing something obvious. Also, the error code is in
full. Thank you in advance!

    
    
    import smtplib
    
    fromaddr = 'email@email.com'
    toaddrs  = 'otheremail@email.com'
    msg = 'Random stuff!'
    
    
    username = 'username'
    password = 'pass'
    
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(username,password)
    server.sendmail(fromaddr, toaddrs, msg)
    server.quit()
    

Error:

    
    
    Traceback (most recent call last):
      File "C:/Users/Brett/PycharmProjects/Texting/sendMessage.py", line 13, in <module>
        server = smtplib.SMTP('smtp.gmail.com')
      File "C:\Python33\lib\smtplib.py", line 238, in __init__
        (code, msg) = self.connect(host, port)
      File "C:\Python33\lib\smtplib.py", line 317, in connect
        self.sock = self._get_socket(host, port, self.timeout)
      File "C:\Python33\lib\smtplib.py", line 288, in _get_socket
        self.source_address)
      File "C:\Python33\lib\socket.py", line 424, in create_connection
        raise err
      File "C:\Python33\lib\socket.py", line 415, in create_connection
        sock.connect(sa)
    ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
    


Answer: Having used the Gmail SMTP server in the past, I can only point out that the
number for the SMTP port actually defaults to the appropriate one for Gmail,
so the port argument (which can also be tacked onto the server name with a
colon) can be omitted. Given this, you probably should try a different machine
to ensure you're not running into networking errors, etc. Also, port 587
requires that ID check takes place, which means Gmail may think you're
spamming or some other nonsense. Port 25, the default taken when no port
number is entered, does not perform such a check.

