发送电子邮件附件作为电子邮件正文 [英] Send Email With Attachment As Email Body
问题描述
我正在尝试发送一封附件作为电子邮件正文的电子邮件。我想复制附件 - >插入作为文本选项,您可以在实际的Outlook应用程序中使用。
这是代码我试过了,但是在电子邮件正文中将我的文档作为文本插入是行不通的。
import win32com.client
#some常数(来自http://msdn.microsoft.com/en-us/library/office/aa219371%28v=office.11%29.aspx)
olFormatHTML = 2
olFormatPlain = 1
olFormatRichText = 3
olFormatUnspecified = 0
olMailItem = 0x0
$
obj = win32com.client.Dispatch(" Outlook.Application")
attachment1 = " test.docx"
打开(attachment1,'r')作为myfile:
  data =(myfile.read())
$
newMail = obj.CreateItem(olMailItem)
newMail.Subject =" I AM SUBJECT !!" b
newMail.BodyFormat = olFormatHTML   #or olFormatRichText或olFormatPlain
newMail.HTMLBody = data
newMail.HTMLBody ="< h1>我是一个标题< / h1>< p>我是段落< / p>"
$
newMail.To =" help@example.com"
newMail.Attachments.Add(Source = attachment1)
#在新窗口中打开并允许在发送之前进行审核
newMail。显示()
你曾提到过,"我想要复制附件 - >插入作为文本选项,您可以在实际的Outlook应用程序中使用。"
您可以尝试参考以下示例可能对您有帮助。
Sub demo()
Dim wd As Object,editor As Object
Dim doc As Object
Dim oMail As MailItem
S. et wd = CreateObject(" Word.Application")
Set doc = wd.Documents.Open(" C:\ Users\v-padee \Desktop\mail_body.docx")
doc.Content.Copy
doc.Close
Set wd = Nothing
Set oMail = Application.CreateItem(olMailItem)
with oMail
.BodyFormat = olFormatRichText
设置editor = .GetInspector.WordEditor
editor.Content.Paste
。显示
结束
结束子
此外,您可以根据您的要求修改代码。
问候
Deepak
I am trying to send an email with an attachment as the body of the email. I want to replicate the attach-->insert as text option that you can use in the actual Outlook application.
This is the code I have tried, but it does not work to insert my document as text in the body of the email.
import win32com.client#some constants (from http://msdn.microsoft.com/en-us/library/office/aa219371%28v=office.11%29.aspx)
olFormatHTML = 2
olFormatPlain = 1
olFormatRichText = 3
olFormatUnspecified = 0
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
attachment1 = "test.docx"
with open(attachment1 , 'r') as myfile:
data=(myfile.read())
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "I AM SUBJECT!!"
newMail.BodyFormat = olFormatHTML #or olFormatRichText or olFormatPlain
newMail.HTMLBody = data
newMail.HTMLBody = "<h1>I am a title</h1><p>I am a paragraph</p>"
newMail.To = "help@example.com"
newMail.Attachments.Add(Source=attachment1)
# open up in a new window and allow review before send
newMail.Display()
You had mentioned that,"I want to replicate the attach-->insert as text option that you can use in the actual Outlook application."
You can try to refer example below may help you.
Sub demo() Dim wd As Object, editor As Object Dim doc As Object Dim oMail As MailItem Set wd = CreateObject("Word.Application") Set doc = wd.Documents.Open("C:\Users\v-padee\Desktop\mail_body.docx") doc.Content.Copy doc.Close Set wd = Nothing Set oMail = Application.CreateItem(olMailItem) With oMail .BodyFormat = olFormatRichText Set editor = .GetInspector.WordEditor editor.Content.Paste .Display End With End SubFurther, You can modify the code as per your requirement.
Regards
Deepak
这篇关于发送电子邮件附件作为电子邮件正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!