1

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
5
  • 2
    Do you have an SMTP server running on your localhost? Commented Sep 14, 2016 at 9:37
  • I don't think so, I am using windows 7 Commented Sep 14, 2016 at 9:38
  • 3
    Then you need an SMTP server (your own or any other you have an account for). Commented Sep 14, 2016 at 9:39
  • I'm voting to close this question as off-topic because the lack of an SMTP server is not a programming issue. Commented Sep 14, 2016 at 9:48
  • Possible duplicate of Sending mail from Python using SMTP Commented Sep 14, 2016 at 10:23

2 Answers 2

0

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"
Sign up to request clarification or add additional context in comments.

2 Comments

how I can use localhost to send emails?
may be you need an email server for that you can't send it from localhost.
0

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")

3 Comments

with open(os.path.expanduser("~/.yagmail")) as f: FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\rajivkum/.yagmail' >>> I got this error with yagmail
As you notice, no password and username :-) Run once yagmail.register("username", "password") which will save your password in your operating systems keyring. Using your username in .yagmail file in your user folder will allow passwordless scripts. Or: yagmail.SMTP("username", "password") if you like writing username/password in your scripts.
In your case you would put .yagmail in your C:\Users\rajivkum folder.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.