Get the actual recipient of an exchange email - vbscript

I have the following bit of code:
For each Item in ofChosenFolder.Items
msgbox Item.Subject
for each recip in Item.Recipients
msgbox "sent to " & recip.address
msgbox "sent to " & recip.addressEntry
next
next
I have some emails addressed to me awalker#example.com and other addressed to projects#example.com.
All are received by my exchange mailbox.
Using the above code I always get my Exchange /O=EXAMPLE/OU=EXCHANGE.../CN=RECIPIENTS/CN=A Walker, etc and my Exchange name "A Walker". This is because Exchange resolves the emails against the Global Address Book.
Is there any way to stop it resolving the email addresses and identify the actual smtp address the email was sent to?

That looks like a perfectly valid EX type address. To get the SMTP address
Check the AddressEntry.Type property. If it is "SMTP", just use the AddressEntry.Address property.
If it is "EX", use AddressEntry.GetExchangeUser.PrimarySmtpAddress
The answer is to get PR_TRANSPORT_MESSAGE_HEADERS.
To do this in VBS:
For Each Item in myNameSpace.GetDefaultFolder(olFolderInbox).items
PropName = "http://schemas.microsoft.com/mapi/proptag/0x007D001E"
Set oPA = Item.PropertyAccessor
Header = oPA.GetProperty(PropName)
'parse the "To" line out of your header to get the email address
Next

Related

How can I reply email using vbs [duplicate]

This question already has an answer here:
Outlook Reply or ReplyAll to an Email
(1 answer)
Closed 1 year ago.
I'm trying to get mailitem by Outlook.application object, and reply email by certain subject naming rule.
Now I'm able to get the mailitem, filter the mail by its subject. How can I move the next step to reply email? Thanks
Belo is my code so far:
Set objOutlook = CreateObject("Outlook.Application")
Set MailItem = objOutlook.Session.Folders("XXX#XXX.com")
For Each eMail in MailItem.Items
If InStr(1,Trim(eMail.Subject),"Production",vbTextCompare) > 0 then
wscript.echo "This is needed email, need to reply"
End If
Next
For wscript use, you can modify the example found in
Outlook Reply or ReplyAll to an Email
You pull eMail in your loop of Mail items
Use this in place of wscript.echo line
Set olReply = eMail.ReplyAll
olReply.HTMLBody = "Hello, Thank you. " & vbCrLf & olReply.HTMLBody
olReply.Display
olReply.Send

Outlook unable to handle bracket > in e-mail correctly

I have an external system feeding draft e-mail in outlook.
The e-mail address is in format:
Username <abcd#gmail.com; efgh#gmail.com>
When I preview e-mail in outbox, the outlook wrongly treated the e-mail as "Username <abcd#gmail.com" and "efgh#gmail.com>"
But, if I type the above wordings manually, Outlook 2016 check name feature can correctly recognize:
Username abcd#gmail.com; efgh#gmail.com
Any thoughts why Outlook cannot check names properly for system generated e-mail? The external system correctly transfer the string to Outlook "Username <abcd#gmail.com; efgh#gmail.com>"
Use the Recipient.Resolve method which attempts to resolve a Recipient object against the Address Book.
Sub AssignTask()
Dim myItem As Outlook.TaskItem
Dim myDelegate As Outlook.Recipient
Set MyItem = Application.CreateItem(olTaskItem)
MyItem.Assign
Set myDelegate = MyItem.Recipients.Add("Eugene Astafiev")
myDelegate.Resolve
If myDelegate.Resolved Then
myItem.Subject = "Prepare Agenda For Meeting"
myItem.DueDate = Now + 30
myItem.Display
myItem.Send
End If
End Sub
You may also find the How To: Fill TO,CC and BCC fields in Outlook programmatically article helpful.
Username <abcd#gmail.com; efgh#gmail.com> format is incorrect. It must be Username <abcd#gmail.com>; Username <efgh#gmail.com>

address error when send email with 32bit outlook using automation

