1
votes

My Code

import smtplib
import socket
import sys
from email.mime.text import MIMEText
fp = open("CR_new.txt", 'r')
msg = MIMEText(fp.read())
fp.close()

you = "[email protected]"
me = "[email protected]"
msg['Subject'] = 'The contents of %s' % "CR_new.txt"
msg['From'] = you
msg['To'] = me
s = smtplib.SMTP('127.0.0.1')
s.sendmail(you,me, msg.as_string())
s.quit()

ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

Note:

  • Not having a SMTP server
2
Do you have an SMTP server running on your localhost? - Klaus D.
I don't think so, I am using windows 7 - RajivKumar
Then you need an SMTP server (your own or any other you have an account for). - Klaus D.
I'm voting to close this question as off-topic because the lack of an SMTP server is not a programming issue. - Klaus D.

2 Answers

0
votes

This code will help you to send the email. You just need to provide your email-id password.

The most important note is: don't give file name as email.py.

import socket
import sys
import smtplib
EMAIL_TO = ["[email protected]"]
EMAIL_FROM = "[email protected]"
EMAIL_SUBJECT = "Test Mail... "
msg= {}
EMAIL_SPACE = ", "



msg['Subject'] = EMAIL_SUBJECT 
msg['To'] = EMAIL_SPACE.join(EMAIL_TO)
msg['From'] = EMAIL_FROM
try:
    mail = smtplib.SMTP_SSL('smtp.bizmail.yahoo.com',465)
    # 
    # if you are using gmail then  use
    #   smtplib.SMTP_SSL('smtp.gmail.com',587)
    #
    mail.login("[email protected]", 'password')   # you account email password..
    mail.sendmail("[email protected]", EMAIL_TO, msg.as_string())   
    mail.quit()
except Exception,e:
    print e
finally:
    print "Error of email sending mail"
0
votes

I would suggest using a package like yagmail, rather than trying to figure out how to get smtplib to work. Disclaimer: I'm the maintainer of yagmail.

Code would look like:

import yagmail
yag = yagmail.SMTP(host="127.0.0.1")
yag.send(to"[email protected]", subject="subject", contents="content")