首页 > 解决方案 > 如何从 MS Access 使用 VBA 发送 HTML 电子邮件

问题描述

我自己找到了解决方案。我没有将 HTML 存储在变量 Text 中,而是直接使用数据库中的 html 字段。所以我现在使用 .htmlbody = rs.fields("tekst") 这很好用!

发送纯文本邮件没有问题。如果我尝试发送 HTML,它总是以纯文本形式发送。

我尝试使用.HTMLbody而不是,.Body但仍然是同样的问题。

HTML 文本保存在 MS Access 表的字段tekst中,格式为备忘录 - 富文本。我正在使用 Gmail 帐户。

Sub SendGmail()

   'creating a CDO object
   Dim Mail As CDO.Message
   Set Mail = New CDO.Message

   'Enable SSL Authentication
   Mail.Configuration.Fields.Item _
   ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True

   'Make SMTP authentication Enabled=true (1)
   Mail.Configuration.Fields.Item _
   ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1

   'Set the SMTP server and port Details
   'Get these details from the Settings Page of your Gmail Account
   Mail.Configuration.Fields.Item _
   ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
   "smtp.gmail.com"
   Mail.Configuration.Fields.Item _
   ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
   Mail.Configuration.Fields.Item _
   ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

   'Set your credentials of your Gmail Account
   Mail.Configuration.Fields.Item _
   ("http://schemas.microsoft.com/cdo/configuration/sendusername") = _
   "myaddress@gmail.com"
   Mail.Configuration.Fields.Item _
   ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = _
   "*********"

   'Update the configuration fields
   Mail.Configuration.Fields.Update

   'get text
   Set rs = CurrentDb.OpenRecordset("select tekst from tekst")
   rs.MoveFirst
   txt = "<html><body>" & rs.Fields("tekst") & "</body></html>"

   'Set All Email Properties
   With Mail
      .AutoGenerateTextBody = False
      .Subject = "test123"
      .From = "myaddress@gmail.com"
      .To = "toaddress"
      .CC = ""
      .HTMLBody = txt
      '.AddAttachment ("Folder Address") 'To attach Documents in mail
   End With
End Sub

标签: vbams-accessgmailms-access-2010cdo.message

解决方案


尝试将ContentMediaType属性设置为"text/html".


推荐阅读