Shawn Mclean
Shawn Mclean

Reputation: 57469

How to test sending emails from asp.net on development machine?

How do I accomplish this? The SMTP class throws error on dev machine about not finding an SMTP server. Is there a way to test sending emails on development machine?

Upvotes: 11

Views: 6891

Answers (7)

Christopher Davies
Christopher Davies

Reputation: 4551

I know this is an old thread, but I just stumbled upon this service: http://mailtrap.io/

Which is friggin brilliant. It serves as a dummy SMTP server for your app, and doesn't actually send the emails, but allows you to easily view them in the browser (or via API).

It's killer.

Upvotes: 4

Yohan P
Yohan P

Reputation: 494

Assuming by "test sending emails" you mean sending test emails instead of formal/unit testing, I like to use smtp4dev: http://smtp4dev.codeplex.com/

As the page explains, it's a dummy SMTP server, basically intercepting your outgoing messages from your app, allowing you to examine those messages and make sure everything works as you expect. It's a Windows app, which hopefully isn't an issue if you're developing for ASP.NET.

Upvotes: 2

jim tollan
jim tollan

Reputation: 22485

Shawn,

Straight from my web.config:

  <smtp deliveryMethod="SpecifiedPickupDirectory">
    <network host="ignored" />
    <specifiedPickupDirectory pickupDirectoryLocation="c:\email_c#" />
  </smtp>

this works fine insofar as being able to review the 'emails' that get saved into the pickupDirectoryLocation directory.

Give it a try...

Upvotes: 15

ARM
ARM

Reputation: 2385

There's a couple possible reasons for this.
1) Is your code configured to use local SMTP server during development and you've upgraded to windows 7? There's no longer a SMTP server available on your localhost. Find and download smtp4dev to allow your localhost to trap the sent Emails.
2) If you are using a remote SMTP server, check your windows firewall to confirm that you are allowed to send outgoing mail. If you are, then confirm that your machine/username has rights to send mail via that server. A quick telnet:25 to the server should let you know if your connection is refused or not.

Upvotes: 2

Chao
Chao

Reputation: 3043

Without seeing the exception there's not much we can do. As long as the details on your dev machine are pointing to a proper smtp server and have the correct credentials then your code won't be the issue and you should look further down the chain. I had an exception of the target machine refusing the request despite everything else being right. After spending ages double and triple checking the credentials, sending from our server etc I tracked the bug down to McAfee blocking email port 25...

Upvotes: 0

Marijn
Marijn

Reputation: 10557

You can dump the files on disk, by configuring System.Net.Mail.SmtpClient to use a deliveryMethod of type SpecifiedPickupDirectory, I found an example on SO

Upvotes: 5

tvanfosson
tvanfosson

Reputation: 532465

I usually do this by creating a wrapper class for the SmtpClient, then mocking out the wrapper in my tests. This removes the actual mail client/server dependencies from my unit tests. The wrapper itself is relatively thin so I don't feel the need to create tests for it. Usually I do my integration level testing for things like this as exploratory tests in my staging environment. The staging environment typically uses a production mail server, but with "fake" data -- e.g., customer email addresses replaced with my own.

Having said that, I would expect the client to work without errors even on your development system unless your mail server is protected by a firewall or something that would prevent your dev system talking to it. Can you give more detail on what the error you are experiencing?

Upvotes: 0

Related Questions