Quantcast
Channel: Second Life of a Hungarian SharePoint Geek
Viewing all articles
Browse latest Browse all 206

Sending mails from a test SharePoint system

$
0
0

In the past weeks I worked on a few existing applications at a customer. Part of these applications was sending e-mails (for example, from event receivers, workflow, timer jobs or simply from web-pats or application pages). The test / developer systems used the copy of the content database from the production system, only a very limited set of test users were available for testing purposes.

The applications either simply ignored the possibility that they might be running in the test systems and sent mail notifications using SPUtility.SendEmail / System.Net.Mail.SmtpClient, or they contained a hardcoded logic wired up on the naming pattern of the server URLs, like (myserver.company.com / myservertest.company.com / myserverdev.company.com) and sent the mails to a dedicated mail address fro dev. and test systems.

It means, users of the real system might become notifications from the test system (not knowing the source of the mail) regarding a task, that they already processed in the productive system, or we have the mails in the dedicated mailbox without the information who would be the addressee of the mail in the real system. I found both cases bad enough.

Instead of this kind of implementation, one could apply Dependency Injection (for example using Unity) and use different approaches in the different systems. In the productive system, your mail processor may send mails as usual, however in the test and developer system you should save the mails into a file or send them to the dedicated mailbox as an attachment. You should be able to set the active mail processor from configuration, without altering the source code.

However, it might be even simpler, if you can (and usually you must be able to!) rewrite your code to use System.Net.Mail.SmtpClient instead of SPUtility.SendEmail. In the case of SmtpClient there is a less-known feature, namely that you can set the delivery mode not only from code, but also from configuration, like this:

<system.net>
  <mailSettings>
    <smtp deliveryMethod="SpecifiedPickupDirectory">
     <specifiedPickupDirectory pickupDirectoryLocation="C:\somedirectory" />
    </smtp>
  </mailSettings>
</system.net>

If you include this node into the <configuration> section of the web.config and owstimer.exe.config files in the test / developer systems, the mails would not be sent through SMTP to the outgoing mail server, but saved as .eml files into the specified folder instead. In that folder you can check the mail content and addresses, attachments, etc.

Note: Be aware, that the method described here does not apply to the mails sent through SPUtility.SendEmail or standard SharePoint mails, like alerts, notifications when a user becomes new permissions or access requests mails, as SharePoint has its own way of implementation for mail sending that is not built on top of SmtpClient.



Viewing all articles
Browse latest Browse all 206

Trending Articles