How to change sender name in Outlook ? - vba

I am sending an email using Outlook object from a subroutine vba
The email gets send from my email and the recipients see : myemail#xxx.com . Is there any way I can make those recipients get an email that would have MyfirstName MylastName instead of my email
Sub Mail_Workbook_1()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.From = "MyfirstName MylastName" 'something like this
.To = "ron#debruin.nl"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = "Hello World!"
....
Set OutMail = Nothing
Set OutApp = Nothing
End Sub

If you are sending through Exchange on behalf of another mailbox, set the MailItem.SentOnBehalfOfName property (assuming you have sufficient privileges)
If you are sending through a particular SMTP account, set the MailItem.SendUsingAccount property.
If you need to send as an arbitrary SMTP user, see this example on my website - you will essentially need to set the "From" named MAPI property in the PS_INTERNET_HEADERS namespace. Note that not all SMTP servers will let you do that - Exchange for one will not let you spoof the sender.
If you want to send as one of the alias (proxy) SMTP addresses belonging to a particular Exchange mailbox, you will need to send through SMTP - sending through OOM or MAPI will always send with the default SMTP address of the mailbox. For an end user, you can configure a dummy POP3/SMTP account or use a product like Proxy Manager (I am its author). See MSOutlook.info for more information.

Related

Forward an email received in shared mailbox, setting FROM to a specific user account

