简体   繁体   English

使用smtp python发送邮件

[英]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. 我正在一个将icalendar文件发送到邮件的应用程序中,但遇到了问题。 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). 我一直在用我所在大学的wifi测试该应用程序(仅学生可以使用该wifi),但无法发送(该应用程序进入循环并且无法发送邮件)。 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). 我认为问题可能出在端口上(也许不是我大学的免费wifi中开放所有端口)。 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). 如果有人知道不会失败的更好方法,那就太好了,因为我与该应用程序有关的唯一问题是使用这种特殊的wifi发送邮件(其他wifi正常工作)。 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. 打开SMTP会话调试。 It should provide some clues. 它应该提供一些线索。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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