通过 Outlook 365 发送电子邮件

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

我正在更新一个旧程序。我注意到在新版本中电子邮件发送不起作用。

我搜索了参考。有没有一种无需参考即可发送的方法,或者 Outlook 365(本地安装)的参考在哪里?

我搜索了地点。我在没有参考资料的情况下尝试过。

我从网络上的示例中提取了此代码。

Public Function SendEmail2()
Dim varName As Variant
Dim varCC As Variant
Dim varSubject As Variant
Dim varBody As Variant
varName = "[email protected]"
varSubject = "Hello"
varBody = "Let's go"
DoCmd.SendObject , , , varName, varCC, , varSubject, varBody, True, False
End Function

错误信息

运行时错误2046
发送对象操作命令现在不可用

vba ms-access ms-access-2007
1个回答
0
投票

这是使用 Outlook 从 MS Access 发送电子邮件的完整子程序。

Sub SendOutlookMail(mTo As String, Optional mCC As String, Optional mBCC As String, Optional mSubject As String, _
   Optional mBody As String, Optional mAttach As String, Optional mSender As String, Optional SendAsHTML As Boolean = False, _
   Optional DirectSend As Boolean = False, Optional TemplatePath As String = "")
' Important: if mSender is filled, it must be the technical name of the Outlook account
   
   Dim Attachment As Variant
   Dim i          As Long
   
   On Error Resume Next
   
   If oApp Is Nothing Then
      Set oApp = CreateObject("OUTLOOK.Application")
   End If
   If Len(TemplatePath) > 0 Then
      Set oMail = oApp.CreateItemFromTemplate(TemplatePath)
   Else
      Set oMail = oApp.CreateItem(olMailItem)
   End If
   With oMail
      .To = mTo
      .SUBJECT = mSubject
      .CC = mCC
      .BCC = mBCC
      If SendAsHTML Then
         .HTMLBody = .HTMLBody & mBody
      Else
         .Body = .Body & mBody
      End If
      If Len(TemplatePath) > 0 Then
         ' In this sub I suggest that a template is always a html-template
         .HTMLBody = .HTMLBody
      End If
      .ReadReceiptRequested = False
      
      'Attachments
      Attachment = Split(mAttach, ',')
      For i = LBound(Attachment) To UBound(Attachment)
         .Attachments.Add Attachment(i)
      Next i
      
      If mSender <> "" Then
         .SentOnBehalfOfName = mSender ' Name of the Outlook account
      End If
      If DirectSend Then
         .Send
      Else
         .Display
      End If
   End With
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.