简体   繁体   中英

Issue sending mail with smtp python

I'm working in an app that sends an icalendar file to a mail and I've an issue with it. The main fact is that the app works in a properly way in all the cases except one. I've been testing the app with the wifi of my university (only students have acces to this wifi) and It couldn't be sent (the app enter in a loop and the mail can't be sent). I attach here the code that I use to send the mail. I think that maybe the problem is with the ports (maybe aren't openend all the ports in the free wifi of my university). If anyone knows a better way that couldn't fail it would be nice, because the only problem that I have with the app is sending the mail with this special wifi (with other wifi works properly). Code:

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders

def send_mail(mail):
    fromaddr = "adress@gmail.com"
    toaddr = mail.strip()
    msg = MIMEMultipart('alternative')
    msg['From'] = "Contact <adress@gmail.com>"
    msg['To'] = toaddr
    msg['Subject'] = u"Subject" 
    body = """Body"""
    msg.attach(MIMEText(body, "html")
    filename = "fileattached.ics"
    part = MIMEBase('application', 'octet-stream',name=filename)
    part.set_payload(cal.to_ical())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', "attachment; filename= %s" % filename) 
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(fromaddr, "password")
    text = msg.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()   

I don't know if the problem is the port that I'm using to send the mail, but I've been told that maybe the issue is produced by that.

Turn on SMTP session debugging. It should provide some clues.

https://docs.python.org/3/library/smtplib.html#smtplib.SMTP.set_debuglevel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM