连接错误SMTP python

时间:2015-05-10 03:34:23

标签: python smtp gmail

我现在一直在使用python并且过去一直在使用电子邮件功能而没有任何错误,但是在最新的程序中我已经得到了这个错误

 Traceback (most recent call last):
    File "daemon.py", line 62, in <module>
    scraper.run()
    File "c:\cfsresd\scraper.py", line 48, in run
    self.scrape()
    File "c:\cfsresd\scraper.py", line 44, in scrape
    handler(msg)
    File "daemon.py", line 57, in handler
    server.ehlo()
    File "C:\Python27\lib\smtplib.py", line 385, in ehlo
    self.putcmd(self.ehlo_msg, name or self.local_hostname)
    File "C:\Python27\lib\smtplib.py", line 318, in putcmd
    self.send(str) 
    File "C:\Python27\lib\smtplib.py", line 310, in send
    raise SMTPServerDisconnected('please run connect() first')
    smtplib.SMTPServerDisconnected: please run connect() first

我为所有项目使用了相同的电子邮件代码,但这是第一次完成。我尝试过添加connect(),但没有区别。以下是我的脚本的电子邮件部分

msg = MIMEText ('%s - %s' % (msg.text, msg.channel))
    server = smtplib.SMTP('smtp.gmail.com:587')
    server.ehlo()
    server.starttls()
    msg['Subject'] = "msg.channel"
    msg['From'] = ('removed')
    msg['To'] = ('removed')
    server.login('user','password')
    server.sendmail(msg.get('From'),msg["To"],msg.as_string())
    server.close()
    server.ehlo()
    server.quit()
    print 'sent'

欢呼任何帮助

9 个答案:

答案 0 :(得分:3)

尝试使用SMTP的空构造函数,然后调用connect(host, port)

    server = smtplib.SMTP()
    server.connect('smtp.gmail.com', '587')
    server.ehlo()
    server.starttls()
    server.login(username, password)

答案 1 :(得分:3)

所有排序都采取了一些想法并尝试了下面的代码

function Validatequantity() {   
    var num = document.getElementById('quantity').value;
    if  (num < 1 || num > 10 ) {
        alert('Invalid ticket number ...Should be between 1-10');
        num = 1; // reset part is here(don't work)
    } 
}

所以我删除了ehlo(),close()和端口号。现在我必须锻炼如何将主题更改为msg.channel,以便每次都更改。

感谢所有

答案 2 :(得分:1)

close之后您有quit。这似乎不太可能成功。此外,closeehlo,因此您可能只是摆脱最后的closenamespace MyFirstWebApplication.Models { public class SchoolInitializer : DropCreateDatabaseIfModelChanges<SchoolContext> { protected override void Seed(SchoolContext context) { var students = new List<Student> { new Student{FirstName = "James", LastName= "Dean", EnrollmentDate = DateTime.Parse("2014-01-02") }, new Student{FirstName = "Lynda", LastName = "Thames", EnrollmentDate = DateTime.Parse("2013-11-02")} }; foreach (var temp in students) { context.Students.Add(temp); } context.SaveChanges(); var courses = new List<Course> { new Course{CourseName = "Java", TotalCredits = 4}, new Course{CourseName = "C#", TotalCredits = 4} }; foreach (var temp in courses) { context.Courses.Add(temp); } //context.SaveChanges(); var enrollments = new List<Enrollment> { new Enrollment{StudentId = 1, CourseId = 1, Grade = 3}, new Enrollment{StudentId = 1, CourseId = 2, Grade = 4} }; foreach (var temp in enrollments) { context.Enrollments.Add(temp); } context.SaveChanges(); } } } namespace MyFirstWebApplication.Models { public class SchoolContext : DbContext { //enables CRUD functionality public DbSet<Student> Students { get; set; } public DbSet<Course> Courses { get; set; } public DbSet<Enrollment> Enrollments { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } } } 来电

答案 3 :(得分:1)

您仍然可以使用SMTP_SSL类与smtp服务器建立加密连接,而无需使用starttls调用(更短)。您不需要每次都调用ehlo,这在需要时自动完成,当连接到默认端口时,在创建实例SMTP *类时不必提供一个。

msg = MIMEText ('%s - %s' % (msg.text, msg.channel))
msg['To'] = ','.join(receivers)
msg['Subject'] = 'msg.channel'
msg['From'] = 'someone@somedomain.com'

将SMTP与starttls一起使用:

server = smtplib.SMTP('smtp.gmail.com')
server.starttls()
server.login('user', 'password')
server.sendmail(msg['From'], receivers, msg.as_string())

现在使用SMTP_SSL类

server = smtplib.SMTP_SSL('smtp.gmail.com')
server.login('user', 'password')
server.sendmail(msg['From'], receivers, msg.as_string())

最后

server.quit()

答案 4 :(得分:1)

对于Pyhton 3.6。*
 注意:在gmail中,只有在关闭两步验证时才能使用它。  允许gmail通过低安全的应用程序打开。

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

from_addr = 'sender-email-id'
to_addr = 'receiver-email-id'
text = 'Hi Friend!!!'

username = 'sender-username'
password = 'password'

msg = MIMEMultipart()

msg['From'] = from_addr
msg['To'] = to_addr
msg['Subject'] = 'Test Mail'
msg.attach(MIMEText(text))


server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.sendmail(from_addr,to_addr,msg.as_string())
server.quit()

答案 5 :(得分:0)

我是yagmail的维护者,这个软件包可以让发送电子邮件变得非常简单。

import yagmail
yag = yagmail.SMTP('user','password')
yag.send(to = 'to@gmail.com', subject = 'msg.channel')

yag离开范围时,它会自动关闭。

我还建议你在密钥环中注册一次,这样你就不必在脚本中写密码了。跑一次:

yagmail.register('user', 'password')

然后你可以缩短它:

SMTP().send('to@gmail.com', 'msg.channel')

您可以使用pip或pip3(对于Python 3)安装它。您还可以read more about it,具有轻松添加附件,内嵌图像/ html,别名等功能。

答案 6 :(得分:0)

当我尝试从Celery(作为Docker容器)发送电子邮件时,我遇到了类似的问题。我将env_file添加到worker并击败了docker compose文件中的容器。

env_file: ./env/dev/.env

在该文件中,我有一个电子邮件配置。

EMAIL_HOST=smtp.gmail.com
EMAIL_HOST_USER=your_mail
EMAIL_HOST_PASSWORD=your_password
EMAIL_PORT=587

答案 7 :(得分:0)

我只是通过删除这一行解决了这个错误:

server.quit() 

答案 8 :(得分:0)

<块引用>

raise SMTPServerDisconnected('请先运行connect()')

如果您遇到此错误,我想安装它:

pip install django-smtp-ssl

这个安装smtp库和ssl协议

它非常适合我