1

Is there a way to "read" the user E-Mail Address from Outlook Account which is logged in and send an E-Mail when I activate this Macro?

Sub MailSenden()

Dim olApp     As Object
Dim olOldBody As String

Rem Email erstellen
Set olApp = CreateObject("Outlook.Application")

With olApp.CreateItem(0)
    .GetInspector.Display
    olOldBody = .htmlBody
    .To = "carsten.asdf@xxx.yy"
    .Subject = "Testformular"
    .Body = "Das ist eine e-Mail" & Chr(13) & _
            "Viele Grüße..." & Chr(13) & Chr(13)
    .Attachments.Add "C:\Users\" & Environ$("USERNAME") & "\Desktop\" & "CSV-Export.csv"
    .Attachments.Add ActiveWorkbook.FullName
    .Send

End With

Kill "C:\Users\" & Environ$("USERNAME") & "\Desktop\" & "CSV-Export.csv"


End Sub

I need to get the "from" E-Mail address.

EDIT1: Solution for smtp

Msgbox        
CreateObject("Outlook.Application").GetNamespace("MAPI").Session.CurrentUser. _
AddressEntry.GetExchangeUser.PrimarySmtpAddress
Community
  • 1
  • 1
Carsten I
  • 33
  • 10

2 Answers2

0

Try MailItem.Session.CurrentUser.Address or MailItem.SenderEmailAddress both should work.

You may need to convert from an Exchange account to SMTP, to do this see:

How can I get the sender email address using Outlook.MailItem in VB.NET?

Ryan Wildry
  • 5,612
  • 1
  • 15
  • 35
0

To get the current user email address, please use following code.

With olApp
MsgBox .GetNamespace("MAPI").CurrentUser.Address
End With

to choose from which address you will send your email, please use this code. This way you are getting "FROM" tab inserted in created email.

With olApp.CreateItem(0)
    .SentOnBehalfOfName = "YourEmail@yourdomain.com"
    .GetInspector.Display
    olOldBody = .htmlBody
    .To = "carsten.asdf@xxx.yy"
    .Subject = "Testformular"
    .Body = "Das ist eine e-Mail" & Chr(13) & _
            "Viele Grüße..." & Chr(13) & Chr(13)
    .Attachments.Add "C:\Users\" & Environ$("USERNAME") & "\Desktop\" & "CSV-Export.csv"
    .Attachments.Add ActiveWorkbook.FullName
    .Send

End With

Please note that you should put .SentOnBehalfOfName = "YourEmail@yourdomain.com" right after With olApp.CreateItem(0) line of code.

Kresimir L.
  • 2,301
  • 2
  • 10
  • 22