0 votes
by (270 points)
edited

I get the following exception when using Rebex.Smtp.Send(MailMessage):

System.ArgumentException: No sender found in the message.

This occurs when I create a Rebex.Mail.MailMessage and then set a few properties (just Subject, BodyText, From and To) and send the message with Rebex.Smtp.Send.

The problem goes away when I change the value I set in the From property from "foo" to "foo@example.com".

Unfortunately it is very important that I be able to set a from address that doesn't include a domain name. The SMTP server I am working with is an internal mail server on a private network. It is not connected to the internet and does not have a domain name, MX record, etc. Furthermore, even if the domain name were necessary to be part of the from address, it is not appropriate that the Rebex.Smtp.Send(MailMessage) method should throw ArgumentException for something that is not an argument to that method (it appears to be occurring on some argument deep inside Rebex code). If there is a problem with something I am setting in the MailMessage object it should throw the exception when setting the property, not when I am attempting to send.

Is there a way I can resolve this and set a from address that doesn't have a domain name?

Applies to: Rebex Secure Mail

1 Answer

0 votes
by (58.9k points)
edited
 
Best answer

Thanks for your question.

There is a way to set an address that doesn't have a domain name. Just use the Rebex.Mime.Headers.MailAddress class and its MailAddress(address, displayName) constructor like this:

        MailMessage mail = new MailMessage();
        mail.Subject = "subject";
        mail.BodyText = "text";
        mail.From = new MailAddress("foo", null);
        mail.To = new MailAddress("boo", null);

        Smtp.Send(mail, "serverName");

Does it work for you now?

The problem is that when setting the sender like this:

mail.From = "foo";

we process the string "foo" as being the displayName, not the email address. Thus later on, when the MailMessage is being sent and the sender's addresses are looked up, there is a header which contains no email address, only the name "foo". So the ArgumentException is thrown at this point. We will consider whether throwing an SmtpException would be better at this point.


Update: The exceptions were fixed in Rebex Secure Mail 2012 R3.

By the way, you can also use this code to parse "foo" as email address:

mail.From = "<foo>";
by (144k points)
edited

DerekMJ is 100% right about the ArgumentException. No method should ever throw an ArgumentException for something that is not an argument to that method. We will fix that for the next release.

by (270 points)
edited

Yes, this works. Thanks!

Throwing an SmtpException would be better than an ArgumentException. However, I also wonder why assigning a single string value to any of the address fields should ever assume that the string value is a display name and not an email address? I can't see why anyone would want to set a display name and not an email address.

In the future I will always wrap an address assignment in a MailAddress object so I will avoid this ambiguity.

by (58.9k points)
edited

In the 1-parameter MailAddress constructor we parse the string in format which is usually used, i.e. 'Name Surname <name.surname@company.net>' or ' "Name Surname" <name.surname@company.net>' If the string contains only one part (address or displayName) we do our best and if the '@' character is missing this is really not an email address. Most of the servers reject addresses without the domain part so that is why we take it as display name in this case.

by (270 points)
edited

Okay, I guess that makes sense. I will say that this behavior is not very apparent from MailAddress(string) in the class library documentation which describes the string parameter as just: "Mail address."

by (58.9k points)
edited

Yes, you are absolutely right. The documentation of the parameter should mention that two things are extracted from the parameter address: Name and the email address. We will fix it for the next release. Thanks!

by (58.9k points)
edited

We have fixed the documentation of the address parameter in the Rebex 2012 R3 release. You can also check the documentation online.

...