0

I am trying to write a python script which sends a html code from my mail id to the target mail id.

It reads the following :

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

me = "prudhvi@domainname.com"
you = "aditya@domainname.com"

msg = MIMEMultipart('multipart')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you

html = """\
<html>
 <head></head>
  <body>
    <p>Hi!<br>
       How are you?<br>       
    </p>
  </body>
</html>
"""

part = MIMEText(html, 'html')

msg.attach(part)

s = smtplib.SMTP('localhost')

s.sendmail(me, you, msg.as_string())
s.quit()

I have tried running it and came across this error :

Traceback (most recent call last):
  File "C:/Users/admin/Desktop/samp.py", line 29, in <module>
    s = smtplib.SMTP('localhost')
  File "C:\Python27\Lib\smtplib.py", line 256, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Python27\Lib\smtplib.py", line 316, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Python27\Lib\smtplib.py", line 291, in _get_socket
    return socket.create_connection((host, port), timeout)
  File "C:\Python27\Lib\socket.py", line 575, in create_connection
    raise err
error: [Errno 10061] No connection could be made because the target machine actively refused it

Could I know where I am going wrong?

prudhvi
  • 1,141
  • 6
  • 23
  • 46
  • Possible duplicate of [Errno 10061 : No connection could be made because the target machine actively refused it ( client - server )](http://stackoverflow.com/questions/12993276/errno-10061-no-connection-could-be-made-because-the-target-machine-actively-re) – David Ferenczy Rogožan Dec 13 '16 at 16:59
  • 1
    smtplib.SMTP('localhost') - You need to connect to an email server. This tries to connect to an email server on your local machine. Try your real email server instead. You will likely need to login. – tdelaney Dec 13 '16 at 16:59
  • @dawidferenczy - I think that's too generic to be a duplicate. OP just needs to login to an email server. – tdelaney Dec 13 '16 at 17:02
  • I would properly set up the SMTP server on your machine or, as tdelaney suggests, use some other SMTP server (like Google's, etc.), but in that case, you have to authorize there and the sender of the e-mail will be set to the account you use for the authentication. – David Ferenczy Rogožan Dec 13 '16 at 17:04
  • @tdelaney Damn, you're right, I used the wrong link, I wanted to use the following: http://stackoverflow.com/questions/37960035/python-email-errno-10061-no-connection-could-be-made-because-the-target-machi – David Ferenczy Rogožan Dec 13 '16 at 17:07
  • [http://stackoverflow.com/questions/882712/sending-html-email-using-python] . There is an answer [3rd answer] in this post which says it is the Gmail implementation and has specified some extra lines where passwod and username have to be mentioned. Will that solve? – prudhvi Dec 13 '16 at 17:07
  • @prudhvi Why don't you try it? Should we try it for you? It should probably work. But as mentioned in the comments of that answer, you have to set the low security in Google account (not recommended) or to generate the application specific access token and use it instead of the account's password. And, as I wrote earlier, you have to set the e-mail sender in your Python script to the e-mail address of your Gmail account. Or add an alias there and use that instead. – David Ferenczy Rogožan Dec 13 '16 at 17:10
  • Actually, I am not sending it from Gmail. I have my own domain name and I have to send it from there. Let me try. – prudhvi Dec 13 '16 at 17:12
  • Well, if you want to use the Gmail's SMTP server, you have to use the e-mail address (ar an alias) you use in your Gmail account. Doesn't matter if you're sending the e-mail from the Gmail website or using the SMTP. – David Ferenczy Rogožan Dec 13 '16 at 17:15
  • Yaaa...it worked. Thank you Dawid – prudhvi Dec 13 '16 at 17:18

1 Answers1

0

I have solved my problem by doing the following :

1.) Providing Username and Password. 2.) Set the low security in google ON.

The code :

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

me = "prudhvir36@gmail.com"
you = "prudhvi.g@domainname.com"

msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you

text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
  <head></head>
  <body>
    <p>Hi!<br>
       How are you?<br>
       Here is the <a href="http://www.python.org">link</a> you wanted.
    </p>
  </body>
</html>
"""

part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

msg.attach(part1)
msg.attach(part2)

mail = smtplib.SMTP('smtp.gmail.com', 587)

mail.ehlo()

mail.starttls()

mail.login('prudhvir36', '*************')
mail.sendmail(me, you, msg.as_string())
mail.quit()
Tom O'Connor
  • 503
  • 9
  • 30
prudhvi
  • 1,141
  • 6
  • 23
  • 46