使用 outlook 自动发送电子邮件时如何设置从电子邮件地址

How to set from email address when sending email by automation with outlook

在旧版 vb6 程序中,我使用以下代码使用完整版 outlook(不是 outlook express)发送电子邮件。效果很好。

现在用户希望 'from:' 地址因不同用途而不同,这样当电子邮件被回复时,回复将显示在 outlook 的用户收件箱中。目前来自的电子邮件是主要的企业电子邮件地址。

我认为这很容易解决;我只需要在 OutMail 对象中设置 .from 属性,但是 OutMail 对象中似乎没有“.from”属性。 (可能叫别的名字?)

所以此时我想知道它现在是如何工作的,没有指定 .from,我假设用户在 Outlook 中设置了多个电子邮件帐户,它使用的是企业的主要电子邮件,而不是个人用户.

如何使用此技术指定发件人电子邮件地址?

Dim mOutlookApp As Object
    Set mOutlookApp = GetObject("", "Outlook.application")

    Dim olNs As Object
    Set olNs = mOutlookApp.GetNamespace("MAPI")
    olNs.Logon

    Dim OutMail As Object
    Set OutMail = mOutlookApp.CreateItem(0)

    'Set the To and Subject lines.  Send the message.
    With OutMail
        .To = txtTo
        .CC = txtCC
        .Subject = txtSubjext
        .HTMLBody = txtBody & vbCrLf

        Dim myAttachments As Object
        Set myAttachments = .Attachments
        vAttach = Split(mAttachments, ",")
        For i = 0 To UBound(vAttach)
            myAttachments.add vAttach(i)
        Next i


        Dim myFolder As Object
        Set myFolder = olNs.GetDefaultFolder(5) 'olFolderSent
        Set .SaveSentMessageFolder = myFolder

        StatusBar1.Panels(1).Text = "Status: Sending"

        .send
    End With

如果您只关心回复是否发送到正确的邮箱,请将该电子邮件地址设置为回复地址。您可以使用 Mailtem.ReplyRecipients.Add.

来做到这一点