SendUsingAccount is not working

RogerCooper

Registered User.
Local time
Today, 00:04
Joined
Jul 30, 2014
Messages
167
I am still trying to use something other than my default e-mail to send invoices. This code should send an e-mail from my other e-mail.

Code:
Sub testEmail()
Dim oLook As Object
Dim oMail As Object
Set oLook = CreateObject("Outlook.Application")
Set oMail = oLook.CreateItem(0)
With oMail
.SendUsingAccount = "ar@spectroline.com"
.to ="<personal e-mail address>"
.body = "Test Message"
.subject = "Test"
.Send
End With
End Sub
An e-mail is created, but it comes from my default address. I can generate an e-mail from the ar@spectroline.com address manually. What I am doing wrong here?
 

Minty

AWF VIP
Local time
Today, 08:04
Joined
Jul 26, 2013
Messages
7,079
You can't refer to the account by name. It's a little convoluted as Outlook stores accounts in an arbitrary fashion, so you have to look up the account reference then send from that.

This function for looking the account number up
Code:
Public Function ListEMailAccounts(AcctToUSe As String) As Integer

    Dim OutApp As Object
    Dim i As Integer
    Dim AccNo As Integer
    Dim emailToSendTo As String

    Set OutApp = CreateObject("Outlook.Application")
    emailToSendTo = AcctToUSe                    'put required email address
    AccNo = 1

    'if smtp address=email we want to send to, acc no we are looking for is identified
    For i = 1 To OutApp.Session.accounts.Count
        'Uncomment the Debug.Print command to see all email addresses that belongs to you
Debug.Print "Acc name: " & OutApp.Session.accounts.Item(i) & " Acc number: " & i & " , email: " & OutApp.Session.accounts.Item(i).smtpAddress
        If OutApp.Session.accounts.Item(i).smtpAddress = emailToSendTo Then AccNo = i
    Next i

    ListEMailAccounts = AccNo

    Set OutApp = Nothing

End Function
And you would use it like this;
Code:
  Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
    On Error Resume Next
    With OutMail                                 ' This creates a blank email and captures the users default signature.
        .BodyFormat = 2                          '2            'olFormatHTML
        .Display
    End With

    signature = OutMail.HTMLBody

  
    EmailAcctNo = ListEMailAccounts("ar@spectroline.com")
    Set objOlAccount = OutApp.Session.accounts.Item(EmailAcctNo)
    
    With OutMail

        .To = Variable_To
        .CC = ""
        .BCC = ""
        .Subject = Variable_Subject
        Set .SendUsingAccount = objOlAccount
        .Attachments.Add (VarAttachFile)
        .HTMLBody = Variable_Body & signature
        .Display                                 'or use .Send
        .ReadReceiptRequested = False
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
Obviously you'll need to set up or change the variable names to match your routines.
 

RogerCooper

Registered User.
Local time
Today, 00:04
Joined
Jul 30, 2014
Messages
167
I adapted the code slightly for my situation

Code:
Public Function ListEMailAccounts(AcctToUSe As String) As Integer
    Dim OutApp As Object
    Dim i As Integer
    Dim AccNo As Integer
    Dim emailToSendTo As String
    
    Set OutApp = CreateObject("Outlook.Application")
    emailToSendTo = "[EMAIL="roger.cooper@aol.com"]roger.cooper@aol.com[/EMAIL]"                    'put required email address
    AccNo = 1
    'if smtp address=email we want to send to, acc no we are looking for is identified
    For i = 1 To OutApp.Session.accounts.Count
        'Uncomment the Debug.Print command to see all email addresses that belongs to you
Debug.Print "Acc name: " & OutApp.Session.accounts.Item(i) & " Acc number: " & i & " , email: " & OutApp.Session.accounts.Item(i).smtpAddress
        If OutApp.Session.accounts.Item(i).smtpAddress = emailToSendTo Then AccNo = i
    Next i
    ListEMailAccounts = AccNo
    Set OutApp = Nothing
