retrieving to address from MailItem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Since I dont think I can access the message source from a MailItem object, I wish to recreate the headers manually, but I cannot find the to email address stored in the MailItem. Is there a way I can retrieve the to address, not name, from a MailItem object?
 
You can get it by iterating the MailItem.Recipients and checking the Type
for each Recipient to determine if it's a To or Cc.
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



olek lorenc said:
Since I dont think I can access the message source from a MailItem object,
I wish to recreate the headers manually, but I cannot find the to email
address stored in the MailItem. Is there a way I can retrieve the to
address, not name, from a MailItem object?
 
I am still unable to get the To email address. If the To reciept is "Olek Lorenc <[email protected]>", I can only get the name, "Olek Lorenc", not the actual address

----- Sue Mosher [MVP-Outlook] wrote: ----

You can get it by iterating the MailItem.Recipients and checking the Typ
for each Recipient to determine if it's a To or Cc
--
Sue Mosher, Outlook MV
Author o
Microsoft Outlook Programming - Jumpstart fo
Administrators, Power Users, and Developer
http://www.outlookcode.com/jumpstart.asp


olek lorenc said:
Since I dont think I can access the message source from a MailItem object
I wish to recreate the headers manually, but I cannot find the to emai
address stored in the MailItem. Is there a way I can retrieve the t
address, not name, from a MailItem object
 
Show your code, please.

Olek Lorenc said:
I am still unable to get the To email address. If the To reciept is "Olek
Lorenc said:
----- Sue Mosher [MVP-Outlook] wrote: -----

You can get it by iterating the MailItem.Recipients and checking the Type
for each Recipient to determine if it's a To or Cc.
 
I'm sorry, I was able to figure it out after looking more into the recipients structure. Only problem I have left is retrieving the information stored in the internet headers, such as the tracing information and X-Originating-IP. But I dont know if its even possible to get that. This is my code so far:

Sub Abuse()
result = MsgBox("Is this email abuse?", vbYesNo)
If result = vbYes Then
myAbuseMail = Application.ActiveWindow
myAbuseAddress = myAbuseMail.SenderEmailAddress
myAbuseAddress = "abuse@" + Right(myAbuseAddress, Len(myAbuseAddress) - InStr(myAbuseAddress, "@"))

Dim gHeader, gTo, gCC, gBCC As String
gHeader = ""
gTo = "To: "
gCC = "CC: "
gBCC = "BCC: "

gHeader = gHeader & "From: " & myAbuseMail.SenderName & " <" & myAbuseMail.SenderEmailAddress & ">" & vbCrLf
If myAbuseMail.ReplyRecipientNames <> "" Then gHeader = "Reply-To: " & myAbuseMail.ReplyRecipientNames & vbCrLf
For i = 1 To myAbuseMail.Recipients.Count
Select Case myAbuseMail.Recipients.Item(i).Type
Case olTo
gTo = gTo & myAbuseMail.Recipients.Item(i).Name & " <" & myAbuseMail.Recipients.Item(i).Address & ">; "
Case olCC
gCC = gCC & myAbuseMail.Recipients.Item(i).Name & " <" & myAbuseMail.Recipients.Item(i).Address & ">; "
Case olBCC
gBCC = gBCC & myAbuseMail.Recipients.Item(i).Name & " <" & myAbuseMail.Recipients.Item(i).Address & ">; "
End Select
Next i
If gTo <> "To: " Then gHeader = gHeader & Left(gTo, Len(gTo) - 2) & vbCrLf
If gCC <> "CC: " Then gHeader = gHeader & Left(gCC, Len(gCC) - 2) & vbCrLf
If gBCC <> "BCC: " Then gHeader = gHeader & Left(gBCC, Len(gBCC) - 2) & vbCrLf
gHeader = gHeader & "Subject: " & myAbuseMail.Subject & vbCrLf
gHeader = gHeader & "Date: " & FormatDateTime(myAbuseMail.SentOn, vbLongDate) & " " & FormatDateTime(myAbuseMail.SentOn, vbLongTime) & vbCrLf
If myAbuseMail.BodyFormat = olFormatHTML Then
gHeader = gHeader & vbCrLf & "Content-Type: text/html;" & vbCrLf & vbCrLf
gHeader = gHeader & myAbuseMail.HTMLBody
Else
gHeader = gHeader & vbCrLf & "Content-Type: text/plain;" & vbCrLf & vbCrLf
gHeader = gHeader & myAbuseMail.Body
End If

Set myMail = Application.CreateItem(olMailItem)
myMail.BodyFormat = olFormatPlain
myMail.To = myAbuseAddress
myMail.Subject = "email abuse"
myMail.Body = gHeader
myMail.Display
End If
End Sub
 
I forgot to mention that this function is run from an open mail window which has a button that runs the Sub Abuse. Thats the reason for using Application.ActiveWindow, I couldnt figure out a simpler way of using the currently selected email.
 
Application.ActiveInspector.CurrentItem
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Olek Lorenc said:
I forgot to mention that this function is run from an open mail window
which has a button that runs the Sub Abuse. Thats the reason for using
Application.ActiveWindow, I couldnt figure out a simpler way of using the
currently selected email.
 
Headers must be retrieved with CDO or, to avoid security prompts,
Redemption, not with Outlook objects. See
http://www.outlookcode.com/d/forms/headers.htm for a sample form that
demonstrates the use of the Fields collection to do this.
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Olek Lorenc said:
I'm sorry, I was able to figure it out after looking more into the
recipients structure. Only problem I have left is retrieving the information
stored in the internet headers, such as the tracing information and
X-Originating-IP.
 
Back
Top