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 = "rajiv@domain.com"
me = "rajiv@domain.com"
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
Razik
  • 172
  • 5
  • 16
RajivKumar
  • 65
  • 8

2 Answers2

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 = ["rajiv@domain.com"]
EMAIL_FROM = "rajiv@domain.com"
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("rajiv@domain.com", 'password')   # you account email password..
    mail.sendmail("rajiv@domain.com", EMAIL_TO, msg.as_string())   
    mail.quit()
except Exception,e:
    print e
finally:
    print "Error of email sending mail"
YakovL
  • 5,213
  • 10
  • 46
  • 71
Chavada Viki
  • 1,444
  • 1
  • 11
  • 20
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"rajiv@domain.com", subject="subject", contents="content")
PascalVKooten
  • 18,070
  • 15
  • 82
  • 140
  • 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 – RajivKumar Sep 14 '16 at 18:54
  • 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. – PascalVKooten Sep 14 '16 at 19:06
  • In your case you would put `.yagmail` in your `C:\Users\rajivkum` folder. – PascalVKooten Sep 14 '16 at 19:12