I've successfully created an SmtpReceiveAgent with OnEndOfDataEvent. It should replace the sender of some emails, so the recipient sees the new sender and is able to reply to it.
This already works very well, just by setting:
MailItem.Message.From = new EmailRecipient(DisplayName, Address);
It doesn't seem to be necessary to also set the following places, like in many examples I found:
MailItem.Message.Sender = new EmailRecipient(DisplayName, Address);
MailItem.FromAddress = new RoutingAddress(Address);
I am able to set any random non-existing or external address, or any existing address of other users in the same company/domain.
However, when that user exists in our system, and it is not its primary address, it is replaced by the primary address. But that's exactly what I wanted: use alternative addresses... How to fix this?
No idea why this happened, but when I replaced
SmtpReceiveAgentandOnEndOfDataEventbyRoutingAgentandOnCategorizedMessage,it just started to work without other changes.
I also added setting
MailItem.FromAddressnow,so the
Return-Pathheader was affected correctly.