vba 在 Exchange 环境中从 Excel 发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6122747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:18:39  来源:igfitidea点击:

Send email from Excel in Exchange environment

emailexcel-vbaexchange-servervbaexcel

提问by greye

I have a userform that helps different users fill in data into the spreadsheet. As soon as the data is inserted it should also be sent by email to a few recipients, depending on the options filled in the form.

我有一个用户表单,可以帮助不同的用户将数据填充到电子表格中。一旦数据被插入,它也应该通过电子邮件发送给几个收件人,具体取决于表单中填写的选项。

This happens within a corporate environment using Exchange. I would create a new email account for this file to be able to send the email as an entity and not use the user's email account.

这发生在使用 Exchange 的公司环境中。我会为此文件创建一个新的电子邮件帐户,以便能够将电子邮件作为实体发送,而不是使用用户的电子邮件帐户。

Is this possible? How? I have googled for it and all I can find is how to create a mail message that the user sends from his account.

这可能吗?如何?我用谷歌搜索过,我能找到的只是如何创建用户从他的帐户发送的邮件消息。

回答by Jean-Fran?ois Corbett

I've used the code below (source) to send e-mails from Excel-VBA. I've only tested it with my own e-mail account, but I assume you could have it send mail from a different account (msgOne.from = ...), as long as the user has permission to send from that account on the Exchange server.

我使用下面的代码(源代码)从 Excel-VBA 发送电子邮件。我只用我自己的电子邮件帐户对其进行了测试,但我假设您可以让它从不同的帐户 ( msgOne.from = ...)发送邮件,只要用户有权从 Exchange 服务器上的该帐户发送邮件。

Dim cdoConfig
Dim msgOne

Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
    .Item(cdoSendUsingMethod) = cdoSendUsingPort
    .Item(cdoSMTPServerPort) = 25 '465 ' (your port number) usually is 25
    .Item(cdoSMTPServer) = "smtp.mysmtpserver.com" ' your SMTP server goes here
    '.Item(cdoSendUserName) = "My Username"
    '.Item(cdoSendPassword) = "myPassword"
    .Update
End With

Set msgOne = CreateObject("CDO.Message")
Set msgOne.Configuration = cdoConfig
msgOne.To = "[email protected]"
msgOne.from = "[email protected]"
msgOne.subject = "Test CDO"
msgOne.TextBody = "It works just fine."
msgOne.Send

Unfortunately, I can't test this hypothesis at this time, as I'm only set up to send from one account. Let me know how it works out!

不幸的是,我目前无法测试这个假设,因为我只设置为从一个帐户发送。让我知道它是如何工作的!

回答by vzczc

If the excel application is running on a machine with outlook, you can something along the following.

如果 excel 应用程序在装有 Outlook 的机器上运行,您可以执行以下操作。

Function SendEmailWithOutlook(er As emailRecord, 
           recipients As String, 
           cc As String, 
           subject As String, 
           body As String, 
           attachmentPath As String) As Boolean
    Dim errorMsg As String
    Dim OutApp As Object
    Dim OutMail As Object
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    On Error GoTo errHandle
    If (er.useTestEmail = True) Then
        recipients = er.emailTest
        cc = er.emailTest
    End If
    With OutMail
        If er.emailFrom <> "" Then
            .sentOnBehalfOfName = er.emailFrom
        End If
        .To = recipients
        .cc = cc
        .bcc = er.emailBcc
        .subject = subject
        .htmlBody = body
        If attachmentPath <> "" Then
            .Attachments.Add attachmentPath
        End If
        .Send   'or use .Display
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
    SendEmailWithOutlook = True
    Exit Function
errHandle:
    errorMsg = "Error sending mail via outlook: " & Err.Description & vbCrLf
    errorMsg = errorMsg & "OnBehalfOf:" & er.emailFrom & vbCrLf
    errorMsg = errorMsg & "Recipients: " & recipients & vbCrLf
    errorMsg = errorMsg & "CC: " & cc & vbCrLf
    errorMsg = errorMsg & "BCC: " & er.emailBcc
    MsgBox errorMsg
    SendEmailWithOutlook = False
End Function

Add a reference to Microsoft Outlook 14.0 Object Library

添加对 Microsoft Outlook 14.0 对象库的引用

回答by Dmitry Streblechenko

Why not use the Outlook Object Model?

为什么不使用 Outlook 对象模型?

You can give the current user the right to send on behalf of the specified user, then set MailItem.SentOnBehalfOfNameand MailItem.ReplyRecipients(if necessary) properties before callign MailItem.Send.

您可以赋予当前用户代表指定用户发送的权限,然后在 callign 之前设置MailItem.SentOnBehalfOfNameMailItem.ReplyRecipients(如果需要)属性MailItem.Send