troubleshooting Question

Get sender email address from msg file before it is sent

Avatar of printmedia
printmedia asked on
Visual Basic.NETMicrosoft OfficeOutlookVBAMicrosoft Visual Studio
6 Comments1 Solution343 ViewsLast Modified:
Hi all.

I'm running into an issue getting the sender SMTP email address (we use Office 365) from the .msg file. The .msg file is saved in the ItemSend event in Outlook (which fires right before the email is sent)

Outlook VBA Code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

Dim outEmail As Outlook.MailItem

Dim savePath As String

 If Item.Class = OlObjectClass.olMail Then
 savePath = "c:\temp\test.msg"
            Set outEmail = Item
            outEmail.SaveAs savePath, olMsg
End If

Dim strProgramName As String
strProgramName = "C:\myProgram\EmailImportClient.exe"
Call Shell("""" & strProgramName & """", vbNormalFocus)

This Outlook VBA code calls my Visual Studio application "EmailImportClient.exe". This application gets the .msg file and brings in the From, To, CC's, email body etc into a SQL table. The problem is when it runs the GetSenderSMTPAddress function it returns blank, and I'm thinking it's because the .msg file gets created in the ItemSend event in Outlook which fires right before the email is sent.

I tried using  fromemailaddress = GetSenderSMTPAddress(item2) but that returns the following instead of

/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=25e8ae1f87684c7b97abe0ed743a5219-myemail

Imports System.IO
Imports Microsoft.Office
Imports Outlook = Microsoft.Office.Interop.Outlook

Public Class EmailImport

    Public fromemailaddress As String

    Private Sub EmailImport_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    End Sub

    Private Sub ImportEmails()
 Dim OL As Outlook.Application = CreateObject("Outlook.Application")
Dim item2 As Outlook.MailItem = CType(OL.CreateItemFromTemplate("c:\temp\test.msg", Type.Missing), Outlook.MailItem)
fromemailaddress = GetSenderSMTPAddress(item2)
End Sub

Private Function GetSenderSMTPAddress(mail As Outlook.MailItem) As String
Const PR_SMTP_ADDRESS As String = ""

        If mail Is Nothing Then
Throw New ArgumentNullException()
        End If
        If mail.SenderEmailType = "EX" Then
Dim sender As Outlook.AddressEntry = mail.Sender

            If sender IsNot Nothing Then
If sender.AddressEntryUserType = Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry OrElse sender.AddressEntryUserType = Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry Then
 Dim exchUser As Outlook.ExchangeUser = sender.GetExchangeUser()
                    If exchUser IsNot Nothing Then
 Return exchUser.PrimarySmtpAddress
Return Nothing
                    End If
Return TryCast(sender.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS), String)

                End If
 Return Nothing
            End If
Return mail.SenderEmailAddress
        End If
    End Function

Any idea how I can get the sender email from the .msg file since it was created before the email is actually sent?

Thank you in advance.

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros