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.MailItemDim savePath As String If Item.Class = OlObjectClass.olMail Then savePath = "c:\temp\test.msg" Set outEmail = Item outEmail.SaveAs savePath, olMsgEnd IfDim strProgramName As StringstrProgramName = "C:\myProgram\EmailImportClient.exe"Call Shell("""" & strProgramName & """", vbNormalFocus)
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.IOImports Microsoft.OfficeImports Outlook = Microsoft.Office.Interop.OutlookPublic 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 SubPrivate Function GetSenderSMTPAddress(mail As Outlook.MailItem) As StringConst PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E" If mail Is Nothing ThenThrow New ArgumentNullException() End If If mail.SenderEmailType = "EX" ThenDim sender As Outlook.AddressEntry = mail.Sender If sender IsNot Nothing ThenIf 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 ElseReturn Nothing End If ElseReturn TryCast(sender.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS), String) End If Else Return Nothing End If ElseReturn mail.SenderEmailAddress End If End Function
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