-4

sometime i got

Traceback '(most recent call last):
  File "main.py", line 45, in <module>
    server.sendmail(sender, receiver, msg.as_string())
  File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/smtplib.py", line 899, in sendmail
    raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {'example@gmail.com': (550, b'The mail server could not deliver mail to example@gmail.com.  The account\nor domain may not exist, they may be blacklisted, or missing the proper dns\nentries.')}'

and script get stopped i need to know if there solution to continue after any error

with smtplib.SMTP(smtpsv, port) as server:

server.starttls() # Secure the connection

server.login(user, password)
server.sendmail(sender, receiver, msg.as_string())
print(" + {}: {} sent succeffully".format(count, line.strip()))
The Thonnu
  • 3,578
  • 2
  • 8
  • 30
  • 4
    https://docs.python.org/3/tutorial/errors.html#handling-exceptions – Anentropic Aug 11 '22 at 17:22
  • Welcome to Stack Overflow! Please take the [tour] and read [ask], which has tips like starting with your own research. If you just paste your question title into google, you get useful results, and I've closed this question under one of them. – wjandrea Aug 11 '22 at 21:24

2 Answers2

0

You can implement try/except to handle error without breaking the flow,

import traceback
try:
    with smtplib.SMTP(smtpsv, port) as server:
        server.starttls() # Secure the connection

        server.login(user, password)
        server.sendmail(sender, receiver, msg.as_string())
        print(" + {}: {} sent succeffully".format(count, line.strip()))
except Exception:
    print(traceback.print_exc())
KnowledgeGainer
  • 1,017
  • 1
  • 5
  • 14
  • [A bare `except` is bad practice](/q/54948548/4518341). Instead, use the specific exception you're expecting like `except smtplib.SMTPRecipientsRefused`, or at least `except Exception`. – wjandrea Aug 11 '22 at 17:28
  • 1
    @wjandrea You are right, modified the above example – KnowledgeGainer Aug 11 '22 at 17:35
0

just wrap particular function /code in try execpt block

try:
    with smtplib.SMTP(smtpsv, port) as server:

        server.starttls()  # Secure the connection

        server.login(user, password)
        server.sendmail(sender, receiver, msg.as_string())
        print(" + {}: {} sent succeffully".format(count, line.strip()))
except Exception as error:
    print(error)

Sachin Salve
  • 146
  • 8