How would I get rid of an annoying "on behalf of" in the sender of a .msg file?

I have a .msg file and when I open it in Outlook I see something like this in the Sender information:

My Name myname@mycompany.com on behalf of My Name

Could you provide a code snippet that would allow me to get rid of the superfluous
“on behalf of My Name”?

c# or vb.net examples are welcome. I’ve been looking through examples but I can’t figure it out…

Hello @kidkeogh,

Welcome to our support forum!

SentRepresentingEmailAddress and SentRepresentingName properties control the “on behalf of” display in Outlook.

To remove the “on behalf of” text, you can try to set SentRepresentingName and SentRepresentingEmailAddress to null.

using Aspose.Email.Mapi;

var message = MapiMessage.Load("example.msg");

// Remove "on behalf of"
message.SentRepresentingName = null; // Clear the represented sender's name
message.SentRepresentingEmailAddress = null; // Clear the represented sender's email address

message.Save("updated_example.msg");

Alas… no that doesn’t work.

Okay I found a way around the problem but it’s a bit “hacky” so I’m not 100% happy about it, maybe if I explain what I am doing you might be able to figure out what the underlying problem is?

I decided to try saving the email as an “.eml” file so I can open it in Notepad to see what it looks like internally.

I noticed that sometimes the message looked like
From: My Name my.email@address.here on behalf of My Name

And sometimes it looked fine
From: My Name my.email@address.here

Often with pretty much the same email.

Here is what I am seeing when I save the email as an .eml

When it looks correct there will be two lines in the .eml file indicating who the sender is:

From: My Name my.email@address.here
Sender: My Name my.email@address.here

When it looks incorrect, the two lines look like:

From: My Name
Sender: My Name my.email@address.here

My “hack” to fix it was to read the .eml file into a string, find the first line that looks like
From: (some text) (followed by a line break)

In the above example, I know the sender is “My Name” and their address is my.email@address.here, so if I find the .eml’s “from” line looks like
From: My Name

then I replace that line with
From: My Name my.email@address.here

If it looks like anything else I’ll leave it alone.

If I change the From: line I then save it back to the original .msg file and that appears to resolve the problem.

Why, though, does changing the .SentRepresetingName and .SentRepresentingEmailAddress to null (or Nothing in VB) not work? I also tried using .SetStringPropertyValue to set the folowing values to empty strings:
MapiPropertyTag.PR_SENT_REPRESENTING_ADDRTYPE
MapiPropertyTag.PR_SENT_REPRESENTING_EMAIL_ADDRESS
MapiPropertyTag.PR_SENT_REPRESENTING_NAME

But that also doesn’t seem to work

The only thing that DOES appear to work is my dirty little hack converting the .msg to an .eml, changing the From line if necessary, and converting it back to a .msg again. Not ideal.

Okay watch this space but I think I may have cracked the problem.

I start by using your MAPI code to nullify the .SentRepresenting values

Then (in VB code) I set MailConversionOptions as follows:

        Dim oTMMOptions As MailConversionOptions
        oTMMOptions = New MailConversionOptions
        oTMMOptions.KeepOriginalEmailAddresses = True

and create an Aspose.Email.MailMessage

        oMsg = oMAPI.ToMailMessage(oTMMOptions)

After that I find that in oMsg the .Sender property is Nothing (null) and I set

        oMsg.From.Address = sFrom
        oMsg.From.DisplayName = sFromName

After that I do whatever else I need to do and save oMsg back to a .msg file and the “on behalf of” is gone.

Does that sound “good” to you? It seems to work for me and it sure sounds a lot less “hacky” than my original messin’ about with the .eml file

@kidkeogh ,

I’ll pass on your request to our developers and get back with the solution asap.

Thank you for sharing.

1 Like

In my own code (which I can’t fully share here) I do happen to have access to the sender’s name, so as a final check I check

oMAPI.SentRepresentingName

if this does NOT match the sender’s name I know that this is actually a legit “on behalf of” message and I will not overwrite the FromAddress and From.DisplayName further down. It also looks like in that case I should leave .KeepOriginalEmailAddress False

@kidkeogh
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): EMAILNET-41469

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Hi @kidkeogh
Unfortunately, we were unable to reproduce your problem on our side.
Please follow a few steps:
-make sure you are using the latest version of Aspose.Email(24.11)
-we have made a simple application on VB with an example message to show that our fix works (don’t forget to add a reference to Aspose.Email), please check it on your side.
-please share examples of messages for which our fix does not work so that we can investigate these cases on our side.

Thanks for your colaboration.
ConsoleAppVB.zip (41.8 KB)

Thank you @alexander.pavlov

Issue is currently with the client. My code samples above may be working, will need to leave it with client for a while to see whether they consistently do.

@kidkeogh ,

Please let us know if there are any updates or further assistance required in the meantime.

1 Like

My apologies Margarita, we were able to resolve the issue, thanks to your suggestions.

@kidkeogh ,

You’re welcome!

If you have questions in future or need assistance, feel free to ask.