Sending meeting invites from another email address - vba

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.

Related

Problems with changing sender field and account in emails received from specific account

I have below code which changes sender field and also changes account from which the email is being sent.
My account is authorized to send also from this email.
Code works well on new emails and also works well replied emails, but does not work well on one of the accounts I have. I have 3 accounts (POP, #outlook.com and Exchange). If email has been previously received from an #outlook.com account (this account is also set as exchange account) or set to be sent from an #outlook.com account it does not do the job. For example: If I open new email, set sending account to XX#outlook.com and then run code it does not work. Similar if I reply to an email, which I have received from xx#outlook.com account the code does not work.
Code works well if my email (replied or new) is previously set to be sent from my POP account or from my third-exchange account. The code does not work, if my email (replied or new) is previously set to be sent from the #outlook.com account.
Why is the code not working on all accounts?
Sub ChangeSender()
Dim NewMail As MailItem, oInspector As Inspector
Set oInspector = Application.ActiveInspector
If oInspector Is Nothing Then
MsgBox "No active inspector"
Else
Set NewMail = oInspector.CurrentItem
If NewMail.Sent Then
MsgBox "This is not an editable email"
Else
NewMail.SentOnBehalfOfName = "test#test.com"
Dim oAccount As Outlook.Account
For Each oAccount In Application.Session.Accounts
If oAccount.DisplayName = "accounttest#accounttest.com" Then
NewMail.SendUsingAccount = oAccount
End If
Next
NewMail.Display
End If
End If
End Sub
In case previously set account is the POP one or the third-exchange account the code works well.
In case previously set account is the xx#outlook.com account, the code does not work.
In VBA I can see the whole code runs through, but there is no change on the line >>NewMail.SendUsingAccount = oAccount as is in case when working properly.
This is my first VBA code, so maybe I am missing some basics.
EDIT:
Here is a visual image of what I am trying to do with VBA. Same think as I do with several mouse clicks and some typing: Change the email FROM field and change the account from which I am sending email from.
Image
EDIT2:
Now I have tried to remove line .SentOnBehalfOfName altogether and the script again works only on POP account and does not work on outlook.com Exchange account. In other words: I left only the line .SendUsingAccount and if email is preset to POP account it changes account correctly, if email is preset to outlook.com exchange account it does not change to the Exchange accounttest#accounttest.com account.
Any suggestion why some accounts can be changed to another one, some cannot be changed to another one?
EDIT3:
Above is image how I am sending email by hand (mouse/keyboard) with from field to test#test.com and it is being sent through my account accounttest#accounttest.com. I am doing this successfully for several months.
For the last 2 weeks I have been using partially successfully above VBA code to achieve the same. VBA code works great if before running the code my email from field is set to my POP account or my Exchange account (accounttest#accounttest.com). Above VBA code does not work in case if before running it, my email from field is set to XX#outlook.com account.
There are obviously some restrictions which account can change to which with VBA. When changing by hand, there are no restrictions.
I think SentOnBehalfOfName line is not a question any more (it is working great when I change account successfully). Maybe I should make a new question altogether without this line?
You need to set either SentOnBehalfOfName or SendUsingAccount property, but not both unless you set an SendUsingAccount to an Exchange account and SentOnBehalfOfName to a GAL user name from that Exchange account.
In the following code:
If NewMail.Sent Then
MsgBox "This is not an editable email"
Else
NewMail.SentOnBehalfOfName = "test#test.com"
Dim oAccount As Outlook.Account
For Each oAccount In Application.Session.Accounts
If oAccount.DisplayName = "accounttest#accounttest.com" Then
NewMail.SendUsingAccount = oAccount
End If
Next
NewMail.Display
End If
The SentOnBehalfOfName is set without making sure you deal with an Exchange account set for the MailItem.SendUsingAccount property. It seems the chosen account in Outlook (or the default one) doesn't have permissions for sending on behalf of another person. Just need to keep the order of setting properties.
So, let's assume the accounttest#accounttest.com account belongs to Exchange where you have sufficient permissions for sending on behalf of another person. The code should look like that then:
If NewMail.Sent Then
MsgBox "This is not an editable email"
Else
Dim oAccount As Outlook.Account
For Each oAccount In Application.Session.Accounts
If oAccount.DisplayName = "accounttest#accounttest.com" Then
NewMail.SendUsingAccount = oAccount
' when you deal with an Exchange account
NewMail.SentOnBehalfOfName = "test#test.com"
End If
Next
NewMail.Display
End If
You can't set the SentOnBehalfOfName for every account in Outlook, it does make sense only for Exchange with sufficient permissions. So, I'd suggest using the Account.AccountType property for checking the account type and differentiate cases where you can use one or the other properties.

.SendUsingAccount selection for .onmicrosoft.com custom domain account

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.

Sending email from Office 365 non-default email addresses connected to a single account

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?

Sending IBM Lotus Notes email using excel VBA sends from wrong email address

I currently have two Lotus Notes databases, each with their own email addresses associated with them. As expected, when I send an email through the first database to my gmail account, it shows up as "From: notesAccount1#db1.com" and when I send using the second database, the message shows up as "From: notesAccount2#db2.com" in my gmail.
I have working code that opens up the second database, searches the inbox for an email containing a certain keyword, and extracts an email address from that email. It then creates a new document in that second database, inserts the recipients name, subject, body, etc., sends the email, and saves it to my sent folder. Everything works smoothly up to this point.
However, when I send an email from the second database to my gmail account using this VBA method, the email shows up in my gmail as "From: notesAccount1#db1.com" - the email associated with the first database.
I can't figure out why this is happening. Pretty limited knowledge of the interactions between VBA and Lotus Notes, and Lotus Notes servers/databases in general. The first database is technically my default one that only I have access to and the second database was added later and multiple people have access to it. I don't know if that's relevant.
Would appreciate any help! Thanks.
Note: This code was copied and adapted from several sources, including some on SO, IBM and other Notes sources, and anything else Google threw my way, including
http://www.fabalou.com/vbandvba/lotusnotesmail.asp
http://www-01.ibm.com/support/docview.wss?uid=swg21178583
Code: (This will have to be adapted as I have taken out server names and mail file names)
Sub ReadNotesEmail()
Dim sess As Object
Dim db As Object
Dim folder As Object
Dim docNext As Object
Dim memoSenders As Variant
Dim newEmail As Object
Dim view As Object
Dim entry As Object
Dim entries As Object
Dim templateEmail As Object
Dim mailServer As String
Dim mailFile As String
Dim folderName As String
Dim todayDate As String
Dim memoBody As String
Dim senderEmail As String
Dim emailStartPos As Integer
Dim emailEndPos As Integer
'This program will search a particular folder in a Notes database that I designate.
'It will search that folder for emails that contain certain key words. Once it finds
'an email that fits, it will grab the sender's email address (written in the body, not
'in the 'from') and send them an email.
'Name of folder to search for emails
folderName = "($Inbox)"
'Create a Lotus Notes session and open it (will require password)
Set sess = CreateObject("Lotus.NotesSession")
sess.Initialize ("")
'Set the mail server, mail file, and database. This will be the tricky part as I need this to
'look at the second mail server as opposed to the default mail server of jdyagoda
Set db = sess.GETDATABASE("***name of second Notes server***", "***name of second mail file***")
'Open the mail database in notes
If Not db.IsOpen = True Then
Call db.Open
End If
Set folder = db.GetView(folderName)
'Now look through the emails one at a time with a loop that ends when no emails are left.
'If an email contains the key word, look for the email address of the person who submitted
'the contact-us form. It follows the string "Email:" and preceeds
'the string "Phone:".
Set doc = folder.GetFirstDocument
Do Until doc Is Nothing
Set docNext = folder.GETNEXTDOCUMENT(doc)
memoBody = LCase(doc.GetItemValue("body")(0))
If (memoBody Like "*") Then 'This is where you designate the keyword
'Here's where you extract the email address - taken out for the purpose of this SO question
'senderEmail = testName#test.com
'Now create a new email to the intended recipient
Set newEmail = db.CREATEDOCUMENT
Call newEmail.ReplaceItemValue("Form", "Memo")
Call newEmail.ReplaceItemValue("SendTo", senderEmail)
Call newEmail.ReplaceItemValue("Subject", "Thank you for your email")
Call newEmail.ReplaceItemValue("body", "Test Body 1. This is a test.")
newEmail.SAVEMESSAGEONSEND = True
'Send the new email
Call newEmail.ReplaceItemValue("PostedDate", Now()) 'Gets the mail to appeaer in the sent items folder
Call newEmail.SEND(False)
End If
Set doc = docNext
Loop
End Sub
Notes will normally send the mail using the email address for the ID you use to login. So if you login using notesAccount1/Domain, then all emails will be coming from notesAccount1#example.com.
If you want to fake the sender, you need to use an undocumented method: inject the email directly into mail.box.
You should not attempt to do this unless you really know what you are doing.
I have posted code on my blog for a mail notification class, it supports this work-around to set the sender on outgoing emails. You can find the latest code at http://blog.texasswede.com/updated-mailnotification-class-now-with-html-email-support-and-web-links/

Filter sent items based on the recipients email address in Outlook

I am working on an activity facility which stores incoming and outgoing emails in the database for different contacts. I do this by looping through each folder in my namespace and restricting emails based on the sender email address.
I have managed to store incoming emails so far, but outgoing emails are completely ignored for some reason. I am assuming I am doing the "Mailtiems.Restrict" incorrectly, however I could not figure out what it may be. Please see the code below:
If folder.Name = outlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Name Then
outlookItems = folder.Items
outlookItems = outlookItems.Restrict("[SenderEmailAddress] = " & Quote(txtContactPersonalEmailAddress.Text) & " AND [MessageClass] = 'IPM.Note'")
' Count total folder items for progress bar
iOutlookFolderTotalMailItems = outlookItems.Count
The count always returns 0 even though I have sent an email to this contact and it appears in my "Sent Items" folder. I wonder if there is a different property I will need to use for "Sent Items" other than "SenderEmailAddress".
If you are searching for the recipients rather than the sender, you should not be specifying SenderEmailAddress - the sender will always be you.
See Filter sent items outlook by address in Excel VBA