kpg
kpg

Reputation: 671

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

In a legacy vb6 program I am sending email using the full version of outlook (not outlook express) using the code below. Works great.

Now the user wants the 'from:' address to be different for different uses so that when the email is replied to the response will show up in that users inbox in outlook. Currently the email from is the main business email address.

I thought that was an easy fix; I just need to set the .from property in the OutMail object, however it seems there is no '.from' property in the OutMail object. (It may be called something else?)

So at this point I'm wondering how it works now, with no .from specified, and I assume the user has multiple email accounts setup in outlook, it is using the main email for the business, not individual users.

How can I specify a from email address using this technique?

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

Upvotes: 1

Views: 113

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66245

If all you care about is that the reply goes to the right mailbox, set that email address as the reply address. You can do that using Mailtem.ReplyRecipients.Add.

Upvotes: 5

Related Questions