Avatar of printmedia
printmedia
 asked on

Get sender email address from msg file before it is sent vb.net

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)

Open in new window


This Outlook VBA code calls my Visual Studio application "EmailImportClient.exe". This vb.net 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 myemail@mydomain.com:

/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
        ImportEmails()
        Me.Close()
    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 = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"

        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
                    Else
Return Nothing
                    End If
                Else
Return TryCast(sender.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS), String)

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

Open in new window


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.
Visual Basic.NETMicrosoft OfficeOutlookVBAMicrosoft Visual Studio

Avatar of undefined
Last Comment
printmedia

8/22/2022 - Mon
AndyAinscow

Have you looked at the mail.Recipients of the mail message.  That should have the email adresses in it.
printmedia

ASKER
Thank you for your reply.

So you're saying the mail.Recipients will give me the smtp email address of the sender?
AndyAinscow

My understanding is that it should be there.  Check it.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ste5an

Why are you using an external application instead of doing it in Outlook? Especially as you automate Outlook to get the addresses? Outlook supports macros as the other Office applications. Thus you can do it in VBA.

But:  not all mail items in contain an SMTP address.
printmedia

ASKER
I do it in Visual Studio because it has to match the sender email to one of our contacts in our database and it was just quicker to do it there. I figured out a way by doing the following and it's worked with all of end users so far:

Dim senderaddress As String = item2.SenderEmailAddress
        Dim smtpsenderaddress As String = senderaddress.Substring(senderaddress.IndexOf("-"c) + 1) & "@mycompanydomain.com"

fromemailaddress = smtpsenderaddress

Open in new window


And now fromemailaddress has the smtp address.
ASKER CERTIFIED SOLUTION
printmedia

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.