System.ArgumentException: No sender found in the message.

0 votes
asked Jul 27, 2012 by DerekMJ (270 points)
edited Nov 5, 2012

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
answered Jul 28, 2012 by Tomas Knopp (58,890 points)
edited Nov 5, 2012
 
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>";
commented Jul 30, 2012 by Lukas Pokorny (97,590 points)
edited Jul 30, 2012

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.

commented Jul 30, 2012 by DerekMJ (270 points)
edited Jul 30, 2012

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.

commented Jul 30, 2012 by Tomas Knopp (58,890 points)
edited Jul 30, 2012

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.

commented Jul 30, 2012 by DerekMJ (270 points)
edited Jul 30, 2012

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."

commented Jul 31, 2012 by Tomas Knopp (58,890 points)
edited Jul 31, 2012

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!

commented Nov 2, 2012 by Tomas Knopp (58,890 points)
edited Nov 5, 2012

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

...