Setting the sender email address according to the recipient

…衆ロ難τιáo~ 提交于 2019-12-13 02:59:10

问题


How can I set programmatically in an email, to which I reply, the sender address to the recipient address?

Is there a way in VBA? I don't know where to start so I apologize because I cannot show any code.


回答1:


Credit to @Al Bundy for correcting this. This solution is based on this post.

In ThisOutlookSession:

Option Explicit

Private WithEvents objMail As MailItem
Private assignmentHandled As Boolean

'Set MailItem
Private Sub Application_ItemLoad(ByVal Item As Object)
    If Item.Class = olMail And Not assignmentHandled Then
        Set objMail = Item
    End If
End Sub

'Handle reply/replayAll from triggering ItemLoad again
Private Sub objMail_Open(Cancel As Boolean)
    assignmentHandled = True
End Sub

'Reply
Private Sub objMail_Reply(ByVal Response As Object, Cancel As Boolean)
    Call SetSentOnBehalfOfName(Response)
End Sub

'Reply all
Private Sub objMail_ReplyAll(ByVal Response As Object, Cancel As Boolean)
    Call SetSentOnBehalfOfName(Response)
End Sub

'MailItem closed
Private Sub objMail_Close(Cancel As Boolean)
    assignmentHandled = False
End Sub

' Avoid repeating code
Private Sub SetSentOnBehalfOfName(ByRef Response As Object)
    Response.SentOnBehalfOfName = objMail.To
    assignmentHandled = False
End Sub



回答2:


Outlook will not let you set message sender to an arbitrary email address - you must have an explicit permission (in case of Exchange) to set the MailItem.SentOnBehalfOfName property. In case of a POp3/SMTP account, set the MailItem.Account property.



来源:https://stackoverflow.com/questions/44019988/setting-the-sender-email-address-according-to-the-recipient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!