End Function
Function Test2()
Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
    On Error Resume Next
    With OutMail                                 ' This creates a blank email and captures the users default signature.
        .BodyFormat = 2                          '2            'olFormatHTML
        .display
    End With
    signature = OutMail.HTMLBody
  
    EmailAcctNo = ListEMailAccounts("[EMAIL="ar@spectroline.com"]ar@spectroline.com[/EMAIL]")
    Set objOlAccount = OutApp.Session.accounts.Item(EmailAcctNo)
    
    With OutMail
        .To = "[EMAIL="info@spectroline.com"]info@spectroline.com[/EMAIL]"
        .CC = ""
        .BCC = ""
        .Subject = "test"
        Set .SendUsingAccount = objOlAccount
        '.Attachments.Add (VarAttachFile)
        .HTMLBody = "test"
        .display
        .ReadReceiptRequested = False
    End With
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
End Function
It still does not work. The e-mail generates under the default address. The debug window shows

Acc name: roger.cooper@spectroline.com Acc number: 1 , email: roger.cooper@spectroline.com
Acc name: ar@spectroline.com Acc number: 2 , email: ar@spectroline.com
 

Minty

AWF VIP
Local time
Today, 08:04
Joined
Jul 26, 2013
Messages
7,079
Do you have that email account registered to the outlook instance you are trying to send from ? e.g. It's folders and inbox are in your Outlook?
 

RogerCooper

Registered User.
Local time
Today, 00:04
Joined
Jul 30, 2014
Messages
167
Do you have that email account registered to the outlook instance you are trying to send from ? e.g. It's folders and inbox are in your Outlook?
Yes, the folders and the inbox are there. And I can select the "ar" address manually and send the e-mail from the "ar" address.
 

isladogs

CID Moderator
Staff member
Local time
Today, 08:04
Joined
Jan 14, 2017
Messages
13,375
Yes, the folders and the inbox are there. And I can select the "ar" address manually and send the e-mail from the "ar" address.
Why not try my CDO email code. The example db is standalone so you can test it quickly and see if it does what you need. All you need to know are the email account settings to send a test email.
 

RogerCooper

Registered User.
Local time
Today, 00:04
Joined
Jul 30, 2014
Messages
167
Why not try my CDO email code. The example db is standalone so you can test it quickly and see if it does what you need. All you need to know are the email account settings to send a test email.
You don't have any code for sending the e-mail. My e-mails are going out, they just are not switching the send from address.
 

isladogs

CID Moderator
Staff member
Local time
Today, 08:04
Joined
Jan 14, 2017
Messages
13,375
Yes, the folders and the inbox are there. And I can select the "ar" address manually and send the e-mail from the "ar" address.
You don't have any code for sending the e-mail. My e-mails are going out, they just are not switching the send from address.
Yes I do or I wouldn't have suggested it.
What do you think the 'Send Email' button does?
As already stated you can use that code to specify the send address which is the whole point of this thread
 

RogerCooper

Registered User.
Local time
Today, 00:04
Joined
Jul 30, 2014
Messages
167
Yes I do or I wouldn't have suggested it.
What do you think the 'Send Email' button does?
As already stated you can use that code to specify the send address which is the whole point of this thread
I will checkout CDO message objects.
 

RogerCooper

Registered User.
Local time
Today, 00:04
Joined
Jul 30, 2014
Messages
167
Why not try my CDO email code. The example db is standalone so you can test it quickly and see if it does what you need. All you need to know are the email account settings to send a test email.
When I use your program I receive the following error:

Error# -2147220960
The "SendUsing" configuration value is invalid.
 

isladogs

CID Moderator
Staff member
Local time
Today, 08:04
Joined
Jan 14, 2017
Messages
13,375
When I use your program I receive the following error:

Error# -2147220960
The "SendUsing" configuration value is invalid.
Read the help file that was supplied with my utility
It includes all the error messages/ numbers I've ever encountered INCLUDING that one!

I worked these out by deliberately doing different incorrect settings to see what message occurred. You may have to amend more than one setting to get them all correct

