我正在尝试执行以下操作:
- 使用 VBA 生成 SMTP 电子邮件
- 显示嵌入在电子邮件中的 InfoPath 表单
- 这将链接到 Access 数据库
不幸的是,我无法使用 CDO 正确配置传出 SMTP 消息。
我一直在构建这篇文章中找到的信息,不幸的是这些信息在 C# 中,并且其中一些功能不直接映射到 VB/VBA。具体来说,“Message.Headers”部分不是 CDO.Message 类具有的属性。
我已经能够更改附件并正确添加它们,但以下工作正常:
.fields("urn:schemas:mailheader:Message-Class") = "IPM.InfoPathForm.InfoPath"
.fields("urn:schemas:mailheader:Content-Class") = "InfoPathForm.InfoPath"
该表单未显示在电子邮件中(xml 和 xsn 都显示为附件,而不显示为嵌入式表单)。
在比较有效表单(手动生成)和无效表单(按程序生成)之间的电子邮件源时,我无法确定我还必须更改什么。电子邮件中还有几个内容标签,一个是:
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
在此之下有来自实际表格的文本。
大概这些部分需要通过我未正确使用的某些设置自动生成。
这是我用来生成电子邮件的代码。请注意,这两个附件是有效的,当我使用 InfoPath 发送电子邮件时,我保存了一个正确显示的表单。
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