+1 vote
by (160 points)
edited

Hi, I have to send a message where unfortunately the receiver perceives < > in the header as invalid characters. I've used a previous question I found to remove the brackets from both the To and From fields, however this causes the message.To and message.From fields to be subsequently removed. Trying to assign to those fields then adds new fields back into the header with < >. Is there a way that I can have the following?

message.Headers["To"] = "abc@test.me" rather than "<abc@test.me>"

(Which I can do) AND at the same time

message.To as "abc@test.me" rather then blank or "<abc@test.me>".

As part of removing the < > from the To and From fields, I am also using the following on Message-Id, however it does not appear to remove the brackets.

mMessage.Headers["Message-Id"] = new MimeHeader("Message-Id", new Unparsed(mMessage.Headers["Message-Id"].Value.ToString()));

Any help you can give would be much appreciated.

Thanks,

Nolan

Applies to: Rebex Secure Mail
by (58.9k points)
edited

As to the other part of your question - please use the following code to remove the angle brackets from the MessageID:

mMessage.Headers["Message-Id"] = new MimeHeader("Message-Id", new Unparsed(mMessage.MessageId.Id));

3 Answers

0 votes
by (148k points)
edited
 
Best answer

UPDATE

In release 2014R1 we have added a new MailAddress constructor overload which enables to encode MailAddress without angle brackets like this:

MailMessage message = new MailMessage();
message.To = new MailAddress(address, true);

Hi Nolan,

I actually have a good news for you! I tried sending the message directly to Gmail and the brackets were not added this time. This means that it's Exchange server, not Gmail, that puts them there. As long as you use a SMTP server that doesn't have a habit of modifying your messages, the message should arrive at Gmail intact (without the brackets).

(On a related note, Gmail refuses to accept "MAIL FROM" and "RCPT TO" commands without the brackets, which is actually an acceptable behavior because SMTP protocol requires them. But this doesn't affect the message data where the brackets are optional.)

Lukas

by (160 points)
edited

Thanks Lukas for the extra investigation.

I'll work with our Technical team to see what options we have.

Nolan

0 votes
by (58.9k points)
edited

Hello,

I am afraid that you will not be able to use the MailMessage properties like To, From or MessageId once you modify the Headers to not contain the angle brackets.

m.Headers["To"] = new MimeHeader("To", new Unparsed("abc@domain.com"));
m.Headers["From"] = new MimeHeader("From", new Unparsed("efg@domain.com"));

To remove the angle brackets from the Message-ID header please do:

m.Headers["Message-Id"] = new MimeHeader("Message-Id", new Unparsed(m.MessageId.Id));

So the simplest solution if you really want to get rid of angle brackets is to use the standard properties and at the end (before saving or sending the MailMessage), just remove the angle brackets as needed.

But one more question why do you want to remove the angle brackets at all?

by (160 points)
edited

What are the standard properties? I've used the MailMessage.To field, but setting this just adds a header with the brackets anyway.

The reason we believe we need to do this is because Medicare Australia appear to parse the raw message, doesn't like the Angle Brackets. I have informed them that it's standard to have them so they should adjust accordingly, however trying to get them to budge is not likely to happen.

by (58.9k points)
edited

We are working on a new MailAddress overload which will enable to add the mail address without brackets (if there is no display name). We will post the hotfix here, when it is ready.

0 votes
by (58.9k points)
edited

Hello Nolan,

we have prepared a hotfix which adds a new overload to the MailAddress:

public MailAddress(string address, bool omitBrackets)

So now you can force the MailAddress to omit the angle brackets like this:

MailMessage message = new MailMessage();

message.From.Add(new MailAddress(address, true));
message.To = new MailAddress(address, true);

This way you are able to work directly with the From, To and other MailMessage properties which use MailAddress (like Sender, Bcc or CC for instance).

Please download the hotfix and give it a try. If you have an active Rebex Secure Mail licence, please write an email to support@rebex.net with your licence details and I will send you a full non-expiring hotfix version. The hotfix will be part of the 2014R1 release.

by (160 points)
edited

Hi Tomas,

I have applied your fix, and while I can see there are no brackets prior to calling Smtp.Send(message, "myMailHost") I still receive the brackets inside the message header when viewed in any client. (Gmail and Outlook)

Wireshark also shows that the MAIL FROM:an.email@address.com and a RCPT TO:to.email@address.com indicating that it's perhaps the Smtp.Send command that's adding brackets. When I look at the IMF message (for one that's been unencrypted.) The message shows that there are no angle brackets.

Do you have any ideas as to an approach I can take that will remove the brackets from the To field when a message is sent to GMail?

e.g., not have

From: test.me@abc.com.au To: test.me@abc.com

Thanks,

Nolan

by (148k points)
edited

The "MAIL FROM" and "RCPT TO" SMTP commands you see in Wireshark are not the actual mail message content, but SMTP protocol commands. And unlike the "From" and "To" headers in mail message headers, the brackets are actually required in those SMTP commands.

To see the actual message content sent to the SMTP server, instead of calling Smtp.Send(message, "myMailHost"), try this:

Smtp smtp = new Smtp();
smtp.Connect("myMAilHost");
smtp.LogWriter = new FileLogWriter("smtp-log.txt", LogLevel.Verbose);
smtp.Send(mail);

This will produce a verbose log where you can see the raw data as sent. I have tried this with the hotfix Tomas posted and the brackets are indeed missing from the message data. However, Gmail still shows the brackets! The only explanation is that either our SMTP server added them, or that Gmail itself added them... I will try sending directly to Gmail as well and report back.

(To make sure the "MAIL FROM" and "RCPT TO" play no role in this, I even tried temporarily removing those brackets from our code, but this had no effect at all.)

by (160 points)
edited

Hi Lukas,

Thanks for the testing and the logging info, I'll try that out after the Christmas break. I did try message.save and the file generated in that instance also did not add the brackets. I haven't been able to determine as yet if it is out Exchange server or as you said Gmail adding the brackets. All I know is that it can be done as Medicare Australia have sent me examples of messages with the brackets removed. They insist they have many clients using their software and none send brackets, but to me this seems odd as it's standard to have them. I'll be looking into this problem into the new year and see how it goes.

Thanks.

Nolan

...