使用VBA

时间:2016-05-02 10:33:10

标签: vba outlook-2010

我有大量的转发电子邮件地址,这些地址都设置为转发到同一个电子邮件帐户。我觉得这很有用,因为如果一个企业遭到黑客入侵而我的电子邮件地址被盗,那么我只会更改该企业的电子邮件地址。例如,“amazon@mydomain.com”,“ebay@mydomain.com”和“facebook@mydomain.com”都将被转发到“mailbox@mydomain.com”。

当我想向企业发送电子邮件时,我必须进入Outlook并更​​改设置的帐户以将转发电子邮件地址作为电子邮件地址。我觉得这很讨厌。我知道我可以在撰写电子邮件时更改电子邮件的来源,但收件人会看到“J Smith代表newaddress@mydomain.com”。我希望它只显示我在from字段中使用的地址,就像我进入帐户设置并更改那里的电子邮件地址一样。

最好设置一个宏,询问我想要使用哪个电子邮件地址,然后为我发送电子邮件。我已经查找了如何更改VBA中的电子邮件帐户详细信息,但看起来好像详细信息都是只读的。有没有办法彻底改变我的“来自”电子邮件地址?甚至在VBA中设置一个新的电子邮件帐户并在发送后立即将其删除?

2 个答案:

答案 0 :(得分:0)

尝试使用组合框和按钮创建用户表单。将所有可用帐户加载到组合框中,以便从中进行选择:

Private Sub UserForm_Initialize()

Dim acc As Account

For Each acc In ThisOutlookSession.Session.Accounts

    Me.ComboBox1.AddItem acc.UserName
Next acc
End Sub

然后在选择正确帐户的按钮中添加一些代码:

Dim objApp As Outlook.Application
Dim objMail As Outlook.MailItem

Set objApp = ThisOutlookSession.Application

Set objMail = objApp.CreateItem(olMailItem)

With objMail

    .To = "lala@lala.com"
    .CC = ""
    .BCC = ""
    .Subject = "Test"
    .Body = "Test"

    Dim i As Integer

    For i = 1 To ThisOutlookSession.Session.Accounts.Count Step 1

        If ThisOutlookSession.Session.Accounts.Item(i).UserName = Me.ComboBox1.Value Then

            .SendUsingAccount = ThisOutlookSession.Session.Accounts.Item(i)
        End If
    Next i

    .Display
End With

也许在您创建新电子邮件时会调用某个事件,否则您必须添加一个按钮或其他内容才能显示该表单。

答案 1 :(得分:0)

I had this exact same problem and ended up being able to solve it by installing Outlook Redemption and using the following script...

                ' Redemption code below. Must install Redemption to work.
                ' http://www.dimastr.com/redemption/faq.htm#14

                Dim sItem, Tag
                Set sItem = CreateObject("Redemption.SafeMailItem")
                sItem.Item = oMailItem
                Tag = sItem.GetIDsFromNames("{00020386-0000-0000-C000-000000000046}", "From")
                Tag = Tag Or &H1E     'the type is PT_STRING8
                sItem.Fields(Tag) = GetHashedReply(oMailItem)
                Tag = sItem.GetIDsFromNames("{00020386-0000-0000-C000-000000000046}", "Sender")
                Tag = Tag Or &H1E     'the type is PT_STRING8
                sItem.Fields(Tag) = GetHashedReply(oMailItem)
                sItem.Subject = sItem.Subject  'to trick Outlook into thinking that something has changed
                sItem.Save

...where oMailItem is a normal Outlook MailItem that you can get with CreateItem() or get passed to you in the ItemSend() parameters.