2011-06-13 474 views
26

我正在使用Outlook 2003通过Python发送Outlook电子邮件?

什么是使用Python发送电子邮件(通过Outlook 2003)的最好方法?通过谷歌

+1

为什么你要通过Outlook发送邮件? – ThiefMaster 2011-06-13 15:34:32

+1

@ThiefMaster:我的smtp服务器与我的电子邮件不同 - 因此,我需要通过我的互联网提供商('att')“通道”我的smtp,尽管我使用的是不同的电子邮件地址(不是' att's')发送电子邮件。 'Outlook'已配置为处理此问题。如果还有其他解决方案(基于非'Outlook'),也会支持这个解决方案,我很乐意听取建议。 – user3262424 2011-06-13 18:11:29

+0

正确的解决方案是使用python的[smtplib](http://docs.python.org/library/smtplib.html#smtp-example) – ThiefMaster 2011-06-13 18:35:04

回答

10

使用随蟒的smtplib。请注意,这将需要您的电子邮件帐户允许smtp,默认情况下不一定启用。

SERVER = "your.mail.server" 
FROM = "[email protected]" 
TO = ["listOfEmails"] # must be a list 

SUBJECT = "Subject" 
TEXT = "Your Text" 

# Prepare actual message 
message = """From: %s\r\nTo: %s\r\nSubject: %s\r\n\ 

%s 
""" % (FROM, ", ".join(TO), SUBJECT, TEXT) 

# Send the mail 
import smtplib 
server = smtplib.SMTP(SERVER) 
server.sendmail(FROM, TO, message) 
server.quit() 

编辑:这个例子中使用了虚构的谷歌邮件到另一个谷歌邮件

SERVER = "smtp.google.com" 
FROM = "[email protected]" 
TO = ["[email protected]"] # must be a list 

SUBJECT = "Hello!" 
TEXT = "This is a test of emailing through smtp in google." 

# Prepare actual message 
message = """From: %s\r\nTo: %s\r\nSubject: %s\r\n\ 

%s 
""" % (FROM, ", ".join(TO), SUBJECT, TEXT) 

# Send the mail 
import smtplib 
server = smtplib.SMTP(SERVER) 
server.login("MrDoe", "PASSWORD") 
server.sendmail(FROM, TO, message) 
server.quit() 

对于它的实际工作,李四先生需要去选择标签在Gmail和设置它允许smtp连接。请注意添加登录线路以向远程服务器进行身份验证。原始版本不包括这一点,这是我的一个疏忽。

+7

这不是问题。问题是关于使用Win32 API来控制Outlook – 2011-06-13 15:38:22

+0

@Spencer Rathbun:谢谢。问题是这样的:我的'smtp'服务器与我的电子邮件不一样 - 因此,我需要通过我的互联网提供商('att')“通道”我的smtp,尽管我使用的是不同的电子邮件地址(不是'att's')发送邮件。 'Outlook'已配置为处理此问题。如果还有其他解决方案(基于非'Outlook'),也会支持这个解决方案,我很乐意听取建议。 – user3262424 2011-06-13 18:12:53

+0

@ user3262424那么你的电子邮件地址与你的smtp服务器不一样?这应该在smtp服务器上处理。它需要设置为将不会从那里发送的电子邮件传递到正确的电子邮件服务器。错误地设置,这可以让垃圾邮件发送者循环通过你。但据推测,你知道涉及的IP地址,并可以将它们设置在允许列表中。 – 2011-06-13 18:24:27

23

检查,有很多的例子,看到here一个。

内联为了便于观察:

import win32com.client 

def send_mail_via_com(text, subject, recipient, profilename="Outlook2003"): 
    s = win32com.client.Dispatch("Mapi.Session") 
    o = win32com.client.Dispatch("Outlook.Application") 
    s.Logon(profilename) 

    Msg = o.CreateItem(0) 
    Msg.To = recipient 

    Msg.CC = "moreaddresses here" 
    Msg.BCC = "address" 

    Msg.Subject = subject 
    Msg.Body = text 

    attachment1 = "Path to attachment no. 1" 
    attachment2 = "Path to attachment no. 2" 
    Msg.Attachments.Add(attachment1) 
    Msg.Attachments.Add(attachment2) 

    Msg.Send() 
+0

谢谢,这很好。问题是,Outlook不断生成一个“警告消息”,询问我是否想继续或终止访问脚本。有没有办法跳过这个'警报消息'? – user3262424 2011-06-13 18:08:56

+0

@ user3262424 - 弹出窗口的确切内容是什么? – 2011-06-13 18:23:40

+0

我现在不在那台电脑旁边。通知脚本正在尝试访问“outlook”,并且可能是病毒等,并且我想继续。 – user3262424 2011-06-13 18:45:46

5

使用pywin32

from win32com.client import Dispatch 

session = Dispatch('MAPI.session') 
session.Logon('','',0,1,0,0,'exchange.foo.com\nUserName'); 
msg = session.Outbox.Messages.Add('Hello', 'This is a test') 
msg.Recipients.Add('Corey', 'SMTP:[email protected]') 
msg.Send() 
session.Logoff() 
+0

谢谢。这不会产生烦人的'outlook'错误讯息? – user3262424 2011-06-13 18:13:44

+1

它可能会触发验证新版本的Windows。不知道你会如何压制这一点。我再也不用跑窗了。 – 2011-06-13 18:20:43

45
import win32com.client as win32 
outlook = win32.Dispatch('outlook.application') 
mail = outlook.CreateItem(0) 
mail.To = 'To address' 
mail.Subject = 'Message subject' 
mail.Body = 'Message body' 
mail.HTMLBody = '<h2>HTML Message body</h2>' #this field is optional 

# To attach a file to the email (optional): 
attachment = "Path to the attachment" 
mail.Attachments.Add(attachment) 

mail.Send() 

将使用本地Outlook帐户发送。

注意,如果你正在尝试做上面没有提到的东西,看看COM文件的属性/方法:https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/mailitem-object-outlook。在上面的代码中,mail是一个MailItem对象。

+0

是否有可能的HTML身体。就像一个带图片的电子邮件正文 – 2016-12-09 10:03:27

+2

已更新的答案。 mail.HTMLBody让你把它设置为一个html字符串 – TheoretiCAL 2016-12-09 17:59:39

+2

简单而直接的答案!在Python 3.6.1中正确工作。 – 2017-06-21 14:00:51

0

我想使用的smtplib,其更容易,它不需要本地设置发送电子邮件。其他答案没有直接帮助后,这就是我所做的。

打开Outlook在浏览器中;点击右上角的设置齿轮图标,从出现的下拉列表中选择“选项”。 转到'账户',点击'流行和Imap', 你会看到选项:“让设备和应用程序使用弹出”,

选择是的选项和保存更改。

这里是后的代码存在;编辑需要的地方。 最重要的是在此启用POP和服务器代码;

import smtplib 

body = 'Subject: Subject Here .\nDear ContactName, \n\n' + 'Email\'s BODY text' + '\nYour :: Signature/Innitials' 
try: 
    smtpObj = smtplib.SMTP('smtp-mail.outlook.com', 587) 
except Exception as e: 
    print(e) 
    smtpObj = smtplib.SMTP_SSL('smtp-mail.outlook.com', 465) 
#type(smtpObj) 
smtpObj.ehlo() 
smtpObj.starttls() 
smtpObj.login('[email protected]', "password") 
smtpObj.sendmail('[email protected]', '[email protected]', body) # Or [email protected] 

smtpObj.quit() 
pass 
0

这是一个我试图使用Win32:

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

# Drafting and sending email notification to senders. You can add other senders' email in the list 
def send_notification(): 


    outlook = win32.Dispatch('outlook.application') 
    olFormatHTML = 2 
    olFormatPlain = 1 
    olFormatRichText = 3 
    olFormatUnspecified = 0 
    olMailItem = 0x0 

    newMail = outlook.CreateItem(olMailItem) 
    newMail.Subject = sys.argv[1] 
    #newMail.Subject = "check" 
    newMail.BodyFormat = olFormatHTML #or olFormatRichText or olFormatPlain 
    #newMail.HTMLBody = "test" 
    newMail.HTMLBody = sys.argv[2] 
    newMail.To = "[email protected]" 
    attachment1 = sys.argv[3] 
    attachment2 = sys.argv[4] 
    newMail.Attachments.Add(attachment1) 
    newMail.Attachments.Add(attachment2) 

    newMail.display() 
    # or just use this instead of .display() if you want to send immediately 
    newMail.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()