0

I am building a form in C#, .NET, and MVC. On the back end the form will send its contents over email. For testing, I am using a local install of hMailServer.

Initially I set HMS to run as localhost.localdomain; the SMTP setting for "local host name" is localhost. I attempted to connect to it on port 587, like so:

SmtpClient smtp = new SmtpClient
{
    Host = WebConfigurationManager.AppSettings["emailServer"],
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = true,
    Credentials = networkCredential
};

I have double- and triple-checked that the credentials are the mail server user and password that I set. Here they are, in case this helps:

<add key="emailUser" value="user@localhost.localdomain"/>
<add key="emailPassword" value="~~~"/>
<add key="emailServer" value="localhost.localdomain"/>

When using localhost.localdomain, sending mail throws an exception, with the outer message: "Failure sending mail", and the inner message: "The remote name could not be resolved: 'localhost.localdomain'."

So I tried using companyname.com. Sending mail throws an exception, with the outer message: "Failure sending mail", and the inner message: "Unable to connect to the remote server."

I expect either my HMS domain config is wrong or my protocol config is wrong. The HMS documentation didn't help me, but I may not have known what to look for.

ETA hMail server status shows zero processed messages in a week, despite all my testing.

Codes with Hammer
  • 788
  • 3
  • 16
  • 47
  • Do you get a response when you `ping localhost.localdomain`? (I do here on Win 8.1) – Haney Dec 29 '14 at 14:13
  • Hadn't thought of that, so I tried it. Result: `Ping request could not find host localhost.localdomain. Please check the name and try again.` So I pinged `mail.companyname.com`, which successfully pinged `pop.where.secureserver.net`. I'm confused on the second ping, and would like to fix the first ping. – Codes with Hammer Dec 29 '14 at 14:20
  • 1
    Add an entry to your `hosts` file: `localhost.localdomain 127.0.0.1` and try again. – Haney Dec 29 '14 at 14:32
  • I added the entry, and ping worked properly. But the application still raised an exception, `Unable to connect to the remote server.` – Codes with Hammer Dec 29 '14 at 14:42

1 Answers1

1

Here is how I configured it for development: Created host file entry like following:

local.myname.com 127.0.0.1

Once done, I opened command prompt and make sure it is updated. I tested it by following:

tracert local.myname.com

It should return 127.0.0.1 if host file entry is updated.

Next, in hmail, we need to create a new domain: local.myname.com and add an email address with password. so your email address would be something like admin@local.myname.com.

Next is, in advance you need to double check the protocols configuration and IP range vs authentication configuration as well.

In my case I configured to block external incoming and outgoing emails and skipped authentication for internal emails. So basically that;s what you can do in advance - IP range configuration. Then with the development, you just need to make sure all your emails are *@local.myname.com and it should work.

Also enable logging in hmail to get detailed error that can help solve the problem because hmail's help documentation works directly with their error codes nicely.

hMail is actually good choice for real emails. For development, I would recommend using smtp4dev though.

Prashant Lakhlani
  • 5,758
  • 5
  • 25
  • 39
  • `tracert localhost.localdomain` properly returns 127.0.0.1 in one hop. I already have this domain in hMail, and the email address that I'm using. I have Application, SMTP, and POP3 logs turned on, but there have been no updates to them in some time. I turned on the other logs in case that helps. – Codes with Hammer Dec 29 '14 at 15:15
  • look at step 5 here in this document: http://swxben.com/setting-up-an-open-smtp-relay-in-an-intranet-with-hmailserver/ You can actually go ahead and skip the entire authentication part using that setting for internal emails. *ALSO* make sure to emails are *@yourdomain.com *ANOTHER* thing I have noticed here is, generally domains end up by .com, .net,.org or something. Just for the safe domain purpose, you can practice using that format just to make sure its not rejected due to that reason – Prashant Lakhlani Dec 29 '14 at 15:20
  • I set up the wildcard route and rule, with the route going to `localhost.localdomain` port 25. No change - the application still can't send mail, same exception message, still no updates to hMail log files. – Codes with Hammer Dec 29 '14 at 15:26
  • I tried installing smtp4dev, but it crashed when I sent a message to it. I had hMail stopped at the time. – Codes with Hammer Dec 29 '14 at 18:52
  • Huh. Once I set `EnableSsl` to `false` in my `SmtpClient` object, the message got through to smtp4dev. Now to try again with hMS. – Codes with Hammer Dec 29 '14 at 19:23
  • By testing with smtp4dev, I found my answer: My hMS instance was not running with SSL, so of course it wouldn't respond to an SSL mail client. Once I turned that off and switched to port 25, it worked. Thanks! – Codes with Hammer Dec 29 '14 at 19:38