Sending emails question

User avatar
Abraxus
3StarLounger
Posts: 254
Joined: 01 Mar 2010, 17:34
Location: Blue Springs, MO

Sending emails question

Post by Abraxus »

I have a database used frequently to send email to large numbers of people.

In my outlook (this is for work) I have the ability to change the FROM so it comes from another email address than mine.

Can I have my VBA code select that FROM automatically? Right now I have to display the email, change it manually, then hit send...

Here's my code:

Code: Select all

Sub SendMail()
    Debug.Print Now
    'Create a new Microsoft Outlook session
    Set appOutlook = CreateObject("outlook.application")
    
    'Open the list to send to
    Dim db As dao.Database
    Set db = CurrentDb
    Dim rst As dao.Recordset
    Set rst = db.OpenRecordset("SELECT * FROM tblAddresses WHERE Sent = FALSE", dbOpenDynaset)
    While Not rst.EOF
        Debug.Print rst!Address
        
        'create a new message
        Set Message = appOutlook.CreateItem(olMailItem)
                 
        With Message
           .Subject = "This is the subject"
    
           Dim strBody As String
           strBody = "This is the body<br><br><br>"
           strBody = strBody & "And More body<br><br>"
                          
           .HTMLBody = strBody
                
           .to = rst!Address
           '.Cc = strCC
           
           '.Attachments.Add ("C:\Temp\ShippingReport - " & strCostCenter & ".xls")
           .Display
           .Send
        End With
                
        'Update as sent
        rst.Edit
        rst!Sent = True
        rst.Update
        rst.MoveNext
    Wend
    
    'appOutlook = Nothing
    
    Debug.Print Now
    MsgBox "Done!"
End Sub
I've tried

Code: Select all

.From = "otheraddress@test.com"
to no avail...

Any hep is appreciated
Morgan

User avatar
Wendell
4StarLounger
Posts: 482
Joined: 24 Jan 2010, 15:02
Location: Colorado, USA

Re: Sending emails question

Post by Wendell »

I believe what you want to change is the .Sender property - see https://msdn.microsoft.com/en-us/librar ... 69056.aspx for the details. Note that it does require the address you set to be a defined account in your Outlook installation. Hope this helps.
Wendell
You can't see the view if you don't climb the mountain!

User avatar
Abraxus
3StarLounger
Posts: 254
Joined: 01 Mar 2010, 17:34
Location: Blue Springs, MO

Re: Sending emails question

Post by Abraxus »

I tried adding .sender = "Other Email Address" and I tried .sender = "Other Email Account" but neither worked...

I also tried .addressentry.Sender = "other@emailaddress.com"

I don't read anything in your link that speaks to setting the value, just reading the value...still confused.

It's not a huge deal, as I rarely have to do this sort of thing, just a nice to have, if I could get it working...
Morgan

Mark L
3StarLounger
Posts: 331
Joined: 11 Feb 2010, 03:55
Location: Land O Lakes, FL

Re: Sending emails question

Post by Mark L »

I think you need to use: .SendUsingAccount= xxx 'where xxx = Outlook Account
Mark Liquorman
Land O Lakes, FL
see my website http://www.liquorman.net for Access Tips and Tricks, and for my Liquorman Utilities.

User avatar
HansV
Administrator
Posts: 78556
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Sending emails question

Post by HansV »

Ron de Bruin provides details about using SendUsingAccount: Use the mail account you want in your mail macro.
Best wishes,
Hans

User avatar
Abraxus
3StarLounger
Posts: 254
Joined: 01 Mar 2010, 17:34
Location: Blue Springs, MO

Re: Sending emails question

Post by Abraxus »

Thanks everyone!

What ended up working is this:

Code: Select all

.SentOnBehalfOfName = """TheAccountName"" <TheEmail@theCompany.com>"
Morgan

User avatar
HansV
Administrator
Posts: 78556
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Sending emails question

Post by HansV »

Good to hear that this worked for you.

Be aware that this doesn't really change the sender. The recipient will see something like "Abraxus on behalf of TheAccountName" as sender.
Best wishes,
Hans

User avatar
Abraxus
3StarLounger
Posts: 254
Joined: 01 Mar 2010, 17:34
Location: Blue Springs, MO

Re: Sending emails question

Post by Abraxus »

HansV wrote:Good to hear that this worked for you.

Be aware that this doesn't really change the sender. The recipient will see something like "Abraxus on behalf of TheAccountName" as sender.
I thought that might happen, but in my tests you can not tell that it was not sent by the other account. The headers look like it originated from that account. possibly because i have my outlook set up to allow me to send form that account?

Either way, I'm good and appreciate everyone's advice!
Morgan

User avatar
HansV
Administrator
Posts: 78556
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Sending emails question

Post by HansV »

Indeed, it's probably because you have permission to send from that account.
Best wishes,
Hans