NOTE: edited from original after I discovered the outlook version was 32-bit not 64-bit.
I have a legacy 32-bit VB6 program that uses outlook 2010 32bit (full version, not express) to send email. Works perfect on many machines except one machine with windows 7 (64-bit I assume). Not sure if all windows 7 machines don't work or just this one.
If I use the automation technique or the MAPI technique (as I call them, see code below) outlook sends the email but the mail server kicks it back as undeliverable saying the recipient does not exist.
Now if the automation technique is used outlook displays no UI and the email is sent in the background.
However if the MAPI technique is used outlook opens it's compose email dialog which allows the user to edit the email prior to sending. What is interesting is that the recipient email looks fine but will fail as undeliverable if sent. However if the recipient is deleted and re-typed then the email will succeed. I believe a copy and re-paste works also.
This tells me there must be one or more hidden illegal characters in the recipient email address (nulls perhaps?). The code to do this shown below is very plain and I can't think of any obvious fix. txtTo is a vb6 string with an email address and this is the field that is causing all the problems.
The error message:
Your message did not reach some or all of the intended recipients.
Subject: a test from daryls cpu #2
Sent: 11/17/2017 8:01 PM
The following recipient(s) cannot be reached:
'someemail#gmail.com' on 11/17/2017 8:01 PM
None of your e-mail accounts could send to this recipient.
Automation Technique
Dim mOutlookApp As Object
Set mOutlookApp = GetObject("", "Outlook.application")
Dim olNs As Object
Set olNs = mOutlookApp.GetNamespace("MAPI")
olNs.Logon
Dim OutMail As Object
Set OutMail = mOutlookApp.CreateItem(0)
'Set the To and Subject lines. Send the message.
With OutMail
.To = txtTo
.CC = txtCC
.Subject = txtSubjext
.HTMLBody = txtBody & vbCrLf
Dim myAttachments As Object
Set myAttachments = .Attachments
vAttach = Split(mAttachments, ",")
For i = 0 To UBound(vAttach)
myAttachments.add vAttach(i)
Next i
Dim myFolder As Object
Set myFolder = olNs.GetDefaultFolder(5) 'olFolderSent
Set .SaveSentMessageFolder = myFolder
StatusBar1.Panels(1).Text = "Status: Sending"
.send
End With
MAPI Technique
'Open up a MAPI session:
With frmMain.MAPISession1
.DownLoadMail = False
.Username = ""
.LogonUI = True
.SignOn
End With
With frmMain.MAPIMessages1
.SessionID = frmMain.MAPISession1.SessionID
.Compose
.MsgIndex = -1
.RecipIndex = 0
.RecipAddress = txtTo
.RecipDisplayName = txtTo
.RecipType = mapToList
If txtCC <> "" Then
.RecipIndex = 1
.RecipDisplayName = txtCC
.RecipAddress = txtCC
.RecipType = mapCcList
End If
'spaces are important! need one space for each attachment
'NOTE .MsgNoteText = " " MUST be there see.. KB173853 in microsoft
.MsgSubject = txtSubjext
.MsgNoteText = Space$(UBound(vAttach) + 1) & vbCrLf
.MsgNoteText = txtBody & vbCrLf
For i = 0 To UBound(vAttach)
.AttachmentIndex = i
.AttachmentPosition = i
.AttachmentType = mapData
.AttachmentName = GetFileFromPath(vAttach(i))
.AttachmentPathName = vAttach(i)
Next i
StatusBar1.Panels(1).Text = "Status: Sending"
.send True
End With
More Info:
I'm making some progress. The error has to do with email type in outlook not being SMTP. If on the send-to email in the outlook compose dialog you right-click on the email address then select outlook properties and change the email type to SMTP it will work. The type displayed is the email address itself, valid values seem to be 'mailto' and 'smtp'. So if I can set the email type from vb6 it should fix the error.
The 'Answer'? https://kb.intermedia.net/article/2344
I can't believe there is no fix for this...
RESOLVED!
I realize this topic is most likely of no interest to anyone programming in the 20th century but here is the fix:
.RecipAddress = "SMTP:" & txtTo
It just came to me. :)

Send Email Using VBScript

I want to send html table in email using the code below. It is working properly with smalls html tables. When a large html table is sent, it throws an error.
"The message could not be sent to the SMTP server. The transport error code was 0x800ccc63. The server response was 501 Syntax error - line too long"
How can I send large html table content using vbscript?
Set objMessage = CreateObject("CDO.Message")
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp server name"
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "email address"
objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
objMessage.Configuration.Fields.Update
objMessage.Subject = "subject"
objMessage.From = "from email"
objMessage.To = "to email"
objMessage.HTMLBody = "html body that contains large table"
objMessage.Send
If Err Then
rtmsg = "ERROR " & Err.Number & ": " & Err.Description
Err.Clear()
Else
rtmsg = "Message sent"
End If
MsgBox(rtmsg)
This can happen if you have too many consecutive characters without line breaks or white spaces. Some mail servers reject mails containing such content as spam (this is not exactly about vbscript, but explains what happens):
Some e-mail systems use an unsolicited commercial e-mail (UCE) filter
that may be configured to reject e-mail activities that have more than
999 consecutive characters in the body of the e-mail message. An (...)
e-mail activity may be rejected by the UCE filter in the
recipient's organization if either of the following conditions is
true:
•The message body contains more than 999 consecutive characters.
•The message contains no line feeds or carriage returns.
Note: Unsolicited commercial e-mail is also known as spam.
(http://support.microsoft.com/kb/896997/en-us)
Try adding White spaces or new lines.
When building an html table try adding an vbcrlf (new line) after a </tr> or </td> for example.

Fetch an Unread email from a particular address in outlook with VB Script

I want to get the email address of the unread mail which is from a particular sender.i tried the following code but it did'nt work
Set olApp=CreateObject("Outlook.Application")
Set olMAPI=olApp.GetNameSpace("MAPI")
Set oFolder = olMAPI.GetDefaultFolder(6)
Set allEmails = oFolder.Items
For Each email In oFolder.Items
If email.Unread = True Then
If email.SenderEmailAddress="Kalyanam.Raghuram#xxxx.com" Then
MsgBox email.Subject
End If
End If
Next
so i checked what actually 'email.SenderEmailAddress' is verifying with then by inserting this code
For Each email In oFolder.Items
If email.Unread = True Then
MsgBox email.Subject
MsgBox email.SenderEmailAddress
End If
Next
it gave me some output which cannot be understood but readable.Please let me know any solution for it.
Dio you mean you got back an EX type address instead of the expected SMTP?
Have you looked at the _ExchangeUser.PrimarySmtpAddress?
In your case you can use MailItem.Sender.GetExchangeUser.PrimarySmtpAddress. Be prepared to handle nulls as each value can be null.
The code you posted worked for me, I am on Windows Vista with Outlook 2007
One thing I would change is this
If LCase(email.SenderEmailAddress) = LCase("Kalyanam.Raghuram#xxxx.com") Then
wscript.echo email.Subject
End If

Resources