[英]Send myself an email using win32com.client
I have a template from code I've gathered from different places to send emails in various of my scripts: 我有一个模板,这些模板是我从不同地方收集的代码,可以通过各种脚本发送电子邮件:
import win32com.client
##################################################
##################################################
##################################################
##################################################
##################################################
#this is a mutipurpose email template
def email_template(recipient, email_subject, mail_body, attachment1, attachment2):
Format = { 'UNSPECIFIED' : 0, 'PLAIN' : 1, 'HTML' : 2, 'RTF' : 3}
profile = "Outlook"
#session = win32com.client.Dispatch("Mapi.Session")
outlook = win32com.client.Dispatch("Outlook.Application")
#session.Logon(profile)
mainMsg = outlook.CreateItem(0)
mainMsg.To = recipient
mainMsg.BodyFormat = Format['RTF']
#########################
#check if there is a mail body
try:
mainMsg.Subject = email_subject
except:
mainMsg.Subject = 'No subject'
#########################
#check if there is a mail body
try:
mainMsg.HTMLBody = mail_body
except:
mainMsg.HTMLBody = 'No email body defined'
#########################
#add first attachement if available
try:
mainMsg.Attachments.Add(attachment1)
except:
pass
#########################
#add second attachement if available
try:
mainMsg.Attachments.Add(attachment2)
except:
pass
mainMsg.Send() #this line actually sends the email
Works perfectly. 完美运作。 Simple.
简单。 However I have a slight problem, I'm building a script that needs to send the user an email.
但是我有一个小问题,我正在构建一个脚本,该脚本需要向用户发送电子邮件。 Using this template, how do I get the users outlook email?
使用此模板,如何获得用户的Outlook电子邮件? I mean like something like just using
"me"
and it will get my address. 我的意思是像使用
"me"
这样的东西,它将得到我的地址。
Thanks!! 谢谢!!
The CurrentUser property of the Namespace or Account class allows to get the display name of the currently logged-on user as a Recipient object. Namespace或Account类的CurrentUser属性允许获取当前登录用户的显示名称作为Recipient对象。 The Recipient class provides the Address property which returns a string representing the e-mail address of the Recipient.
Recipient类提供了Address属性,该属性返回一个表示收件人的电子邮件地址的字符串。
In case of Exchange server you may need to call more properties and methods: 如果是Exchange服务器,则可能需要调用更多属性和方法:
Finally, I'd recommend using the Recipients property of the MailItem class which returns a Recipients collection that represents all the recipients for the Outlook item. 最后,我建议使用MailItem类的Recipients属性,该属性返回代表Outlook项目的所有收件人的Recipients集合。 The Add method creates a new recipient in the Recipients collection.
Add方法在“收件人”集合中创建一个新的收件人。
Sub CreateStatusReportToBoss()
Dim myItem As Outlook.MailItem
Dim myRecipient As Outlook.Recipient
Set myItem = Application.CreateItem(olMailItem)
Set myRecipient = myItem.Recipients.Add("Eugene Astafiev")
myItem.Subject = "Status Report"
myItem.Display
End Sub
Don't forget to call the Resolve or ResolveAll methods of the Recipient(s) class to get your recipients resolved against the address book. 不要忘记调用Recipient(s)类的Resolve或ResolveAll方法来使收件人针对通讯录进行解析。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.