2017-10-09 124 views
0

我想这个电子邮件发送sample code导入错误:DLL加载失败:%1不是有效的Win32应用程序,同时导入win32com

# -*- coding: utf-8 -*- 
""" 
Created on Wed Sep 21 15:36:00 2016 

@author: Deepesh.Singh 
""" 

import win32com.client as win32 
import psutil 
import os 
import subprocess 

# Drafting and sending email notification to senders. You can add other senders' email in the list 
def send_notification(): 
    outlook = win32.Dispatch('outlook.application') 
    mail = outlook.CreateItem(0) 
    mail.To = '[email protected]; [email protected]', 
    mail.Subject = 'Sent through Python' 
    mail.body = 'This email alert is auto generated. Please do not respond.' 
    mail.send 

# Open Outlook.exe. Path may vary according to system config 
# Please check the path to .exe file and update below 

def open_outlook(): 
    try: 
     subprocess.call(['C:\Program Files\Microsoft Office\Office15\Outlook.exe']) 
     os.system("C:\Program Files\Microsoft Office\Office15\Outlook.exe"); 
    except: 
     print("Outlook didn't open successfully") 

# Checking if outlook is already opened. If not, open Outlook.exe and send email 
for item in psutil.pids(): 
    p = psutil.Process(item) 
    if p.name() == "OUTLOOK.EXE": 
     flag = 1 
     break 
    else: 
     flag = 0 

if (flag == 1): 
    send_notification() 
else: 
    open_outlook() 
    send_notification() 

,但不停的按下面的错误,当我在命令提示符下运行的代码:

C:\<>\Desktop\Exp>python sendemail.py 
Traceback (most recent call last): 
    File "sendemail.py", line 40, in <module> 
    import win32com.client 
    File "C:\Python27\lib\site-packages\win32com\__init__.py", line 5, in <module> 
    import win32api, sys, os 
ImportError: DLL load failed: %1 is not a valid Win32 application. 

有人可以请指导我如何解决这个错误?或者他们是一个更好的方法来做到这一点?

谢谢。

+0

请告诉你的python版本和位?和操作系统? – Harsha

+0

Python 2.7.13(v2.7.13:a06454b1afa1,Dec 17 2016,20:42:59)[MSC v.1500 32 bit(Intel)] on win32。 我正在使用Windows 10 64位操作系统 – Khan

+0

我重新安装了pywin32(32位版本),正如下面的Trapli指出的那样,并且还安装了psutil模块并重新启动了我的系统。现在问题已解决,我可以发送电子邮件。谢谢大家帮助我。 – Khan

回答

1

这可能是x86与x64的问题。如果你使用64位的python,导入一个64位的dll,如果你使用32位的python,导入32位的dll。我想this是你在找什么。详情请参阅this

+0

你的答案是不言自明的,一个更正式和更好的答案应该解决问题*“你怎么能做到这一点?”*,而不是仅仅说“你需要这样做”,但不告诉他们该怎么做。 – abccd

0

您是否安装了用于Python的Win32 扩展?制作 确定你为你的系统选择了正确的 安装程序,而 版本的Python,否则 可能无法正常工作。

0

我重新安装了pywin32(32位版本),正如Trapli指出的那样,并且还安装了psutil模块并重新启动了我的系统。现在问题已解决,我可以发送电子邮件。 谢谢大家帮助我。

相关问题