从发件人帐户获取电子邮件地址

时间:2017-09-25 17:34:03

标签: excel vba email outlook

有没有办法去"读" Outlook帐户中的用户电子邮件地址登录并在激活此宏时发送电子邮件?

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

我需要从#34;得到#34;电子邮件地址。

EDIT1:smtp的解决方案

Msgbox        
CreateObject("Outlook.Application").GetNamespace("MAPI").Session.CurrentUser. _
AddressEntry.GetExchangeUser.PrimarySmtpAddress

2 个答案:

答案 0 :(得分:0)

尝试MailItem.Session.CurrentUser.AddressMailItem.SenderEmailAddress两者都应该有效。

您可能需要从Exchange帐户转换为SMTP,为此请执行以下操作:

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

答案 1 :(得分:0)

要获取当前用户的电子邮件地址,请使用以下代码。

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

选择您要发送电子邮件的地址,请使用此代码。这样您就可以在创建的电子邮件中插入"FROM"标签了。

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

请注意,您应该在.SentOnBehalfOfName = "YourEmail@yourdomain.com"代码行之后放置With olApp.CreateItem(0)