NOTE:
Send Using settings are 1 for local machine; 2 for network but you may need 2 even if on your own home PC.
 

isladogs

CID Moderator
Staff member
Local time
Today, 08:04
Joined
Jan 14, 2017
Messages
13,375
Are you using my utility?
If so, it's the first setting on the only form and it's fairly self explanatory.
 

RogerCooper

Registered User.
Local time
Today, 00:04
Joined
Jul 30, 2014
Messages
167
Are you using my utility?
If so, it's the first setting on the only form and it's fairly self explanatory.
I am now receiving error 2147220975. I am supplying the user name & password
Code:
Set Message = CreateObject("cdo.Message")
With Message.Configuration.Fields
    '.Item("[URL]http://schemas.microsoft.com/cdo/configuration/sendusing[/URL]") = txtSendUsing
    .Item("[URL]http://schemas.microsoft.com/cdo/configuration/sendusing[/URL]") = 2
    .Item("[URL]http://schemas.microsoft.com/cdo/configuration/smtpserverport[/URL]") = txtPort
    .Item("[URL]http://schemas.microsoft.com/cdo/configuration/smtpserver[/URL]") = txtServer
    .Item("[URL]http://schemas.microsoft.com/cdo/configuration/smtpauthenticate[/URL]") = txtAuthenticate
    .Item("[URL]http://schemas.microsoft.com/cdo/configuration/sendusername[/URL]") = "[EMAIL="ar@spectroline.com"]ar@spectroline.com[/EMAIL]"
    .Item("[URL]http://schemas.microsoft.com/cdo/configuration/sendpassword[/URL]") = "<password>"
    .Item("[URL]http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout[/URL]") = intTimeout
    .Item("[URL]http://schemas.microsoft.com/cdo/configuration/smtpusessl[/URL]") = txtSSL
    .Update
End With
Any ideas?
 

isladogs

CID Moderator
Staff member
Local time
Today, 08:04
Joined
Jan 14, 2017
Messages
13,375
If you are using my utility (or similar version in your own project), you need to use the code as supplied - not hardcode the values

If you want to hardcode your values, don't put your password in <>

There shouldn't be any hyperlinks in the code - but I expect that's the forum software interpreting (un)helpfully

The error is actually -2147220975 and once again that is covered in the Word doc that was supplied with the utility.

As it says in the help file for that error, you either have the user name or password incorrect.

Please use the help file - that's why I supplied it!
 

RogerCooper

Registered User.
Local time
Today, 00:04
Joined
Jul 30, 2014
Messages
167
I receive the same error using your form. My network administrator advised me that we are using STARTTLS for encryption. I wonder if this is underlying cause of the problem.
 

isladogs

CID Moderator
Staff member
Local time
Today, 08:04
Joined
Jan 14, 2017
Messages
13,375
Unfortunately I don't have an email account that uses STARTTLS encryption so I am unable to test that for you or offer a solution.

I did test on an IMAP account I have that uses SSL encryption to send emails and that worked fine

Suggest you research settings needed for that encryption type online
Or ask your network staff for the full settings for your account

Here's are two links to start you off:
https://www.fastmail.com/help/technical/ssltlsstarttls.html
http://codekabinett.com/rdumps.php?Lang=2&targetDoc=send-email-access-vba-cdo

The 2nd one is by Phillip Stiefel who is always an excellent reference source.The link also contains code for StartTLS but as I say I am unable to test it for you

If you find out the answer , let me know & I'll try & add it to my utility
 

RogerCooper

Registered User.
Local time
Today, 00:04
Joined
Jul 30, 2014
Messages
167
I will check that out. I may also try using SendKeys. Thanks for your help on this.
 

isladogs

CID Moderator
Staff member
Local time
Today, 08:04
Joined
Jan 14, 2017
Messages
13,375
Phil's code includes an undocumented item for use with port 587 and STARTTLS. As I said, do let me know if that works.

No idea why you mentioned SendKeys in this context.
As a general rule that isn't recommended as different versions have different keys for the same command.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 0)

Top Bottom