【问题标题】:How to send HTML Email using VBA from MS Access如何从 MS Access 使用 VBA 发送 HTML 电子邮件
【发布时间】:2020-05-20 10:42:52
【问题描述】:

我自己找到了解决方案。我没有将 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

【问题讨论】:

    标签: vba ms-access gmail ms-access-2010 cdo.message


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您必须将您的数据包装在一个完全格式化的 HTML 页面中,用于.HTMLbody - 就像这个例子:

      <!DOCTYPE html> 
      
      <html xmlns="http://www.w3.org/1999/xhtml" lang="en" charset="iso-8859-1">
      <head>
          <title>E-mail Test</title>
      </head>
      <body style="font-family: 'segoe ui', helvetica, arial, sans-serif">
      
          <span style="font-size: 12pt">
              <p>Best regards,</p>
              <p style="font-style: italic">
                  Firstname Lastname<br />
                  Company
              </p>
              <img src="https://i.imgur.com/HbdRTBD.gif" alt="Company" title="Company"/>
          </span>
      
          <table style="font-size: 9pt">
              <tr>
                  <td>Phone:</td>
                  <td>00 00 00 00</td>
              </tr>
              <tr>
                  <td>E-mail:</td>
                  <td>
                      <a title="name@example.com" href="mailto:Firstname%20Lastname%20%3cname@example.com%3e?subject=Received%20data">
                          Firstname Lastname
                      </a>
                  </td>
              </tr>
          </table>
          <hr>
      
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 2021-11-21
        • 2020-05-16
        • 2021-02-14
        • 1970-01-01
        • 2011-09-28
        • 2021-05-11
        • 1970-01-01
        • 2015-06-07
        相关资源
        最近更新 更多