You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VB.NET中获取Exchange环境下当前发件人邮箱地址?

解决VB.NET中获取Outlook当前发件人邮箱用于抄送的问题

嘿,我懂你踩的坑了——你现在的代码里,刚创建的MailItem还没和任何发件人账户绑定呢,所以SenderEmailAddress肯定是空的!要拿到当前Outlook用户的邮箱地址,得从Outlook的账户配置里取,而不是从空白的新邮件对象里要。

给你两种靠谱的解决方案:

方案1:从当前用户Exchange账户获取(适合Exchange环境)

这种方式直接取当前登录用户的主SMTP地址,简洁高效:

Dim oApp As New Outlook.Application()
' 获取Outlook命名空间
Dim oNamespace As Outlook.NameSpace = oApp.GetNamespace("MAPI")
' 获取当前用户的主SMTP邮箱地址
Dim currentUserEmail As String = oNamespace.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress

Dim oMsg As Outlook.MailItem = DirectCast(oApp.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
' 设置邮件基本信息
oMsg.To = "你的目标邮箱@example.com"
oMsg.Subject = "带自抄送的测试邮件"
oMsg.Body = "这是测试内容"
' 将当前用户邮箱添加到抄送
oMsg.CC = currentUserEmail

' 发送邮件
oMsg.Send()

方案2:遍历账户找默认发送账户(兼容POP3/IMAP等所有账户类型)

如果你的Outlook配置的是POP3或IMAP账户,上面的Exchange方式可能失效,这时候可以遍历所有账户,找到默认的发送账户:

Dim oApp As New Outlook.Application()
Dim currentUserEmail As String = String.Empty

' 遍历所有账户,找到默认发送账户
For Each acc As Outlook.Account In oApp.Session.Accounts
    If acc.Default = True Then
        currentUserEmail = acc.SmtpAddress
        Exit For
    End If
Next

If Not String.IsNullOrEmpty(currentUserEmail) Then
    Dim oMsg As Outlook.MailItem = DirectCast(oApp.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
    oMsg.To = "你的目标邮箱@example.com"
    oMsg.Subject = "带自抄送的测试邮件"
    oMsg.Body = "这是测试内容"
    oMsg.CC = currentUserEmail
    oMsg.Send()
Else
    MsgBox("无法找到默认发送账户,请检查Outlook配置")
End If

为什么你原来的代码不行?

刚创建的MailItem是完全空白的,还没有被分配任何发件人信息——SenderEmailAddress属性只有在邮件已经被发送、或者你手动指定了发件人之后才会有值。所以直接从新邮件对象里拿这个属性肯定拿不到东西啦。

内容的提问来源于stack exchange,提问作者user9684191

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:28:36