回答
使用随蟒的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连接。请注意添加登录线路以向远程服务器进行身份验证。原始版本不包括这一点,这是我的一个疏忽。
这不是问题。问题是关于使用Win32 API来控制Outlook – 2011-06-13 15:38:22
@Spencer Rathbun:谢谢。问题是这样的:我的'smtp'服务器与我的电子邮件不一样 - 因此,我需要通过我的互联网提供商('att')“通道”我的smtp,尽管我使用的是不同的电子邮件地址(不是'att's')发送邮件。 'Outlook'已配置为处理此问题。如果还有其他解决方案(基于非'Outlook'),也会支持这个解决方案,我很乐意听取建议。 – user3262424 2011-06-13 18:12:53
@ user3262424那么你的电子邮件地址与你的smtp服务器不一样?这应该在smtp服务器上处理。它需要设置为将不会从那里发送的电子邮件传递到正确的电子邮件服务器。错误地设置,这可以让垃圾邮件发送者循环通过你。但据推测,你知道涉及的IP地址,并可以将它们设置在允许列表中。 – 2011-06-13 18:24:27
检查,有很多的例子,看到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()
谢谢,这很好。问题是,Outlook不断生成一个“警告消息”,询问我是否想继续或终止访问脚本。有没有办法跳过这个'警报消息'? – user3262424 2011-06-13 18:08:56
@ user3262424 - 弹出窗口的确切内容是什么? – 2011-06-13 18:23:40
我现在不在那台电脑旁边。通知脚本正在尝试访问“outlook”,并且可能是病毒等,并且我想继续。 – user3262424 2011-06-13 18:45:46
使用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()
谢谢。这不会产生烦人的'outlook'错误讯息? – user3262424 2011-06-13 18:13:44
它可能会触发验证新版本的Windows。不知道你会如何压制这一点。我再也不用跑窗了。 – 2011-06-13 18:20:43
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对象。
是否有可能的HTML身体。就像一个带图片的电子邮件正文 – 2016-12-09 10:03:27
已更新的答案。 mail.HTMLBody让你把它设置为一个html字符串 – TheoretiCAL 2016-12-09 17:59:39
简单而直接的答案!在Python 3.6.1中正确工作。 – 2017-06-21 14:00:51
我想使用的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
除了Win32的,如果你的公司已经建立了您网站的前景,你也可以尝试PYTHON REST API,这是正式由微软。 (https://msdn.microsoft.com/en-us/office/office365/api/mail-rest-operations)
这是一个我试图使用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()
- 1. PYTHON/OUTLOOK通过PYTHON与DOCX发送电子邮件
- 2. 从Python发送Outlook 2010电子邮件
- 3. 通过Python发送电子邮件
- 4. Excel自动通过Outlook发送电子邮件随机发送
- 5. 通过Outlook中的Outlook发送电子邮件时出错?
- 6. 如何通过Outlook通过Outlook发送电子邮件与表7
- 7. 通过VBA发送带有附件的Outlook电子邮件
- 8. 错误虽然通过Outlook从Excel发送电子邮件VBA
- 9. 通过Powershell和Outlook发送电子邮件
- 10. 使用Outlook通过PHP发送电子邮件梨
- 11. JAVA通过Outlook发送电子邮件与SWT
- 12. 节点Webkit-通过outlook打开和发送电子邮件
- 13. AV发送通过Outlook电子邮件和赎回时
- 14. 如何通过Outlook 2016发送HTML响应电子邮件?
- 15. 通过MS Outlook发送电子邮件并禁用警告
- 16. 如何通过Outlook发送电子邮件
- 17. 通过电子邮件发送文件
- 18. 如何通过电子邮件发送电子邮件地址
- 19. Excel VBA - 发送Outlook电子邮件
- 20. 创建并发送Outlook电子邮件
- 21. 发送电子邮件与Outlook C#
- 22. PowerShell Outlook发送电子邮件
- 23. 通过Javamail发送电子邮件
- 24. 回复通过电子邮件发送
- 25. 通过VB6发送电子邮件
- 26. 通过Gmail发送电子邮件
- 27. 通过电子邮件发送Spark DataFrame
- 28. 通过gmail发送电子邮件,Laravel
- 29. 通过电子邮件发送的navigator.userAgent
- 30. 通过Gmail发送Sitecore电子邮件
为什么你要通过Outlook发送邮件? – ThiefMaster 2011-06-13 15:34:32
@ThiefMaster:我的smtp服务器与我的电子邮件不同 - 因此,我需要通过我的互联网提供商('att')“通道”我的smtp,尽管我使用的是不同的电子邮件地址(不是' att's')发送电子邮件。 'Outlook'已配置为处理此问题。如果还有其他解决方案(基于非'Outlook'),也会支持这个解决方案,我很乐意听取建议。 – user3262424 2011-06-13 18:11:29
正确的解决方案是使用python的[smtplib](http://docs.python.org/library/smtplib.html#smtp-example) – ThiefMaster 2011-06-13 18:35:04