I set up a custom domain for my Microsoft 365 business account ("#company.com" is the custom domain which is really "#company.onmicrosoft.com").
I have two other accounts, which go through other mail servers like "#yahoo.com", "#gmail.com".
I created a VBA sub to send email and use .SendUsingAccount to send with different accounts as needed.
The function works for the "#yahoo.com", "#gmail.com" accounts but when I select my "#Company.com" (default) account, which is basically a "#company.onmicrosoft.com" account, it selects the immediately following "#yahoo.com" account.
Here's the code:
For i = 1 To OlApp.Session.Accounts.Count
If OlApp.Session.Accounts.Item(i).SmtpAddress = "person#company.onmicrosoft.com" Then AccNo = i
' Debug.Print "Acc name: " & OlApp.Session.Accounts.Item(i) & " Acc number: " & i & " , email: " & OlApp.Session.Accounts.Item(i).SmtpAddress
Next i
Set objOlAccount = OlApp.Session.Accounts.Item(i)
Set OlMail = OlApp.CreateItem(olMailItem)
Set OlMail.SendUsingAccount = objOlAccount
When I run Debug.Print I see the "person#company.onmicrosoft.com account for i=1, the "#yahoo.com" account for i=2, "#Gmail.com" account for i=3.
I went as far as forcing the selection: Set objOlAccount = OlApp.Session.Accounts.Item(1), but my "#yahoo.com" account is still used to send the email.
How can I set the send account to my "#company.com" ("#company.onmicrosft.com") account?
Set objOlAccount = OlApp.Session.Accounts.Item(i)
should be
Set objOlAccount = OlApp.Session.Accounts.Item(AccNo)
Might be better as:
For i = 1 To OlApp.Session.Accounts.Count
If OlApp.Session.Accounts.Item(i).SmtpAddress = "person#company.onmicrosoft.com" Then
Set objOlAccount = OlApp.Session.Accounts.Item(i)
Exit For
End If
Next i
If Not objOlAccount Is Nothing then
Set OlMail = OlApp.CreateItem(olMailItem)
Set OlMail.SendUsingAccount = objOlAccount
End If
When I added my person#company.com account I used IMAP settings. Microsoft exchange server added a second person#company.com account, through exchange, without me realizing I had two such accounts. Once I removed the person#company.com account I used IMAP settings, all was sorted.
If you are sending through an Exchange account in Outlook, it always uses the default SMTP address of that account.
To work around that, you'd need to send using straight SMTP (it preserves the right sender address if you are using one of the proxy addresses). On the end user level, you can use a utility like Proxy Manager (I am its author).
If it is just a matter of "#company.com" vs "#company.onmicrosoft.com", you need to set your "#company.com" address as the default one in the Exchange admin console.
Related
I am using tool coded in VBA to send meeting invites in bunch. meeting invite
When I use an email address, that I added to Outlook first, everything works.
When I use another email address that I added to Outlook after it, I get this message:
"Sending account 'invitation#gmail.com' not found."
I paste the email address, invitation#gmail.com, from which I want to send meeting invitations in sh.cell(1,2).
The part of the code that is giving me the message:
Dim currentAccount As Outlook.Account
Dim acc As Outlook.Account
For Each currentAccount In AppOutlook.Session.Accounts
If currentAccount.SmtpAddress = sht.Cells(1, 2) Then
Set acc = currentAccount
FoundAccount = True
Exit For
End If
Next
If Not FoundAccount Then
MsgBox "Sending account '" & sht.Cells(1, 2) & "' not found."
Exit Sub
End If
How can I set up invitation#gmail.com as my current account?
I can provide the rest of the code, but my assumption is that I have to set up something in Outlook.
Your code indicates the email address is not an account.
Outlook documentation typically refers to email addresses / mailboxes as accounts.
Outlook VBA requires accounts to be added like this:
Add an email account to Outlook.
Similar problem to "Resca's" Utility to send email to Gmail stopped working after google implemented OAuth. I have an excel workbook using VBA to ONLY SEND individual gmails to multiple “google group” members. It had been working for 4 years until Gmail added Oauth.
I have done tests with OAuth code from "Email Architect". I can test the app in test mode and it will send the email if I click passed the alarms and add include read. Compose, send and delete permissions. When I configure it as production I get not verified message and go through a similar routine that "test" needed. When I try to verify the Gmail API it requires a domain which I don't have. This is run under a single user xxx#gmail with owner permission from my disk. After seeing Resca's post, I think I may be over doing the OAuth.
I am not a programmer, but have vintage experience with assembler, macro and micro code, but not high level languages. Self taught VBA/Excel
I got parts of this code from "Jean-François Corbett" # https://stackoverflow.com Tonyyy at MRexcel.com
Set CDO_Mail = CreateObject("CDO.Message")
On Error GoTo Error_Handling
Set CDO_Config = CreateObject("CDO.Configuration")
CDO_Config.Load -1
Set SMTP_Config = CDO_Config.Fields
With SMTP_Config
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxxxx#gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxx"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
'25 & 465 are valid ports and sometimes may fail. Change to the other if you can't connect
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Update
End With
With CDO_Mail
Set .Configuration = CDO_Config
End With
CDO_Mail.Subject = strSubject
CDO_Mail.From = strFrom
CDO_Mail.To = strTo
CDO_Mail.TextBody = strBody
CDO_Mail.CC = strCc
CDO_Mail.BCC = strBcc
CDO_Mail.Send
To be clear Google did not add Oauth2. The option of using Xoauth2 with the smtp server has always been there. Nor is google forcing you to use Xoauth2 with the smtp server.
What google did was remove the option for Less secure apps & your Google Account. Which allowed you to use the google account password to connect to the smtp server.
If you have 2fa enabled on the google account you can create an apps password and use that password instead of the actual password for the google account in your code.
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "AppsPassword"
If you do not want to use an apps password then yes you will need to configure Xoauth2. xoauth2-protocol Its been a very long time since i have touched VBA I dont think this is something i can help with. But a quick google search shows it should be supported Oauth
VBA created emails are sent successfully, however I can only send from the email address of the default account.
As I have a few Office 365 connected email addresses, in Outlook 2016 I am able to manually select the 'FROM' out of the list of available connected email addresses.
In Outlook VBA, there is only one account (the exchange of Office 365), so using SendUsingAccount will find the one account.
SentOnBehalfOfName bounces on security issues. Is it possible to define the 'FROM' email address?
Yes, you can, but you need to create the different accounts in Outlook. When done, use this code excerpt:
Dim SendAccount As String
SendAccount = "MyDesiredAccountName"
Set MAPISession = objOutlook.Application.Session
Set MAPIMailItem = objOutlook.CreateItem(olMailItem) 'Create a new mail message
'**********************************************
'* Find desired from-account
'* If not found, use default
'*
For Each Account In MAPISession.Accounts
If Account = SendAccount Then
MAPIMailItem.SendUsingAccount = Account
Exit For
End If
Next
Is this enough to get you started?
I created a makro in excel to send all TODOs to the responsible people. Now I want to add the sender address into the CC. I know how to set the CC but I don't know how to get the current sender address.
Set session = CreateObject("Notes.NotesSession")
Set db = session.GETDATABASE("", "")
Call db.OPENMAIL
Set doc = db.CREATEDOCUMENT
Call doc.REPLACEITEMVALUE("CopyTo", strEmail)
I think it should work with the notes session, but I didn't find any method for this.
You can just use NotesSession.UserName(). This is Notes mail you are sending. You don't need a full SMTP-style address with the # and the DNS domain name. You can just put the user's Notes username in an addressing field and the Domino router will do the lookup and it will just work.
The above is true as long as (a) the server that you have established the session with is either the user's home mail server, a member of the same Notes domain (which is not the same thing as a DNS domain), or a member of a Notes domain that includes the user's Notes domain as part of its Directory Assistance (or its cascading address book list if it's using 20-year-old configurations), and (b) the username is unique within the above scope.
another suggestion, copy sender from last sent mail, to test
Set view = db.GetView("(($Sent))")
Set sentdoc = View.GetLastDocument
sender=sentdoc.getItemValue("From")
The way I automated Lotus Notes and sending emails was using this site below:
Send files using Lotus Notes
The area you want to pay attention to is at the bottom, which takes "noDocument" and adds the relevant titles "Subject", "to", "Sendto" etc.
'Add values to the created e-mail main properties.
With noDocument
.Form = "Memo"
.SendTo = vaRecipients
.CopyTo = vaCopyTo
.Subject = stSubject
.Body = vaMsg
.SaveMessageOnSend = True
.PostedDate = Now()
.Send 0, vaRecipients
End With
Use NotesSession.userName() to get the current username. If you really want the full email address you might also be able use #namelookup formula.
However, I would stay away from accessing notes via COM as it does not work on 64bit and IBM couldn't care less about it. I had several excel files which used this handy technique, but they are all broken since we moved to 64bit. Check this old kb https://www-304.ibm.com/support/docview.wss?uid=swg21454291
I am developing a mail macro that a number of users will run in Outlook. Currently I use the following approach to search my Inbox...
Set objNamespace = Application.GetNamespace("MAPI")
'Replace line below with code that sets olShareName equal to the current 'outlook user's primary e-mail account.
Set olShareName = objNamespace.CreateRecipient("rpullman#dogs.com")
Set objFolder = objNamespace.GetSharedDefaultFolder(olShareName, olFolderInbox)
Set DestFolder = objNamespace.GetSharedDefaultFolder(olShareName, olFolderToDo)
strFilter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '%Dogs Secretly Control the World%'"
Set filteredItems = objFolder.Items.Restrict(strFilter)
I want to make this code portable in the sense that the olShareName variable will be set to whomever is running the macro's primary e-mail address.
If, for instance, my colleagues were to use this macro, and they had the following e-mail addresses (that are accessed through outlook): bailey#dogs.com and molly#dogs.com, they should be able to run it without having to key in any modification to the code.
Obviously, I could use an input-box and have them type in their e-mail address, but I want to avoid that.
Use the NameSpace.CurrentUser property to get a Recipient object for the current user. This will return the address associated with the default account in the active Outlook profile. If this is an Exchange account you will need to access Recipient.AddressEntry.GetExchangeUser() to get ExchangeUser.PrimarySMTPAddress. Although you really only need the Recipient object to use as the parameter for NameSpace.GetSharedDefaultFolder.
If you need the addresses of that user's aliases then I believe you need to use MAPI directly (or Redemption). But if you want to access the email address of the other accounts configured in the profile then you can loop through NameSpace.Accounts and check Account.SmtpAddress.