如何在电子邮件正文中添加文本?

How to add text to body of e-mail?

我搜索了 Whosebug 和其他人,没有好的结果。

我必须通过 Access VBA 发送电子邮件。我正在使用 Selenium Basic 库。

我可以,访问电子邮件网页,点击“新邮件”按钮,填写“收件人”、“主题”、“复制到”和“盲复制到”字段(如果提供此类参数) , 然后单击“发送电子邮件”按钮。

我不能,编辑电子邮件的富文本正文。我可以发送电子邮件,但它们是空白的。

Public Sub sendEmail( _
    emailPage As String, _
    sendTo As String, _
    subject As String, _
    emailText As String, _
    Optional copyTo As String, _
    Optional blindCopyTo As String)
    
'Parameters above: emailPage is the intranet, corporative e-mail site from which the e-mails must be 
'sent; sendTo is the e-mail address to which the e-mail will be sent; subject is the subject of the 
'e-mail; emailText is the text that I want to go in the body of the e-mail. 

'Variables below: preparing variables to be used, like a webDriver. I tried to mingle Selenium and 
'Windows HTML objects to no vail, as one can infer from the commented variable

    Dim browser As WebDriver
    Dim webEle As WebElement
    Dim theKeys As selenium.Keys
    Dim por As By
    'Dim CurrentWindow As HTMLWindowProxy
    
'choosing the web browser driver. I chose firefox since it went farther than chrome*:

    Set browser = New FirefoxDriver

'with the browser driver, I go to the e-mail webpage:

    With browser
        .Get emailPage 

'Below I find the elements with the XPath to interact with

        .SwitchToFrame "s_MainFrame"
        .FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[2]/div[10]/table[1]/tbody/tr/td[2]/table/tbody/tr/td[1]/span").Click
        .FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[1]/div/div[5]/div/form/div/div/div[5]/table[1]/tbody/tr[3]/td[3]/div/textarea").SendKeys sendTo
        If IsMissing(copyTo) = False Then
            .FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[1]/div/div[5]/div/form/div/div/div[5]/table[1]/tbody/tr[4]/td[3]/div/textarea").SendKeys copyTo
        End If
        If IsMissing(blindCopyTo) - False Then
            .FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[1]/div/div[5]/div/form/div/div/div[5]/table[1]/tbody/tr[5]/td[3]/div/textarea").SendKeys blindCopyTo
        End If
        .FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[1]/div/div[5]/div/form/div/div/div[5]/table[1]/tbody/tr[6]/td[3]/div/textarea").SendKeys subject

'And here is the problem: I cannot access the e-mail body text to change it. SendKeys won't work. I also
'tried inserting HTML texts like <HTLM><BODY>email text goes here</BODY></HTML> and 
'<HTLM><H1>email text goes here</BODY></HTML>, to no vail.

        .FindElementByXPath("/html/body/div/div").SendKeys emailText
        '.SwitchToFrame "s_MainFrame"

'After that, I click the button to send the e-mail, close the browser driver, and set it to nothing.
        .FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[2]/div[14]/table[1]/tbody/tr/td[1]/span").Click
    End With
    browser.Close
    Set browser = Nothing
   
End Sub

这些是我在检查正文时得到的元素。在干净的电子邮件正文文本的第一行中,元素是

<div><br></div>

它的 XPath 是:

/html/body/div/div

但是用它作为访问路径在我的测试中没有用。下面是富文本正文元素的主要值:

<body role="main" id="e-$new-0-bodyrich-editor" class="s-mailbody-edit s-disable-inotesstyle">

它是 XPath:

/html/body

在 Chrome 中,出现错误,无法打开电子邮件页面。我有 Chrome 87 版本和相应的驱动程序。

不好意思打扰大家了!

我做了更多测试,发现我必须为 iframe 引用 XPath:

    .FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[1]/div/div[3]/div[1]/div/div[5]/div/form/div/div/div[2]/div/div/div[1]/iframe").SendKeys emailText

我还使用 While DoEvents 更新了函数,以便仅在电子邮件页面写入已成功发送电子邮件的引用 HTML 元素后关闭浏览器驱动程序:

    Do While InStr(1, browser.FindElementByXPath("/html/body/div[2]/div/div[4]/div/div/div[1]/div/div[2]/div[1]/div/div[2]/div/table[1]/tbody/tr/td[2]/span").text, "Email sent successfully") = 0
        DoEvents
    Loop
    

此外,一些声明的变量未使用并被删除,例如 webEle。最终代码中只声明了浏览器变量。