【问题标题】:Get Email address from sender account从发件人帐户获取电子邮件地址
【发布时间】:2018-03-06 18:20:06
【问题描述】:

当我激活这个宏时,有没有办法从登录的 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

我需要获取“发件人”电子邮件地址。

EDIT1:smtp 的解决方案

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

【问题讨论】:

    标签: excel vba email outlook


    【解决方案1】:

    尝试MailItem.Session.CurrentUser.AddressMailItem.SenderEmailAddress 两者都应该工作。

    您可能需要从 Exchange 帐户转换为 SMTP,为此请参阅:

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

    【讨论】:

      【解决方案2】:

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

      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) 代码行之后。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-12
        • 2018-10-27
        相关资源
        最近更新 更多