I receive invoices from suppliers that I want to forward to a QBO (Quickbooks Online) email address that handles 'receipts/invoices' wherein it reads the attachments and parses the info within - this speeds up data entry.
QBO only accepts these emails from specific email addresses (i.e., ones that are registered as accounts in QBO). So assume that QBO only accepts emails from "me#mydomain.com". I receive the invoices on "billing#mydomain.com" which is a shared Office 365 mailbox that "me#mydomain.com" has access to.
My VBA code should forward the currently selected emails (found within the billing#mydomain.com mailbox) using the me#mydomain.com sending address to mydomain#qbodocs.com.
The problem is that the forwarded email arrives in the recipient's mailbox as having come from billing#mydomain.com. When I .Display (instead of .Send), I see that the sending account is set "correctly" yet it still arrives from the wrong account.
I decided that after the window pops up (using .Display), I would change the sending account to something else, then back to the intended sending account - and it works. So there's some setting/headers other than .SentOnBehalfOfName that I need to set as I don't want any user intervention.
Option Explicit
Public Sub SendToQBO()
Dim Email As Object
Dim Sender As String
Sender = "me#mydomain.com"
For Each Email In ActiveExplorer.Selection
With Email.Forward
' Just send to myself for now until this is figured out
.To = Sender
'.To = "mydomain#qbodocs.com"
.Subject = "Sent From Outlook"
.Body = Email.Body
.SendUsingAccount = Session.Accounts(Sender)
.SentOnBehalfOfName = Sender
.Send
' Using .Display instead shows the right sending address, but it's ineffective
' unless I select another, then select it again before manually sending.
' .Display
End With
Next
End Sub
There is no need to set up two properties simultaneously on the mail item.
The MailItem.SendUsingAccount returns or sets an Account object that represents the account under which the MailItem is to be sent. The SendUsingAccount property can be used to specify the account that should be used to send the MailItem when the Send method is called. Note, corresponding account should be configured in Outlook:
Sub SendUsingAccount()
Dim oAccount As Outlook.account
For Each oAccount In Application.Session.Accounts
If oAccount.AccountType = olPop3 Then
Dim oMail As Outlook.MailItem
Set oMail = Application.CreateItem(olMailItem)
oMail.Subject = "Sent using POP3 Account"
oMail.Recipients.Add ("someone#example.com")
oMail.Recipients.ResolveAll
Set oMail.SendUsingAccount = oAccount
oMail.Send
End If
Next
End Sub
In case given permissions to the shared account you can use the MailItem.SentOnBehalfOfName property which allows setting a string indicating the display name for the intended sender of the mail message. In that case the account may not be configured in Outlook.
And the last bit which I've noticed in your code:
For Each Email In ActiveExplorer.Selection
Remember, the selection object may contain different kind of Outlook items - appointments, posts, mail items and etc. So, I'd suggest adding a check for the item type before accessing MailItem methods and properties.

New Outlook Mail Item forgetting Categories

I have the following code but the email that is created doesn't have any categories set. Ever.
Private Sub Application_Reminder(ByVal Item As Object)
.
.
.
Dim objMail As Outlook.mailItem
Set objMail = Application.CreateItem(olMailItem)
With objMail
.BodyFormat = olFormatHTML
.To = toContent
.CC = ccContent
.HTMLBody = messageContent
.Categories = Item.Categories
.Subject = Item.Subject
.Send
End With
.
.
.
End Sub
The Item object is a Task object that has a reminder set. I'm trapping the reminder in the Application_Reminder sub and generating an email from it. All properties get copied from the task to the email. At run time I can but a breakpoint on .Send and see that the .Categories property of the email is set correctly. When the email is received, that has been reset and is blank. The categories that I'm using are the standard Outlook ones.
To avoid releasing potentially private information on outgoing email messages, categories are not sent with email in Outlook when you use Exchange server mailboxes. When you use categories with internal codes or potentially embarrassing keywords, the recipient will not see them.
The Category is removed by Exchange Server's transport rules, not Outlook, when the message is sent. You may check out the item placed to the Sent Items folder.
If you need to send categories on outgoing email you can use the SendPersonalCategories registry entry.
HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Outlook\Preferences
DWORD: SendPersonalCategories
Value Data: 1 to keep categories on sent mail, 0 to not include categories
Where 16.0 stands for the Outlook version (2016).
Read more about that in the Sending Categories on Email Messages article.

How to set Outlook From email address from Word VBA

I'm trying to use Word VBA to send a document to an email recipient. For the most part, it is not difficult. I have this code so far:
With oItem
'Set the recipient for the new email
.To = "person1#mail.com"
'Set the recipient for a copy
.CC = "ccperson#mail.com"
'Set the subject
.Subject = "Blah blah"
End With
My problem is that I have several sender email addresses configured in Outlook, and Outlook is picking the wrong one by default.
Is there a way to specify a sender email address using the method above? Needless to say, the intuitive code line for specifying a sender address (.From = me#wherever.com) does not work. Thank you.
UPDATE:
I finally got my code to work after modifying it using the suggestions from peakpeak and Dimitry below. My changes were
1) to include a reference to the Microsoft Outlook 16 object library so that I could get access to the Outlook.MailItem datatype. The mail would send fine with the code above (without the reference), but would always send with the wrong From address.
2) Declare the mail item as Outlook.MailItem. This seemed to enable the SentOnBehalfOfName field.
3) Used my desired From: email address in the SentOnBehalfOfName field.
Here is the working code:
Dim MAPIMailItem As Outlook.MailItem
Set MAPIMailItem = olkApp.CreateItem(olMailItem) 'Create a new mail message
With MAPIMailItem
.BodyFormat = olFormatPlain
.to = strTo
' SentOnBehalfOfName sets the From field on my machine,
' AFTER I declared MAPIMailItem as Outlook.MailItem
.SentOnBehalfOfName = "fromAddress#foo.com"
.Subject = strSubject
.body = strBody
.attachments.Add strAtt
'.send
.Display
End With
I use this code:
Dim WantedAccount as String ' Set to preferred account name
Set MAPISession = objOutlook.Application.Session 'Get the MAPI Outlook session
Set MAPIMailItem = objOutlook.CreateItem(olMailItem) 'Create a new mail message
With MAPIMailItem
For Each Account In MAPISession.Accounts
If Account = WantedAccount Then
.SendUsingAccount = Account
Exit For
End If
Next
If you are sending through an Exchange account, set the MailItem.SentOnBehalfOfName property (assuming you have the right to send on behalf of the specified mailbox). If you are sending through a POP3/SMTP account, set the MailItem.SendUsingAccount property.

E-Mail body is lost when sending (outlook vba)

