使用SMTP/CDO在带有VBA的电子邮件中嵌入InfoPath表单 [英] Embedding InfoPath form in email with VBA using SMTP/CDO
问题描述
我正在尝试执行以下操作:
I am attempting to do the following:
- 使用VBA生成SMTP电子邮件
- 显示嵌入在电子邮件中的InfoPath表单
- 这将链接到Access数据库
- Use VBA to generate SMTP email
- Display InfoPath form embedded in email
- This will be linked to an Access database
不幸的是,我无法使用CDO正确配置传出SMTP邮件.
Unfortunately, I have not been able to configure the outgoing SMTP message correctly using CDO.
I have been building on information found in this post which unfortunately is in C# and some of this functionality doesn't directly map to VB/VBA. Specifically, the "Message.Headers" part is not a property the CDO.Message class has.
我可以更改附件并正确添加它们,但是可以进行以下操作:
I have been able to change the attachments and add them correctly but while the following works:
.fields("urn:schemas:mailheader:Message-Class") = "IPM.InfoPathForm.InfoPath" .fields("urn:schemas:mailheader:Content-Class") = "InfoPathForm.InfoPath"
该表单未显示在电子邮件中(xml和xsn都显示为附件,而未显示为嵌入式表单).
the form is not displayed in the email (both the xml and xsn are appearing as attachments and NOT displaying as an embedded form).
在比较有效形式(手动生成)和无效形式(按比例生成)之间的电子邮件来源时,我无法确定我还必须更改什么.电子邮件中还有几个内容标签,一个是:
In comparing email source between a valid form (generated manually) and invalid (generated proramatically) I have not been able to determine what else I must change. There are several more content tags in the emails, one is:
Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable <html dir=3D"ltr" id=3D"L044F61201A9E6BE2"> <head> <meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-= 1"> </head> (etc, there is a bunch more)
另一个是:
Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
在此之下是实际形式的文本.
And under this there is text from the actual form.
大概这些部分需要通过我不是正确使用的某些设置自动生成.
Presumably these sections need to be generated automatically by some setting I am not using correctly.
这是我用来生成电子邮件的代码.请注意,这两个附件是有效的,当我使用InfoPath发送电子邮件时,我将其保存在一个表格中,该表格可以正确显示.
Here is the code I am using to generate my emails. Note that the two attachments are valid and ones I saved off a form which does display correctly, when I use InfoPath to send the email.
Sub testSendingEmail() On Error GoTo errHndlr 'boring error handling Dim myAttach(1 To 2) As String Dim myContentType(1 To 2) As String myAttach(1) = "C:\Users\UserID\Desktop\infoPath\outlooksaves\Form1.xml" myAttach(2) = "C:\Users\UserID\Desktop\infoPath\outlooksaves\Add Projects Table Form.xsn" myContentType(1) = "application/x-microsoft-InfoPathForm" myContentType(2) = "application/x-microsoft-InfoPathFormTemplate" Dim mailMessage As Object Set mailMessage = CreateObject("CDO.Message") With mailMessage .Subject = "Test Automatic Subject 363" .from = "donotreply@a.com" .To = "TestEmail@gmail.com" .AddAttachment myAttach(1) .AddAttachment myAttach(2) .Attachments.Item(1).ContentMediaType = myContentType(1) .Attachments.Item(2).ContentMediaType = myContentType(2) 'testing - this isn't right :( .fields("urn:schemas:mailheader:Message-Class") = "IPM.InfoPathForm.InfoPath" .fields("urn:schemas:mailheader:Content-Class") = "InfoPathForm.InfoPath" With .Configuration.fields .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mailserve" .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 '.Item("http://schemas.microsoft.com/cdo/configuration/mailheader:Content-Class") = "InfoPathForm.InfoPath" .Update End With '.BodyPart.ContentClass = "InfoPathForm.InfoPath" 'from C# code '.Headers.Add "Content-Class", "InfoPathForm.InfoPath" ' .Headers.Add "Message-Class", "IPM.InfoPathForm.InfoPath" .Send End With Exit Sub errHndlr: Debug.Print "Error!" & " " & Err.Description End Sub
推荐答案
我能够使用另外一行来完成此工作.添加标头后,您需要添加.fields.update.
I was able to get this working with one extra line. You need to add .fields.update after you add the headers.
不幸的是,它不会在预览中显示该表单,但会将其作为适当的信息路径表单附加.
This will not show the form in the preview unfortunately, but it will attach it as proper infopath form.
'testing - this isn't right :( .fields("urn:schemas:mailheader:Message-Class") = "IPM.InfoPathForm.InfoPath" .fields("urn:schemas:mailheader:Content-Class") = "InfoPathForm.InfoPath" .fields.update 'Need to update the header fields
这篇关于使用SMTP/CDO在带有VBA的电子邮件中嵌入InfoPath表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!