【问题标题】:Include an email signature in包括电子邮件签名
【发布时间】:2020-07-14 09:04:39
【问题描述】:

以下代码由 HK1 发布,以响应 2012 年 7 月 20 日在 VBA 中发送没有 Outlook 的电子邮件的答案。

代码运行良好,但我需要在文本末尾添加一个签名块(基本上是本地文件夹中的 jpg 文件),但我能想到的最好的方法是添加路径( text) 而不是图像本身到电子邮件正文。

Const cdoSendUsingPickup = 1
Const cdoSendUsingPort = 2
Const cdoAnonymous = 0
' Use basic (clear-text) authentication.
Const cdoBasic = 1
' Use NTLM authentication
Const cdoNTLM = 2 'NTLM

Public Sub SendEmail()
 Dim imsg As Object
 Dim iconf As Object
 Dim flds As Object
 Dim schema As String

 Set imsg = CreateObject("CDO.Message")
  Set iconf = CreateObject("CDO.Configuration")
Set flds = iconf.Fields

' send one copy with SMTP server (with autentication)
schema = "http://schemas.microsoft.com/cdo/configuration/"
flds.Item(schema & "sendusing") = cdoSendUsingPort
flds.Item(schema & "smtpserver") = "mail.myserver.com"
flds.Item(schema & "smtpserverport") = 25
flds.Item(schema & "smtpauthenticate") = cdoBasic
flds.Item(schema & "sendusername") = "email@email.com"
flds.Item(schema & "sendpassword") = "password"
flds.Item(schema & "smtpusessl") = False
flds.Update

With imsg
    .To = "email@email.com"
    .From = "email@email.com"
    .Subject = "Test Send"
    .HTMLBody = "Test"
    '.Sender = "Sender"
    '.Organization = "My Company"
    '.ReplyTo = "address@mycompany.com"
    Set .Configuration = iconf
    .Send
End With

   Set iconf = Nothing
   Set imsg = Nothing
   Set flds = Nothing
End Sub

我尝试将代码修改如下,但这只是将文件路径添加到正文中:

With imsg
  .To = vRecipients
  .From = senderEmail
  .CC = vCC
  .Subject = vSubject

  vBody = Replace(vBody, vbCrLf, "<br>")
  vBody = "<FONT face=arial size=2>" & vBody

  vBody = vBody & "<br>" & signFile
  .HTMLBody = vBody

  .Sender = senderName
  .ReplyTo = senderEmail
  .AddAttachment vAttachments

  Set .Configuration = iconf
  .Send
End With

有什么建议吗?

【问题讨论】:

  • 所以signFile 只包含签名文件的文件名。然后你必须读取文件的内容并将其添加到正文中。

标签: email ms-access vba


【解决方案1】:

dwo 是正确的。您需要使用文件系统对象或文件对象来读取 signFile 的文本内容。否则你的代码看起来应该可以工作。

这是一个可以用来读取文件内容的函数。该函数只是假设您将传递您的应用程序至少具有读取权限的文本文件的完整路径和文件名。

Public Function GetTextFileContents(sFilePath as String) As String
    If Dir(sFilePath) <> "" Then
        Dim fso As Object
        Dim ts As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set ts = fso.GetFile(sFilePath).OpenAsTextStream(1, -2)
        GetTextFileContents = ts.ReadAll
        ts.Close
        Set ts = Nothing
        Set fso = Nothing
    End If
End Function

【讨论】:

  • 我尝试读取文件内容并将signFile替换为GetTextFileContents,但图像没有显示在电子邮件中,而是显示ÿØÿà,我猜这是二进制内容的一部分?
  • 签名中的图像是一个复杂的问题。我诉诸于告诉我的用户,将他们包括在内是不可行的。我不认为图像实际上存储在签名中。我认为这可能是一个相对文件路径,但我不记得了。您可以通过使用记事本手动打开签名文件来查找。它应该是一个 .HTML 文件。在 Google 中搜索 Outlook 存储签名的位置。
  • 实际上,签名文件只是一个带有徽标和一些联系方式的jpg。所以我想归结为使用 CDO.Message 时如何在正文中包含图像?
  • 我不会说它不能完成,但我在正文中包含图像的唯一方法是使用 Outlook。即便如此,这也不是一件容易的事。
  • 如果您可以将您的徽标放在允许热链接的网站上,那么您应该可以使用 html img 标签。看到这个帖子:stackoverflow.com/questions/10904763/…
【解决方案2】:

您是否尝试过现成的解决方案?我过去使用过 exclaimer 和 emailsignanture.com。我认为 emailsignature 如何通过您讨论的 jpeg 挑战是图像是作为 HTML markdown 完成的,类似于:

(/path/to/img.jpg)

(/path/to/img.jpg "Optional title")

它处理不同渲染代理的大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 2015-12-22
    相关资源
    最近更新 更多