I'm trying to write a macro that sends an automatic notification to specific addresses before sending the original email. (Like a cc, without actually using cc.)
The content of the original formatted email, (including text, tables, and pictures,) should be copied and pasted into a new email which is then automatically sent. Everything works when I just display the message, but not when actually sending the email.
Here is my code:
Dim objMsg As Outlook.MailItem
Dim activeMailMessage As Outlook.MailItem
Dim BodyText As Object
' Create the message.
Set objMsg = Application.CreateItem(olMailItem)
'copy body of current item
Set activeMailMessage = ActiveInspector.CurrentItem
activeMailMessage.GetInspector().WordEditor.Range.FormattedText.Copy
'paste body into new email
Set BodyText = objMsg.GetInspector.WordEditor.Range
BodyText.Paste
'set up and send notification email
With objMsg
.To = "test#domain.com"
.Subject = "text"
.Send
End With
The text should be pasted into the body like this, but it won't paste:
With objMsg
.To = "test#domain.com"
.Subject = "test"
.body = bodytext.paste
.Send
End With
When I use .display the correct content is displayed. But when I send it directly (without first using .display), all of all information is lost and an empty email is sent. What can I do?
I could add a bcc in the original email to achieve the same result, but the original email does not always send, whereas this notification should be.
Your code is never actually setting the Body of the e-mail in the objMsg object. It is working when you have objMsg displayed because your interacting with the 'Inspector'.
If you directly set either the HTMLBody (if you want to retain formatting), or the Body property on objMsg then it will work as in the below example.
With objMsg
.HTMLBody = activeMailMessage.HTMLBody
.To = "test#domain.com"
.Subject = "text"
.Send
End With
Bob, regarding your question on images that are embedded within the e-mail being lost with the above approach. An alternate solution could be to use the MailItem's Copy method to create your new MailItem exactly as the original Item. This will also retain who the e-mail is being sent to you need to clear this to make sure only the intended recipients receive it.
Dim objMsg As Outlook.MailItem
Dim activeMailMessage As Outlook.MailItem
' Create the new message.
Set objMsg = Application.CreateItem(olMailItem)
' Assign the current item to activeMailMessage
Set activeMailMessage = ActiveInspector.CurrentItem
' Copy the current item to create a new message
Set objMsg = activeMailMessage.Copy
' Clear any existing recipients of the e-mail, as these will be retained in the Copy
While objMsg.Recipients.Count > 0
objMsg.Recipients.Remove 1
Wend
'set up and send notification email
With objMsg
.To = "test#domain.com"
.Subject = "text"
.Send
End With
This should retain your images and other attachments as they were in the original e-mail.
Try to call the Save method after calling the Paste method.

How to send mail without asking a sender account for permission?

I'm trying to make a VB.NET app that sends a mail to a certain email account, that account's domain is not gmail,hotmail,etc. it belongs to the company where I work.
My problem is, Outlook prompts a warning that says "a program is attempting to send email on your behalf..." and it requires a click to send the mail(that's because it is using the current PC user account to send the mail). I need to make it automatized(no clicks required)
Since this is the first time I do this kind of thing, I must ask if there is a way to send the mail without a sender account? So I don't need to ask for permission to send the mail nor write any password in the code. Or if it is not possible at least a way to make my .exe "trusted" for any PC inside the company where it is executed.
What lines do I have to change in my code to skip that message?
Dim OutApp As Object
Dim OutMail As Object
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim date As String
Dim proc As System.Diagnostics.Process
For Each proc In System.Diagnostics.Process.GetProcessesByName("EXCEL")
If proc.MainWindowTitle.Trim.Length = 0 Then
proc.Kill()
End If
Next
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Open("\\file.xlsm")
xlWorkSheet = xlWorkBook.Worksheets("sheet")
Try
xlWorkSheet.Range("E4").Select()
xlWorkSheet.Range("E4").Activate()
date = xlApp.ActiveCell.Value
Do While Convert.ToString(xlApp.ActiveCell.Value) <> ""
'The mail part start here
If (DateTime.Today - CDate(date)).Days = 180 Then
OutApp = CreateObject("Outlook.Application")
OutMail = OutApp.CreateItem(0)
'On Error Resume Next
With OutMail
.To = "xxxx#xxxx.com"
.CC = ""
.BCC = ""
.Subject = "subject"
.Body = "text"
.Send()
End With
' On Error GoTo 0
OutMail = Nothing
OutApp = Nothing
End If
xlApp.ActiveCell.Offset(1, 0).Select()
date = xlApp.ActiveCell.Value
Loop
Catch ex As Exception
MsgBox(ex.ToString())
End Try
This is a console app, so there is no web.config here.
Please help.
You can try third party email service providers like Amazon SES, SendGrid, etc. Then using SMTP or APIs provided by them you can send mail from any email address but note that you need to authorize the email address just one time.
They generally send an email with a link to an email address and you just need to click on it to authorize that address.