如何从MS Access使用VBA发送HTML电子邮件

问题描述 投票:0回答:2

发送纯文本邮件没有问题。如果我尝试发送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") = _
   "[email protected]"
   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 = "[email protected]"
      .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
2个回答
0
投票

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


0
投票

您必须将数据包装在.HTMLbody的完全格式化的HTML页面中-类似于此示例:

<!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="[email protected]" href="mailto:Firstname%20Lastname%20%[email protected]%3e?subject=Received%20data">
                    Firstname Lastname
                </a>
            </td>
        </tr>
    </table>
    <hr>

</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.