0 votes

We are using Mail.dll for an application which retrieves and stores e-mails locally for further processing from an Office 365 IMAP server.

This application runs for few months and we faced one minor issue.

Example EML File:

Date: Thu, 21 May 2025 08:00:00 +0000
Message-ID: <random-message-id>
Subject: Test Mail
Sender: MAILER-DAEMON <MAILER-DAEMON@LOCALHOST>
From: MAILER-DAEMON <MAILER-DAEMON@LOCALHOST>
To: Max Test <max@test.eu>

If we parse this EML via MailBuilder.CreateFromEmlFile() or MailBuilder.CreateFromEml() method, we get an IMail object as expected and Sender/From fields with mail address set to "MAILER-DAEMON@LOCALHOST".

If we try to create a MailBox object via constructur

new MailBox("MAILER-DAEMON@LOCALHOST")

we get following exception:

System.ArgumentException: 
'MAILER-DAEMON@LOCALHOST' is not a valid email address.

As a working solution, we already using following method now:

MailBox.CreateWithoutValidation("MAILER-DAEMON@LOCALHOST")

My colleagues would just need some clarification, as we didn't find this information in the manual/documentation or overseen it:
Is our assumption right, that for the MailBuilder.Create() methods no mail address validation happens in the first place?

ago by

1 Answer

0 votes
 
Best answer

Your assumption is correct.

During email parsing, mailboxes (email addresses) are not validated in any way. This is by-design; otherwise broken email address would prevent email to be parsed.

In other words when parsing, MailBox.CreateWithoutValidation method is used internally.

When you create a new email, you should be using MailBox constructor, that performs format validation.

The reason is that an email with a broken addresses usually can not be sent.

If for some reason, you need to create an email with a broken address, then MailBox.CreateWithoutValidation can be used.

Hope it helps.

ago by (303k points)
...